aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrator.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-25 09:44:57 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-25 09:44:57 +0100
commitb769007f733769d3afe2d29a3eb23e2e7693f301 (patch)
tree3816d542e907f298d08338129e0f6b9ca0df8fbb /server/initializers/migrator.js
parentdd6019932efd6ae3b790bf024bc0cd74162e4517 (diff)
downloadPeerTube-b769007f733769d3afe2d29a3eb23e2e7693f301.tar.gz
PeerTube-b769007f733769d3afe2d29a3eb23e2e7693f301.tar.zst
PeerTube-b769007f733769d3afe2d29a3eb23e2e7693f301.zip
Update migrations code
Diffstat (limited to 'server/initializers/migrator.js')
-rw-r--r--server/initializers/migrator.js88
1 files changed, 66 insertions, 22 deletions
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js
index 9e5350e60..eaecb4936 100644
--- a/server/initializers/migrator.js
+++ b/server/initializers/migrator.js
@@ -1,6 +1,7 @@
1'use strict' 1'use strict'
2 2
3const eachSeries = require('async/eachSeries') 3const eachSeries = require('async/eachSeries')
4const fs = require('fs')
4const path = require('path') 5const path = require('path')
5 6
6const constants = require('./constants') 7const constants = require('./constants')
@@ -12,35 +13,24 @@ const migrator = {
12} 13}
13 14
14function migrate (callback) { 15function migrate (callback) {
15 db.Application.loadSqlSchemaVersion(function (err, actualVersion) { 16 db.Application.loadMigrationVersion(function (err, actualVersion) {
16 if (err) return callback(err) 17 if (err) return callback(err)
17 18
18 // If there are a new mongo schemas 19 // If there are a new migration scripts
19 if (!actualVersion || actualVersion < constants.LAST_SQL_SCHEMA_VERSION) { 20 if (actualVersion < constants.LAST_MIGRATION_VERSION) {
20 logger.info('Begin migrations.') 21 logger.info('Begin migrations.')
21 22
22 eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { 23 getMigrationScripts(function (err, migrationScripts) {
23 const versionScript = entity.version 24 if (err) return callback(err)
24
25 // Do not execute old migration scripts
26 if (versionScript <= actualVersion) return callbackEach(null)
27
28 // Load the migration module and run it
29 const migrationScriptName = entity.script
30 logger.info('Executing %s migration script.', migrationScriptName)
31 25
32 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) 26 eachSeries(migrationScripts, function (entity, callbackEach) {
33 migrationScript.up(function (err) { 27 executeMigration(actualVersion, entity, callbackEach)
34 if (err) return callbackEach(err) 28 }, function (err) {
29 if (err) return callback(err)
35 30
36 // Update the new mongo version schema 31 logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
37 db.Application.updateSqlSchemaVersion(versionScript, callbackEach) 32 return callback(null)
38 }) 33 })
39 }, function (err) {
40 if (err) return callback(err)
41
42 logger.info('Migrations finished. New SQL version schema: %s', constants.LAST_SQL_SCHEMA_VERSION)
43 return callback(null)
44 }) 34 })
45 } else { 35 } else {
46 return callback(null) 36 return callback(null)
@@ -52,3 +42,57 @@ function migrate (callback) {
52 42
53module.exports = migrator 43module.exports = migrator
54 44
45// ---------------------------------------------------------------------------
46
47function getMigrationScripts (callback) {
48 fs.readdir(path.join(__dirname, 'migrations'), function (err, files) {
49 if (err) return callback(err)
50
51 const filesToMigrate = []
52
53 files.forEach(function (file) {
54 // Filename is something like 'version-blabla.js'
55 const version = file.split('-')[0]
56 filesToMigrate.push({
57 version,
58 script: file
59 })
60 })
61
62 return callback(err, filesToMigrate)
63 })
64}
65
66function executeMigration (actualVersion, entity, callback) {
67 const versionScript = entity.version
68
69 // Do not execute old migration scripts
70 if (versionScript <= actualVersion) return callback(null)
71
72 // Load the migration module and run it
73 const migrationScriptName = entity.script
74 logger.info('Executing %s migration script.', migrationScriptName)
75
76 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
77
78 db.sequelize.transaction().asCallback(function (err, t) {
79 if (err) return callback(err)
80
81 migrationScript.up({ transaction: t }, function (err) {
82 if (err) {
83 t.rollback()
84 return callback(err)
85 }
86
87 // Update the new migration version
88 db.Application.updateMigrationVersion(versionScript, t, function (err) {
89 if (err) {
90 t.rollback()
91 return callback(err)
92 }
93
94 t.commit()
95 })
96 })
97 })
98}