From 91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Dec 2017 17:46:23 +0100 Subject: Add migration --- server/initializers/constants.ts | 2 +- server/initializers/database.ts | 4 +-- .../migrations/0125-table-lowercase.ts | 36 ++++++++++++++++++++++ server/initializers/migrator.ts | 21 ++++++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 server/initializers/migrations/0125-table-lowercase.ts (limited to 'server') 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 }) }) } -- cgit v1.2.3