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()
})
import * as program from 'commander'
-import { initDatabase } from '../server/initializers'
+import { initDatabaseModels } from '../server/initializers'
import { UserModel } from '../server/models/account/user'
program
process.exit(-1)
}
-initDatabase(true)
+initDatabaseModels(true)
.then(() => {
return UserModel.loadByUsername(program['user'])
})
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()
})
// ----------- 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'
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, () => {
// ---------------------------------------------------------------------------
-const LAST_MIGRATION_VERSION = 120
+const LAST_MIGRATION_VERSION = 125
// ---------------------------------------------------------------------------
}
})
-async function initDatabase (silent: boolean) {
+async function initDatabaseModels (silent: boolean) {
sequelizeTypescript.addModels([
ApplicationModel,
AvatarModel,
// ---------------------------------------------------------------------------
export {
- initDatabase,
+ initDatabaseModels,
sequelizeTypescript
}
--- /dev/null
+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
+}
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'
// 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
}
await migrationScript.up(options)
// Update the new migration version
- await ApplicationModel.updateMigrationVersion(versionScript, t)
+ await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t })
})
}