Migration script in prevision of the future settings "chat-type".
This commit is contained in:
parent
24c764c3a5
commit
11d79fc611
88
server/lib/migration/settings.ts
Normal file
88
server/lib/migration/settings.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import { pluginShortName } from '../helpers'
|
||||||
|
import type { ChatType } from '../settings'
|
||||||
|
|
||||||
|
async function _migrateChatTypeSetting (options: RegisterServerOptions): Promise<void> {
|
||||||
|
const peertubeHelpers = options.peertubeHelpers
|
||||||
|
const logger = peertubeHelpers.logger
|
||||||
|
// Previous to plugin v3.0.0, there was multiple checkbox and input-text for settings the plugin mode.
|
||||||
|
// With Peertube v2.3.0, we can replace all these settings with a single select.
|
||||||
|
// This function migrates old values if needed.
|
||||||
|
// NB: we cant use safely settingsManager.getSetting, because settings are not registered yet.
|
||||||
|
logger.info('Checking if we need to migrate chat-type')
|
||||||
|
if (!/^[-a-z]+$/.test(pluginShortName)) {
|
||||||
|
// to prevent sql injection... be sure there is no special char here.
|
||||||
|
throw new Error(`Wrong pluginShortName '${pluginShortName}'`)
|
||||||
|
}
|
||||||
|
const [results] = await peertubeHelpers.database.query(
|
||||||
|
'SELECT "settings" FROM "plugin"' +
|
||||||
|
' WHERE "plugin"."name" = :pluginShortName',
|
||||||
|
{
|
||||||
|
replacements: {
|
||||||
|
pluginShortName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if (!Array.isArray(results)) {
|
||||||
|
throw new Error('_migrateChatTypeSetting: query result is not an array.')
|
||||||
|
}
|
||||||
|
if (results.length === 0) {
|
||||||
|
logger.error('Plugin not found in database')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (results.length > 1) {
|
||||||
|
logger.error('Multiple lines for plugin in database, dont know which one to migrate... Aborting.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const settings = results[0].settings
|
||||||
|
if (!settings) {
|
||||||
|
logger.info('Plugin settings are empty in database, no migration needed.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (typeof settings !== 'object') {
|
||||||
|
logger.error('Plugin settings in database seems to be invalid json')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ('chat-type' in settings) {
|
||||||
|
logger.info('The setting chat-type is already here, no need to migrate.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info('The setting chat-type is not here, checking if we have to migrate from previous settings...')
|
||||||
|
let chatType: ChatType | undefined
|
||||||
|
if (settings['chat-use-prosody'] === true) {
|
||||||
|
chatType = 'builtin-prosody'
|
||||||
|
} else if (settings['chat-use-builtin'] === true) {
|
||||||
|
chatType = 'builtin-converse'
|
||||||
|
} else if (((settings['chat-uri'] || '') as string) !== '') {
|
||||||
|
chatType = 'external-uri'
|
||||||
|
} else {
|
||||||
|
logger.info('It seems there was no previous active chat configuration.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`We have to set chat-type to value '${chatType}'.`)
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
await peertubeHelpers.database.query(
|
||||||
|
'UPDATE "plugin" ' +
|
||||||
|
' SET "settings" = "settings" || :value ' +
|
||||||
|
' WHERE "name" = :pluginShortName',
|
||||||
|
{
|
||||||
|
replacements: {
|
||||||
|
pluginShortName,
|
||||||
|
value: JSON.stringify({
|
||||||
|
'chat-type': chatType
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function migrateSettings (options: RegisterServerOptions): Promise<void> {
|
||||||
|
const logger = options.peertubeHelpers.logger
|
||||||
|
logger.info('Checking if there is a migration script to launch...')
|
||||||
|
await _migrateChatTypeSetting(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
migrateSettings
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
import { getBaseRouterRoute } from './helpers'
|
import { getBaseRouterRoute } from './helpers'
|
||||||
import { ensureProsodyRunning, ensureProsodyNotRunning } from './prosody/ctl'
|
import { ensureProsodyRunning, ensureProsodyNotRunning } from './prosody/ctl'
|
||||||
|
|
||||||
export function initSettings (options: RegisterServerOptions): void {
|
type ChatType = 'disabled' | 'builtin-prosody' | 'builtin-converse' | 'external-uri'
|
||||||
|
|
||||||
|
function initSettings (options: RegisterServerOptions): void {
|
||||||
const { peertubeHelpers, registerSetting, settingsManager } = options
|
const { peertubeHelpers, registerSetting, settingsManager } = options
|
||||||
|
|
||||||
registerSetting({
|
registerSetting({
|
||||||
@ -214,3 +216,8 @@ Before asking for help, please use this diagnostic tool:
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
ChatType,
|
||||||
|
initSettings
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { migrateSettings } from './lib/migration/settings'
|
||||||
import { initSettings } from './lib/settings'
|
import { initSettings } from './lib/settings'
|
||||||
import { initRouters } from './lib/routers/index'
|
import { initRouters } from './lib/routers/index'
|
||||||
import { ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
|
import { ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
|
||||||
@ -10,6 +11,8 @@ let OPTIONS: RegisterServerOptions | undefined
|
|||||||
async function register (options: RegisterServerOptions): Promise<any> {
|
async function register (options: RegisterServerOptions): Promise<any> {
|
||||||
OPTIONS = options
|
OPTIONS = options
|
||||||
|
|
||||||
|
await migrateSettings(options)
|
||||||
|
|
||||||
await initSettings(options)
|
await initSettings(options)
|
||||||
await initRouters(options)
|
await initRouters(options)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user