diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-13 17:46:23 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 17:46:23 +0100 |
commit | 91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d (patch) | |
tree | 1f227acea3e9684928dedbc070cc5fa6b3af42ee | |
parent | 65b3ed25fc33c63ab2c56372c1a010541f8fddc2 (diff) | |
download | PeerTube-91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d.tar.gz PeerTube-91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d.tar.zst PeerTube-91fea9fc48a4ce53dd69e0e20f5804ad95a6c27d.zip |
Add migration
-rw-r--r-- | scripts/danger/clean/cleaner.ts | 4 | ||||
-rwxr-xr-x | scripts/reset-password.ts | 4 | ||||
-rwxr-xr-x | scripts/update-host.ts | 4 | ||||
-rw-r--r-- | server.ts | 15 | ||||
-rw-r--r-- | server/initializers/constants.ts | 2 | ||||
-rw-r--r-- | server/initializers/database.ts | 4 | ||||
-rw-r--r-- | server/initializers/migrations/0125-table-lowercase.ts | 36 | ||||
-rw-r--r-- | server/initializers/migrator.ts | 21 |
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 @@ | |||
1 | import * as Promise from 'bluebird' | 1 | import * as Promise from 'bluebird' |
2 | import * as rimraf from 'rimraf' | 2 | import * as rimraf from 'rimraf' |
3 | import { CONFIG, initDatabase, sequelizeTypescript } from '../../../server/initializers' | 3 | import { CONFIG, initDatabaseModels, sequelizeTypescript } from '../../../server/initializers' |
4 | 4 | ||
5 | initDatabase(true) | 5 | initDatabaseModels(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 @@ | |||
1 | import * as program from 'commander' | 1 | import * as program from 'commander' |
2 | import { initDatabase } from '../server/initializers' | 2 | import { initDatabaseModels } from '../server/initializers' |
3 | import { UserModel } from '../server/models/account/user' | 3 | import { UserModel } from '../server/models/account/user' |
4 | 4 | ||
5 | program | 5 | program |
@@ -11,7 +11,7 @@ if (program['user'] === undefined) { | |||
11 | process.exit(-1) | 11 | process.exit(-1) |
12 | } | 12 | } |
13 | 13 | ||
14 | initDatabase(true) | 14 | initDatabaseModels(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 @@ | |||
1 | import { getServerAccount } from '../server/helpers' | 1 | import { getServerAccount } from '../server/helpers' |
2 | import { initDatabase } from '../server/initializers' | 2 | import { initDatabaseModels } from '../server/initializers' |
3 | import { AccountFollowModel } from '../server/models/account/account-follow' | 3 | import { AccountFollowModel } from '../server/models/account/account-follow' |
4 | import { VideoModel } from '../server/models/video/video' | 4 | import { VideoModel } from '../server/models/video/video' |
5 | 5 | ||
6 | initDatabase(true) | 6 | initDatabaseModels(true) |
7 | .then(() => { | 7 | .then(() => { |
8 | return getServerAccount() | 8 | return getServerAccount() |
9 | }) | 9 | }) |
@@ -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) |
42 | import { logger } from './server/helpers/logger' | 42 | import { logger } from './server/helpers/logger' |
43 | |||
43 | // Initialize database and models | 44 | // Initialize database and models |
44 | import { initDatabase } from './server/initializers/database' | 45 | import { initDatabaseModels } from './server/initializers/database' |
45 | initDatabase(false).then(() => onDatabaseInitDone()) | 46 | import { migrate } from './server/initializers/migrator' |
47 | migrate() | ||
48 | .then(() => initDatabaseModels(false)) | ||
49 | .then(() => onDatabaseInitDone()) | ||
46 | 50 | ||
47 | // ----------- PeerTube modules ----------- | 51 | // ----------- PeerTube modules ----------- |
48 | import { migrate, installApplication } from './server/initializers' | 52 | import { installApplication } from './server/initializers' |
49 | import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib' | 53 | import { activitypubHttpJobScheduler, transcodingJobScheduler, VideosPreviewCache } from './server/lib' |
50 | import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' | 54 | import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' |
51 | 55 | ||
@@ -154,9 +158,8 @@ app.use(function (err, req, res, next) { | |||
154 | 158 | ||
155 | function onDatabaseInitDone () { | 159 | function 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 | ||
11 | const LAST_MIGRATION_VERSION = 120 | 11 | const 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 | ||
53 | async function initDatabase (silent: boolean) { | 53 | async 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 | ||
83 | export { | 83 | export { |
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | async 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 | |||
29 | function down (options) { | ||
30 | throw new Error('Not implemented.') | ||
31 | } | ||
32 | |||
33 | export { | ||
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 @@ | |||
1 | import * as path from 'path' | 1 | import * as path from 'path' |
2 | import { logger, readdirPromise } from '../helpers' | 2 | import { logger, readdirPromise } from '../helpers' |
3 | import { ApplicationModel } from '../models/application/application' | ||
4 | import { LAST_MIGRATION_VERSION } from './constants' | 3 | import { LAST_MIGRATION_VERSION } from './constants' |
5 | import { sequelizeTypescript } from './database' | 4 | import { 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 | } |