const logger = require('./server/helpers/logger')
// Initialize database and models
const db = require('./server/initializers/database')
-db.init()
+db.init(onDatabaseInitDone)
// ----------- Checker -----------
const checker = require('./server/initializers/checker')
// ----------- Run -----------
-const port = constants.CONFIG.LISTEN.PORT
-installer.installApplication(function (err) {
- if (err) throw err
-
- // Run the migration scripts if needed
+function onDatabaseInitDone () {
+ const port = constants.CONFIG.LISTEN.PORT
+ // Run the migration scripts if needed
migrator.migrate(function (err) {
if (err) throw err
- // ----------- Make the server listening -----------
- server.listen(port, function () {
- // Activate the communication with friends
- friends.activate()
+ installer.installApplication(function (err) {
+ if (err) throw err
+
+ // ----------- Make the server listening -----------
+ server.listen(port, function () {
+ // Activate the communication with friends
+ friends.activate()
- logger.info('Server listening on port %d', port)
- logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL)
+ logger.info('Server listening on port %d', port)
+ logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL)
- app.emit('ready')
+ app.emit('ready')
+ })
})
})
-})
+}
module.exports = app
// ---------------------------------------------------------------------------
+const LAST_MIGRATION_VERSION = 10
+
+// ---------------------------------------------------------------------------
+
// API version
const API_VERSION = 'v1'
// ---------------------------------------------------------------------------
-const LAST_MIGRATION_VERSION = 5
-
-// ---------------------------------------------------------------------------
-
// Number of points we add/remove from a friend after a successful/bad request
const PODS_SCORE = {
MALUS: -10,
-/*
- This is just an example.
-*/
+'use strict'
-// utils = { transaction, queryInterface }
-exports.up = function (utils, callback) {
+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
+ 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) {
--- /dev/null
+'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.')
+}
'use strict'
+const waterfall = require('async/waterfall')
const eachSeries = require('async/eachSeries')
const fs = require('fs')
const path = require('path')
migrate: migrate
}
-function migrate (callback) {
- db.Application.loadMigrationVersion(function (err, actualVersion) {
- if (err) return callback(err)
+function migrate (finalCallback) {
+ waterfall([
+
+ function checkApplicationTableExists (callback) {
+ db.sequelize.getQueryInterface().showAllTables().asCallback(function (err, tables) {
+ 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)
+ },
- // If there are a new migration scripts
- if (actualVersion < constants.LAST_MIGRATION_VERSION) {
+ 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
logger.info('Begin migrations.')
getMigrationScripts(function (err, migrationScripts) {
- if (err) return callback(err)
+ return callback(err, actualVersion, migrationScripts)
+ })
+ },
- eachSeries(migrationScripts, function (entity, callbackEach) {
- executeMigration(actualVersion, entity, callbackEach)
- }, function (err) {
- if (err) return callback(err)
+ function doMigrations (actualVersion, migrationScripts, callback) {
+ eachSeries(migrationScripts, function (entity, callbackEach) {
+ executeMigration(actualVersion, entity, callbackEach)
+ }, function (err) {
+ if (err) return callback(err)
- logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
- return callback(null)
- })
+ logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
+ return callback(null)
})
- } else {
- return callback(null)
}
- })
+ ], finalCallback)
}
// ---------------------------------------------------------------------------
const options = {
transaction: t,
queryInterface: db.sequelize.getQueryInterface(),
+ sequelize: db.sequelize,
Sequelize: db.Sequelize
}
migrationScript.up(options, function (err) {
}
},
email: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(400),
allowNull: false,
validate: {
isEmail: true