1
0
Fork 0

Add migration

This commit is contained in:
Chocobozzz 2017-12-13 17:46:23 +01:00
parent 65b3ed25fc
commit 91fea9fc48
No known key found for this signature in database
GPG key ID: 583A612D890159BE
8 changed files with 71 additions and 19 deletions

View file

@ -1,8 +1,8 @@
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import * as rimraf from 'rimraf' import * as rimraf from 'rimraf'
import { CONFIG, initDatabase, sequelizeTypescript } from '../../../server/initializers' import { CONFIG, initDatabaseModels, sequelizeTypescript } from '../../../server/initializers'
initDatabase(true) initDatabaseModels(true)
.then(() => { .then(() => {
return sequelizeTypescript.drop() return sequelizeTypescript.drop()
}) })

View file

@ -1,5 +1,5 @@
import * as program from 'commander' import * as program from 'commander'
import { initDatabase } from '../server/initializers' import { initDatabaseModels } from '../server/initializers'
import { UserModel } from '../server/models/account/user' import { UserModel } from '../server/models/account/user'
program program
@ -11,7 +11,7 @@ if (program['user'] === undefined) {
process.exit(-1) process.exit(-1)
} }
initDatabase(true) initDatabaseModels(true)
.then(() => { .then(() => {
return UserModel.loadByUsername(program['user']) return UserModel.loadByUsername(program['user'])
}) })

View file

@ -1,9 +1,9 @@
import { getServerAccount } from '../server/helpers' import { getServerAccount } from '../server/helpers'
import { initDatabase } from '../server/initializers' import { initDatabaseModels } from '../server/initializers'
import { AccountFollowModel } from '../server/models/account/account-follow' import { AccountFollowModel } from '../server/models/account/account-follow'
import { VideoModel } from '../server/models/video/video' import { VideoModel } from '../server/models/video/video'
initDatabase(true) initDatabaseModels(true)
.then(() => { .then(() => {
return getServerAccount() return getServerAccount()
}) })

View file

@ -40,12 +40,16 @@ if (errorMessage !== null) {
// ----------- Database ----------- // ----------- Database -----------
// Do not use barrels because we don't want to load all modules here (we need to initialize database first) // 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' import { logger } from './server/helpers/logger'
// Initialize database and models // Initialize database and models
import { initDatabase } from './server/initializers/database' import { initDatabaseModels } from './server/initializers/database'
initDatabase(false).then(() => onDatabaseInitDone()) import { migrate } from './server/initializers/migrator'
migrate()
.then(() => initDatabaseModels(false))
.then(() => onDatabaseInitDone())
// ----------- PeerTube modules ----------- // ----------- PeerTube modules -----------
import { migrate, installApplication } from './server/initializers' import { installApplication } from './server/initializers'
import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib' import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib'
import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers'
@ -154,9 +158,8 @@ app.use(function (err, req, res, next) {
function onDatabaseInitDone () { function onDatabaseInitDone () {
const port = CONFIG.LISTEN.PORT const port = CONFIG.LISTEN.PORT
// Run the migration scripts if needed
migrate() installApplication()
.then(() => installApplication())
.then(() => { .then(() => {
// ----------- Make the server listening ----------- // ----------- Make the server listening -----------
server.listen(port, () => { server.listen(port, () => {

View file

@ -8,7 +8,7 @@ import { isTestInstance, root } from '../helpers/core-utils'
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const LAST_MIGRATION_VERSION = 120 const LAST_MIGRATION_VERSION = 125
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View file

@ -50,7 +50,7 @@ const sequelizeTypescript = new SequelizeTypescript({
} }
}) })
async function initDatabase (silent: boolean) { async function initDatabaseModels (silent: boolean) {
sequelizeTypescript.addModels([ sequelizeTypescript.addModels([
ApplicationModel, ApplicationModel,
AvatarModel, AvatarModel,
@ -81,6 +81,6 @@ async function initDatabase (silent: boolean) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
initDatabase, initDatabaseModels,
sequelizeTypescript sequelizeTypescript
} }

View file

@ -0,0 +1,36 @@
import * as Sequelize from 'sequelize'
async function up (utils: {
transaction: Sequelize.Transaction,
queryInterface: Sequelize.QueryInterface,
sequelize: Sequelize.Sequelize
}): Promise<void> {
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
}

View file

@ -1,6 +1,5 @@
import * as path from 'path' import * as path from 'path'
import { logger, readdirPromise } from '../helpers' import { logger, readdirPromise } from '../helpers'
import { ApplicationModel } from '../models/application/application'
import { LAST_MIGRATION_VERSION } from './constants' import { LAST_MIGRATION_VERSION } from './constants'
import { sequelizeTypescript } from './database' import { sequelizeTypescript } from './database'
@ -11,9 +10,23 @@ async function migrate () {
// The installer will do that // The installer will do that
if (tables.length === 0) return 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) { if (actualVersion === null) {
await ApplicationModel.create({ migrationVersion: 0 }) await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)')
actualVersion = 0 actualVersion = 0
} }
@ -88,6 +101,6 @@ async function executeMigration (actualVersion: number, entity: { version: strin
await migrationScript.up(options) await migrationScript.up(options)
// Update the new migration version // Update the new migration version
await ApplicationModel.updateMigrationVersion(versionScript, t) await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t })
}) })
} }