Server: fix migration scripts

This commit is contained in:
Chocobozzz 2017-02-18 11:56:28 +01:00
parent c1a7ab7f04
commit 5804c0db33
6 changed files with 130 additions and 42 deletions

View File

@ -20,7 +20,7 @@ const constants = require('./server/initializers/constants')
const logger = require('./server/helpers/logger') const logger = require('./server/helpers/logger')
// Initialize database and models // Initialize database and models
const db = require('./server/initializers/database') const db = require('./server/initializers/database')
db.init() db.init(onDatabaseInitDone)
// ----------- Checker ----------- // ----------- Checker -----------
const checker = require('./server/initializers/checker') const checker = require('./server/initializers/checker')
@ -119,14 +119,15 @@ app.use(function (err, req, res, next) {
// ----------- Run ----------- // ----------- Run -----------
const port = constants.CONFIG.LISTEN.PORT function onDatabaseInitDone () {
installer.installApplication(function (err) { const port = constants.CONFIG.LISTEN.PORT
if (err) throw err
// Run the migration scripts if needed // Run the migration scripts if needed
migrator.migrate(function (err) { migrator.migrate(function (err) {
if (err) throw err if (err) throw err
installer.installApplication(function (err) {
if (err) throw err
// ----------- Make the server listening ----------- // ----------- Make the server listening -----------
server.listen(port, function () { server.listen(port, function () {
// Activate the communication with friends // Activate the communication with friends
@ -138,6 +139,7 @@ installer.installApplication(function (err) {
app.emit('ready') app.emit('ready')
}) })
}) })
}) })
}
module.exports = app module.exports = app

View File

@ -5,6 +5,10 @@ const path = require('path')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const LAST_MIGRATION_VERSION = 10
// ---------------------------------------------------------------------------
// API version // API version
const API_VERSION = 'v1' const API_VERSION = 'v1'
@ -95,10 +99,6 @@ const FRIEND_SCORE = {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const LAST_MIGRATION_VERSION = 5
// ---------------------------------------------------------------------------
// Number of points we add/remove from a friend after a successful/bad request // Number of points we add/remove from a friend after a successful/bad request
const PODS_SCORE = { const PODS_SCORE = {
MALUS: -10, MALUS: -10,

View File

@ -1,18 +1,39 @@
/* 'use strict'
This is just an example.
*/
// utils = { transaction, queryInterface } const waterfall = require('async/waterfall')
exports.up = function (utils, callback) {
// utils = { transaction, queryInterface, sequelize, Sequelize }
exports.up = function (utils, finalCallback) {
const q = utils.queryInterface const q = utils.queryInterface
const Sequelize = utils.Sequelize const Sequelize = utils.Sequelize
const data = { const data = {
type: Sequelize.STRING(400), type: Sequelize.STRING(400),
allowNull: false allowNull: false,
defaultValue: ''
} }
q.addColumn('Pods', 'email', data, { transaction: utils.transaction }).asCallback(callback) waterfall([
function addEmailColumn (callback) {
q.addColumn('Pods', 'email', data, { transaction: utils.transaction }).asCallback(function (err) {
return callback(err)
})
},
function updateWithFakeEmails (callback) {
const query = 'UPDATE "Pods" SET "email" = \'dummy@example.com\''
utils.sequelize.query(query, { transaction: utils.transaction }).asCallback(function (err) {
return callback(err)
})
},
function nullOnDefault (callback) {
data.defaultValue = null
q.changeColumn('Pods', 'email', data, { transaction: utils.transaction }).asCallback(callback)
}
], finalCallback)
} }
exports.down = function (options, callback) { exports.down = function (options, callback) {

View File

@ -0,0 +1,41 @@
'use strict'
const waterfall = require('async/waterfall')
// utils = { transaction, queryInterface, sequelize, Sequelize }
exports.up = function (utils, finalCallback) {
const q = utils.queryInterface
const Sequelize = utils.Sequelize
const data = {
type: Sequelize.STRING(400),
allowNull: false,
defaultValue: ''
}
waterfall([
function addEmailColumn (callback) {
q.addColumn('Users', 'email', data, { transaction: utils.transaction }).asCallback(function (err) {
return callback(err)
})
},
function updateWithFakeEmails (callback) {
const query = 'UPDATE "Users" SET "email" = CONCAT("username", \'@example.com\')'
utils.sequelize.query(query, { transaction: utils.transaction }).asCallback(function (err) {
return callback(err)
})
},
function nullOnDefault (callback) {
data.defaultValue = null
q.changeColumn('Users', 'email', data, { transaction: utils.transaction }).asCallback(callback)
}
], finalCallback)
}
exports.down = function (options, callback) {
throw new Error('Not implemented.')
}

View File

@ -1,5 +1,6 @@
'use strict' 'use strict'
const waterfall = require('async/waterfall')
const eachSeries = require('async/eachSeries') const eachSeries = require('async/eachSeries')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
@ -12,17 +13,42 @@ const migrator = {
migrate: migrate migrate: migrate
} }
function migrate (callback) { function migrate (finalCallback) {
db.Application.loadMigrationVersion(function (err, actualVersion) { waterfall([
function checkApplicationTableExists (callback) {
db.sequelize.getQueryInterface().showAllTables().asCallback(function (err, tables) {
if (err) return callback(err) if (err) return callback(err)
// No tables, we don't need to migrate anything
// The installer will do that
if (tables.length === 0) return finalCallback(null)
return callback(null)
})
},
function loadMigrationVersion (callback) {
db.Application.loadMigrationVersion(callback)
},
function abortMigrationIfNotNeeded (actualVersion, callback) {
// No need migrations
if (actualVersion >= constants.LAST_MIGRATION_VERSION) return finalCallback(null)
return callback(null, actualVersion)
},
function getMigrations (actualVersion, callback) {
// If there are a new migration scripts // If there are a new migration scripts
if (actualVersion < constants.LAST_MIGRATION_VERSION) {
logger.info('Begin migrations.') logger.info('Begin migrations.')
getMigrationScripts(function (err, migrationScripts) { getMigrationScripts(function (err, migrationScripts) {
if (err) return callback(err) return callback(err, actualVersion, migrationScripts)
})
},
function doMigrations (actualVersion, migrationScripts, callback) {
eachSeries(migrationScripts, function (entity, callbackEach) { eachSeries(migrationScripts, function (entity, callbackEach) {
executeMigration(actualVersion, entity, callbackEach) executeMigration(actualVersion, entity, callbackEach)
}, function (err) { }, function (err) {
@ -31,11 +57,8 @@ function migrate (callback) {
logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION) logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
return callback(null) return callback(null)
}) })
})
} else {
return callback(null)
} }
}) ], finalCallback)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -81,6 +104,7 @@ function executeMigration (actualVersion, entity, callback) {
const options = { const options = {
transaction: t, transaction: t,
queryInterface: db.sequelize.getQueryInterface(), queryInterface: db.sequelize.getQueryInterface(),
sequelize: db.sequelize,
Sequelize: db.Sequelize Sequelize: db.Sequelize
} }
migrationScript.up(options, function (err) { migrationScript.up(options, function (err) {

View File

@ -33,7 +33,7 @@ module.exports = function (sequelize, DataTypes) {
} }
}, },
email: { email: {
type: DataTypes.STRING, type: DataTypes.STRING(400),
allowNull: false, allowNull: false,
validate: { validate: {
isEmail: true isEmail: true