aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrator.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-02-18 11:56:28 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-02-18 12:13:33 +0100
commit5804c0db337ecf492fc61b98a3de7b730a0d8ce3 (patch)
tree83c0e8d8f10ec0f9ca269624feaba48970a7a0e3 /server/initializers/migrator.js
parentc1a7ab7f04fdb1601cf1e41c4e372dbd3c81f3de (diff)
downloadPeerTube-5804c0db337ecf492fc61b98a3de7b730a0d8ce3.tar.gz
PeerTube-5804c0db337ecf492fc61b98a3de7b730a0d8ce3.tar.zst
PeerTube-5804c0db337ecf492fc61b98a3de7b730a0d8ce3.zip
Server: fix migration scripts
Diffstat (limited to 'server/initializers/migrator.js')
-rw-r--r--server/initializers/migrator.js56
1 files changed, 40 insertions, 16 deletions
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js
index 233ee2bdd..8c67903ad 100644
--- a/server/initializers/migrator.js
+++ b/server/initializers/migrator.js
@@ -1,5 +1,6 @@
1'use strict' 1'use strict'
2 2
3const waterfall = require('async/waterfall')
3const eachSeries = require('async/eachSeries') 4const eachSeries = require('async/eachSeries')
4const fs = require('fs') 5const fs = require('fs')
5const path = require('path') 6const path = require('path')
@@ -12,30 +13,52 @@ const migrator = {
12 migrate: migrate 13 migrate: migrate
13} 14}
14 15
15function migrate (callback) { 16function migrate (finalCallback) {
16 db.Application.loadMigrationVersion(function (err, actualVersion) { 17 waterfall([
17 if (err) return callback(err) 18
19 function checkApplicationTableExists (callback) {
20 db.sequelize.getQueryInterface().showAllTables().asCallback(function (err, tables) {
21 if (err) return callback(err)
22
23 // No tables, we don't need to migrate anything
24 // The installer will do that
25 if (tables.length === 0) return finalCallback(null)
26
27 return callback(null)
28 })
29 },
30
31 function loadMigrationVersion (callback) {
32 db.Application.loadMigrationVersion(callback)
33 },
18 34
19 // If there are a new migration scripts 35 function abortMigrationIfNotNeeded (actualVersion, callback) {
20 if (actualVersion < constants.LAST_MIGRATION_VERSION) { 36 // No need migrations
37 if (actualVersion >= constants.LAST_MIGRATION_VERSION) return finalCallback(null)
38
39 return callback(null, actualVersion)
40 },
41
42 function getMigrations (actualVersion, callback) {
43 // If there are a new migration scripts
21 logger.info('Begin migrations.') 44 logger.info('Begin migrations.')
22 45
23 getMigrationScripts(function (err, migrationScripts) { 46 getMigrationScripts(function (err, migrationScripts) {
24 if (err) return callback(err) 47 return callback(err, actualVersion, migrationScripts)
48 })
49 },
25 50
26 eachSeries(migrationScripts, function (entity, callbackEach) { 51 function doMigrations (actualVersion, migrationScripts, callback) {
27 executeMigration(actualVersion, entity, callbackEach) 52 eachSeries(migrationScripts, function (entity, callbackEach) {
28 }, function (err) { 53 executeMigration(actualVersion, entity, callbackEach)
29 if (err) return callback(err) 54 }, function (err) {
55 if (err) return callback(err)
30 56
31 logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION) 57 logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
32 return callback(null) 58 return callback(null)
33 })
34 }) 59 })
35 } else {
36 return callback(null)
37 } 60 }
38 }) 61 ], finalCallback)
39} 62}
40 63
41// --------------------------------------------------------------------------- 64// ---------------------------------------------------------------------------
@@ -81,6 +104,7 @@ function executeMigration (actualVersion, entity, callback) {
81 const options = { 104 const options = {
82 transaction: t, 105 transaction: t,
83 queryInterface: db.sequelize.getQueryInterface(), 106 queryInterface: db.sequelize.getQueryInterface(),
107 sequelize: db.sequelize,
84 Sequelize: db.Sequelize 108 Sequelize: db.Sequelize
85 } 109 }
86 migrationScript.up(options, function (err) { 110 migrationScript.up(options, function (err) {