aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrator.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
commit99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch)
treec9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/initializers/migrator.js
parentfcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff)
parent91cc839af88730ba55f84997c56b85ea100070a7 (diff)
downloadPeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip
Merge branch 'postgresql'
Diffstat (limited to 'server/initializers/migrator.js')
-rw-r--r--server/initializers/migrator.js92
1 files changed, 67 insertions, 25 deletions
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js
index 6b31d994f..e5288b615 100644
--- a/server/initializers/migrator.js
+++ b/server/initializers/migrator.js
@@ -1,48 +1,36 @@
1'use strict' 1'use strict'
2 2
3const eachSeries = require('async/eachSeries') 3const eachSeries = require('async/eachSeries')
4const mongoose = require('mongoose') 4const fs = require('fs')
5const path = require('path') 5const path = require('path')
6 6
7const constants = require('./constants') 7const constants = require('./constants')
8const db = require('./database')
8const logger = require('../helpers/logger') 9const logger = require('../helpers/logger')
9 10
10const Application = mongoose.model('Application')
11
12const migrator = { 11const migrator = {
13 migrate: migrate 12 migrate: migrate
14} 13}
15 14
16function migrate (callback) { 15function migrate (callback) {
17 Application.loadMongoSchemaVersion(function (err, actualVersion) { 16 db.Application.loadMigrationVersion(function (err, actualVersion) {
18 if (err) return callback(err) 17 if (err) return callback(err)
19 18
20 // If there are a new mongo schemas 19 // If there are a new migration scripts
21 if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) { 20 if (actualVersion < constants.LAST_MIGRATION_VERSION) {
22 logger.info('Begin migrations.') 21 logger.info('Begin migrations.')
23 22
24 eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { 23 getMigrationScripts(function (err, migrationScripts) {
25 const versionScript = entity.version 24 if (err) return callback(err)
26
27 // Do not execute old migration scripts
28 if (versionScript <= actualVersion) return callbackEach(null)
29
30 // Load the migration module and run it
31 const migrationScriptName = entity.script
32 logger.info('Executing %s migration script.', migrationScriptName)
33 25
34 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) 26 eachSeries(migrationScripts, function (entity, callbackEach) {
35 migrationScript.up(function (err) { 27 executeMigration(actualVersion, entity, callbackEach)
36 if (err) return callbackEach(err) 28 }, function (err) {
29 if (err) return callback(err)
37 30
38 // Update the new mongo version schema 31 logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
39 Application.updateMongoSchemaVersion(versionScript, callbackEach) 32 return callback(null)
40 }) 33 })
41 }, function (err) {
42 if (err) return callback(err)
43
44 logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION)
45 return callback(null)
46 }) 34 })
47 } else { 35 } else {
48 return callback(null) 36 return callback(null)
@@ -54,3 +42,57 @@ function migrate (callback) {
54 42
55module.exports = migrator 43module.exports = migrator
56 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().asCallback(callback)
95 })
96 })
97 })
98}