aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-13 17:46:23 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 17:46:23 +0100
commit91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d (patch)
tree1f227acea3e9684928dedbc070cc5fa6b3af42ee
parent65b3ed25fc33c63ab2c56372c1a010541f8fddc2 (diff)
downloadPeerTube-91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d.tar.gz
PeerTube-91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d.tar.zst
PeerTube-91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d.zip
Add migration
-rw-r--r--scripts/danger/clean/cleaner.ts4
-rwxr-xr-xscripts/reset-password.ts4
-rwxr-xr-xscripts/update-host.ts4
-rw-r--r--server.ts15
-rw-r--r--server/initializers/constants.ts2
-rw-r--r--server/initializers/database.ts4
-rw-r--r--server/initializers/migrations/0125-table-lowercase.ts36
-rw-r--r--server/initializers/migrator.ts21
8 files changed, 71 insertions, 19 deletions
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 @@
1import * as Promise from 'bluebird' 1import * as Promise from 'bluebird'
2import * as rimraf from 'rimraf' 2import * as rimraf from 'rimraf'
3import { CONFIG, initDatabase, sequelizeTypescript } from '../../../server/initializers' 3import { CONFIG, initDatabaseModels, sequelizeTypescript } from '../../../server/initializers'
4 4
5initDatabase(true) 5initDatabaseModels(true)
6 .then(() => { 6 .then(() => {
7 return sequelizeTypescript.drop() 7 return sequelizeTypescript.drop()
8 }) 8 })
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 @@
1import * as program from 'commander' 1import * as program from 'commander'
2import { initDatabase } from '../server/initializers' 2import { initDatabaseModels } from '../server/initializers'
3import { UserModel } from '../server/models/account/user' 3import { UserModel } from '../server/models/account/user'
4 4
5program 5program
@@ -11,7 +11,7 @@ if (program['user'] === undefined) {
11 process.exit(-1) 11 process.exit(-1)
12} 12}
13 13
14initDatabase(true) 14initDatabaseModels(true)
15 .then(() => { 15 .then(() => {
16 return UserModel.loadByUsername(program['user']) 16 return UserModel.loadByUsername(program['user'])
17 }) 17 })
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 @@
1import { getServerAccount } from '../server/helpers' 1import { getServerAccount } from '../server/helpers'
2import { initDatabase } from '../server/initializers' 2import { initDatabaseModels } from '../server/initializers'
3import { AccountFollowModel } from '../server/models/account/account-follow' 3import { AccountFollowModel } from '../server/models/account/account-follow'
4import { VideoModel } from '../server/models/video/video' 4import { VideoModel } from '../server/models/video/video'
5 5
6initDatabase(true) 6initDatabaseModels(true)
7 .then(() => { 7 .then(() => {
8 return getServerAccount() 8 return getServerAccount()
9 }) 9 })
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) {
40// ----------- Database ----------- 40// ----------- Database -----------
41// Do not use barrels because we don't want to load all modules here (we need to initialize database first) 41// Do not use barrels because we don't want to load all modules here (we need to initialize database first)
42import { logger } from './server/helpers/logger' 42import { logger } from './server/helpers/logger'
43
43// Initialize database and models 44// Initialize database and models
44import { initDatabase } from './server/initializers/database' 45import { initDatabaseModels } from './server/initializers/database'
45initDatabase(false).then(() => onDatabaseInitDone()) 46import { migrate } from './server/initializers/migrator'
47migrate()
48 .then(() => initDatabaseModels(false))
49 .then(() => onDatabaseInitDone())
46 50
47// ----------- PeerTube modules ----------- 51// ----------- PeerTube modules -----------
48import { migrate, installApplication } from './server/initializers' 52import { installApplication } from './server/initializers'
49import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib' 53import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib'
50import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' 54import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers'
51 55
@@ -154,9 +158,8 @@ app.use(function (err, req, res, next) {
154 158
155function onDatabaseInitDone () { 159function onDatabaseInitDone () {
156 const port = CONFIG.LISTEN.PORT 160 const port = CONFIG.LISTEN.PORT
157 // Run the migration scripts if needed 161
158 migrate() 162 installApplication()
159 .then(() => installApplication())
160 .then(() => { 163 .then(() => {
161 // ----------- Make the server listening ----------- 164 // ----------- Make the server listening -----------
162 server.listen(port, () => { 165 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'
8 8
9// --------------------------------------------------------------------------- 9// ---------------------------------------------------------------------------
10 10
11const LAST_MIGRATION_VERSION = 120 11const LAST_MIGRATION_VERSION = 125
12 12
13// --------------------------------------------------------------------------- 13// ---------------------------------------------------------------------------
14 14
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({
50 } 50 }
51}) 51})
52 52
53async function initDatabase (silent: boolean) { 53async function initDatabaseModels (silent: boolean) {
54 sequelizeTypescript.addModels([ 54 sequelizeTypescript.addModels([
55 ApplicationModel, 55 ApplicationModel,
56 AvatarModel, 56 AvatarModel,
@@ -81,6 +81,6 @@ async function initDatabase (silent: boolean) {
81// --------------------------------------------------------------------------- 81// ---------------------------------------------------------------------------
82 82
83export { 83export {
84 initDatabase, 84 initDatabaseModels,
85 sequelizeTypescript 85 sequelizeTypescript
86} 86}
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 @@
1import * as Sequelize from 'sequelize'
2
3async function up (utils: {
4 transaction: Sequelize.Transaction,
5 queryInterface: Sequelize.QueryInterface,
6 sequelize: Sequelize.Sequelize
7}): Promise<void> {
8 await utils.queryInterface.renameTable('Applications', 'application')
9 await utils.queryInterface.renameTable('AccountFollows', 'accountFollow')
10 await utils.queryInterface.renameTable('AccountVideoRates', 'accountVideoRate')
11 await utils.queryInterface.renameTable('Accounts', 'account')
12 await utils.queryInterface.renameTable('Avatars', 'avatar')
13 await utils.queryInterface.renameTable('BlacklistedVideos', 'videoBlacklist')
14 await utils.queryInterface.renameTable('Jobs', 'job')
15 await utils.queryInterface.renameTable('OAuthClients', 'oAuthClient')
16 await utils.queryInterface.renameTable('OAuthTokens', 'oAuthToken')
17 await utils.queryInterface.renameTable('Servers', 'server')
18 await utils.queryInterface.renameTable('Tags', 'tag')
19 await utils.queryInterface.renameTable('Users', 'user')
20 await utils.queryInterface.renameTable('VideoAbuses', 'videoAbuse')
21 await utils.queryInterface.renameTable('VideoChannels', 'videoChannel')
22 await utils.queryInterface.renameTable('VideoChannelShares', 'videoChannelShare')
23 await utils.queryInterface.renameTable('VideoFiles', 'videoFile')
24 await utils.queryInterface.renameTable('VideoShares', 'videoShare')
25 await utils.queryInterface.renameTable('VideoTags', 'videoTag')
26 await utils.queryInterface.renameTable('Videos', 'video')
27}
28
29function down (options) {
30 throw new Error('Not implemented.')
31}
32
33export {
34 up,
35 down
36}
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 @@
1import * as path from 'path' 1import * as path from 'path'
2import { logger, readdirPromise } from '../helpers' 2import { logger, readdirPromise } from '../helpers'
3import { ApplicationModel } from '../models/application/application'
4import { LAST_MIGRATION_VERSION } from './constants' 3import { LAST_MIGRATION_VERSION } from './constants'
5import { sequelizeTypescript } from './database' 4import { sequelizeTypescript } from './database'
6 5
@@ -11,9 +10,23 @@ async function migrate () {
11 // The installer will do that 10 // The installer will do that
12 if (tables.length === 0) return 11 if (tables.length === 0) return
13 12
14 let actualVersion = await ApplicationModel.loadMigrationVersion() 13 let actualVersion: number = null
14
15 // Search in "Applications" or "application" tables
16 try {
17 const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "Applications"')
18 if (rows && rows[ 0 ] && rows[ 0 ].migrationVersion) {
19 actualVersion = rows[ 0 ].migrationVersion
20 }
21 } catch {
22 const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "application"')
23 if (rows && rows[0] && rows[0].migrationVersion) {
24 actualVersion = rows[0].migrationVersion
25 }
26 }
27
15 if (actualVersion === null) { 28 if (actualVersion === null) {
16 await ApplicationModel.create({ migrationVersion: 0 }) 29 await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)')
17 actualVersion = 0 30 actualVersion = 0
18 } 31 }
19 32
@@ -88,6 +101,6 @@ async function executeMigration (actualVersion: number, entity: { version: strin
88 await migrationScript.up(options) 101 await migrationScript.up(options)
89 102
90 // Update the new migration version 103 // Update the new migration version
91 await ApplicationModel.updateMigrationVersion(versionScript, t) 104 await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t })
92 }) 105 })
93} 106}