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 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()
})

View File

@ -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'])
})

View File

@ -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()
})

View File

@ -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, () => {

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([
ApplicationModel,
AvatarModel,
@ -81,6 +81,6 @@ async function initDatabase (silent: boolean) {
// ---------------------------------------------------------------------------
export {
initDatabase,
initDatabaseModels,
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 { 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 })
})
}