From 91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Dec 2017 17:46:23 +0100 Subject: [PATCH] Add migration --- scripts/danger/clean/cleaner.ts | 4 +-- scripts/reset-password.ts | 4 +-- scripts/update-host.ts | 4 +-- server.ts | 15 ++++---- server/initializers/constants.ts | 2 +- server/initializers/database.ts | 4 +-- .../migrations/0125-table-lowercase.ts | 36 +++++++++++++++++++ server/initializers/migrator.ts | 21 ++++++++--- 8 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 server/initializers/migrations/0125-table-lowercase.ts diff --git a/scripts/danger/clean/cleaner.ts b/scripts/danger/clean/cleaner.ts index a22e2ed6a..009f8f7a0 100644 --- a/scripts/danger/clean/cleaner.ts +++ b/scripts/danger/clean/cleaner.ts @@ -1,8 +1,8 @@ import * as Promise from 'bluebird' import * as rimraf from 'rimraf' -import { CONFIG, initDatabase, sequelizeTypescript } from '../../../server/initializers' +import { CONFIG, initDatabaseModels, sequelizeTypescript } from '../../../server/initializers' -initDatabase(true) +initDatabaseModels(true) .then(() => { return sequelizeTypescript.drop() }) diff --git a/scripts/reset-password.ts b/scripts/reset-password.ts index a6863f807..6516edc28 100755 --- a/scripts/reset-password.ts +++ b/scripts/reset-password.ts @@ -1,5 +1,5 @@ import * as program from 'commander' -import { initDatabase } from '../server/initializers' +import { initDatabaseModels } from '../server/initializers' import { UserModel } from '../server/models/account/user' program @@ -11,7 +11,7 @@ if (program['user'] === undefined) { process.exit(-1) } -initDatabase(true) +initDatabaseModels(true) .then(() => { return UserModel.loadByUsername(program['user']) }) diff --git a/scripts/update-host.ts b/scripts/update-host.ts index ba4656b75..eccf203ea 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -1,9 +1,9 @@ import { getServerAccount } from '../server/helpers' -import { initDatabase } from '../server/initializers' +import { initDatabaseModels } from '../server/initializers' import { AccountFollowModel } from '../server/models/account/account-follow' import { VideoModel } from '../server/models/video/video' -initDatabase(true) +initDatabaseModels(true) .then(() => { return getServerAccount() }) diff --git a/server.ts b/server.ts index 2e9ed31d2..a89cdd69a 100644 --- a/server.ts +++ b/server.ts @@ -40,12 +40,16 @@ if (errorMessage !== null) { // ----------- Database ----------- // Do not use barrels because we don't want to load all modules here (we need to initialize database first) import { logger } from './server/helpers/logger' + // Initialize database and models -import { initDatabase } from './server/initializers/database' -initDatabase(false).then(() => onDatabaseInitDone()) +import { initDatabaseModels } from './server/initializers/database' +import { migrate } from './server/initializers/migrator' +migrate() + .then(() => initDatabaseModels(false)) + .then(() => onDatabaseInitDone()) // ----------- PeerTube modules ----------- -import { migrate, installApplication } from './server/initializers' +import { installApplication } from './server/initializers' import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib' import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' @@ -154,9 +158,8 @@ app.use(function (err, req, res, next) { function onDatabaseInitDone () { const port = CONFIG.LISTEN.PORT - // Run the migration scripts if needed - migrate() - .then(() => installApplication()) + + installApplication() .then(() => { // ----------- Make the server listening ----------- server.listen(port, () => { diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index f539eb2ee..341086bd6 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -8,7 +8,7 @@ import { isTestInstance, root } from '../helpers/core-utils' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 120 +const LAST_MIGRATION_VERSION = 125 // --------------------------------------------------------------------------- diff --git a/server/initializers/database.ts b/server/initializers/database.ts index f9e24c6b8..2260938b1 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -50,7 +50,7 @@ const sequelizeTypescript = new SequelizeTypescript({ } }) -async function initDatabase (silent: boolean) { +async function initDatabaseModels (silent: boolean) { sequelizeTypescript.addModels([ ApplicationModel, AvatarModel, @@ -81,6 +81,6 @@ async function initDatabase (silent: boolean) { // --------------------------------------------------------------------------- export { - initDatabase, + initDatabaseModels, sequelizeTypescript } diff --git a/server/initializers/migrations/0125-table-lowercase.ts b/server/initializers/migrations/0125-table-lowercase.ts new file mode 100644 index 000000000..78041ccb0 --- /dev/null +++ b/server/initializers/migrations/0125-table-lowercase.ts @@ -0,0 +1,36 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize +}): Promise { + await utils.queryInterface.renameTable('Applications', 'application') + await utils.queryInterface.renameTable('AccountFollows', 'accountFollow') + await utils.queryInterface.renameTable('AccountVideoRates', 'accountVideoRate') + await utils.queryInterface.renameTable('Accounts', 'account') + await utils.queryInterface.renameTable('Avatars', 'avatar') + await utils.queryInterface.renameTable('BlacklistedVideos', 'videoBlacklist') + await utils.queryInterface.renameTable('Jobs', 'job') + await utils.queryInterface.renameTable('OAuthClients', 'oAuthClient') + await utils.queryInterface.renameTable('OAuthTokens', 'oAuthToken') + await utils.queryInterface.renameTable('Servers', 'server') + await utils.queryInterface.renameTable('Tags', 'tag') + await utils.queryInterface.renameTable('Users', 'user') + await utils.queryInterface.renameTable('VideoAbuses', 'videoAbuse') + await utils.queryInterface.renameTable('VideoChannels', 'videoChannel') + await utils.queryInterface.renameTable('VideoChannelShares', 'videoChannelShare') + await utils.queryInterface.renameTable('VideoFiles', 'videoFile') + await utils.queryInterface.renameTable('VideoShares', 'videoShare') + await utils.queryInterface.renameTable('VideoTags', 'videoTag') + await utils.queryInterface.renameTable('Videos', 'video') +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts index f3a05cc8c..bb2539fc8 100644 --- a/server/initializers/migrator.ts +++ b/server/initializers/migrator.ts @@ -1,6 +1,5 @@ import * as path from 'path' import { logger, readdirPromise } from '../helpers' -import { ApplicationModel } from '../models/application/application' import { LAST_MIGRATION_VERSION } from './constants' import { sequelizeTypescript } from './database' @@ -11,9 +10,23 @@ async function migrate () { // The installer will do that if (tables.length === 0) return - let actualVersion = await ApplicationModel.loadMigrationVersion() + let actualVersion: number = null + + // Search in "Applications" or "application" tables + try { + const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "Applications"') + if (rows && rows[ 0 ] && rows[ 0 ].migrationVersion) { + actualVersion = rows[ 0 ].migrationVersion + } + } catch { + const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "application"') + if (rows && rows[0] && rows[0].migrationVersion) { + actualVersion = rows[0].migrationVersion + } + } + if (actualVersion === null) { - await ApplicationModel.create({ migrationVersion: 0 }) + await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)') actualVersion = 0 } @@ -88,6 +101,6 @@ async function executeMigration (actualVersion: number, entity: { version: strin await migrationScript.up(options) // Update the new migration version - await ApplicationModel.updateMigrationVersion(versionScript, t) + await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t }) }) } -- 2.25.1