aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server.js30
-rw-r--r--server/initializers/constants.js8
-rw-r--r--server/initializers/migrations/0005-email-pod.js35
-rw-r--r--server/initializers/migrations/0010-email-user.js41
-rw-r--r--server/initializers/migrator.js56
-rw-r--r--server/models/user.js2
6 files changed, 130 insertions, 42 deletions
diff --git a/server.js b/server.js
index 4a0de72bb..33d399786 100644
--- a/server.js
+++ b/server.js
@@ -20,7 +20,7 @@ const constants = require('./server/initializers/constants')
20const logger = require('./server/helpers/logger') 20const logger = require('./server/helpers/logger')
21// Initialize database and models 21// Initialize database and models
22const db = require('./server/initializers/database') 22const db = require('./server/initializers/database')
23db.init() 23db.init(onDatabaseInitDone)
24 24
25// ----------- Checker ----------- 25// ----------- Checker -----------
26const checker = require('./server/initializers/checker') 26const checker = require('./server/initializers/checker')
@@ -119,25 +119,27 @@ app.use(function (err, req, res, next) {
119 119
120// ----------- Run ----------- 120// ----------- Run -----------
121 121
122const port = constants.CONFIG.LISTEN.PORT 122function onDatabaseInitDone () {
123installer.installApplication(function (err) { 123 const port = constants.CONFIG.LISTEN.PORT
124 if (err) throw err 124 // Run the migration scripts if needed
125
126 // Run the migration scripts if needed
127 migrator.migrate(function (err) { 125 migrator.migrate(function (err) {
128 if (err) throw err 126 if (err) throw err
129 127
130 // ----------- Make the server listening ----------- 128 installer.installApplication(function (err) {
131 server.listen(port, function () { 129 if (err) throw err
132 // Activate the communication with friends 130
133 friends.activate() 131 // ----------- Make the server listening -----------
132 server.listen(port, function () {
133 // Activate the communication with friends
134 friends.activate()
134 135
135 logger.info('Server listening on port %d', port) 136 logger.info('Server listening on port %d', port)
136 logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL) 137 logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL)
137 138
138 app.emit('ready') 139 app.emit('ready')
140 })
139 }) 141 })
140 }) 142 })
141}) 143}
142 144
143module.exports = app 145module.exports = app
diff --git a/server/initializers/constants.js b/server/initializers/constants.js
index eb0509efe..821580893 100644
--- a/server/initializers/constants.js
+++ b/server/initializers/constants.js
@@ -5,6 +5,10 @@ const path = require('path')
5 5
6// --------------------------------------------------------------------------- 6// ---------------------------------------------------------------------------
7 7
8const LAST_MIGRATION_VERSION = 10
9
10// ---------------------------------------------------------------------------
11
8// API version 12// API version
9const API_VERSION = 'v1' 13const API_VERSION = 'v1'
10 14
@@ -95,10 +99,6 @@ const FRIEND_SCORE = {
95 99
96// --------------------------------------------------------------------------- 100// ---------------------------------------------------------------------------
97 101
98const LAST_MIGRATION_VERSION = 5
99
100// ---------------------------------------------------------------------------
101
102// Number of points we add/remove from a friend after a successful/bad request 102// Number of points we add/remove from a friend after a successful/bad request
103const PODS_SCORE = { 103const PODS_SCORE = {
104 MALUS: -10, 104 MALUS: -10,
diff --git a/server/initializers/migrations/0005-email-pod.js b/server/initializers/migrations/0005-email-pod.js
index d90b674a1..9bbb354bf 100644
--- a/server/initializers/migrations/0005-email-pod.js
+++ b/server/initializers/migrations/0005-email-pod.js
@@ -1,18 +1,39 @@
1/* 1'use strict'
2 This is just an example.
3*/
4 2
5// utils = { transaction, queryInterface } 3const waterfall = require('async/waterfall')
6exports.up = function (utils, callback) { 4
5// utils = { transaction, queryInterface, sequelize, Sequelize }
6exports.up = function (utils, finalCallback) {
7 const q = utils.queryInterface 7 const q = utils.queryInterface
8 const Sequelize = utils.Sequelize 8 const Sequelize = utils.Sequelize
9 9
10 const data = { 10 const data = {
11 type: Sequelize.STRING(400), 11 type: Sequelize.STRING(400),
12 allowNull: false 12 allowNull: false,
13 defaultValue: ''
13 } 14 }
14 15
15 q.addColumn('Pods', 'email', data, { transaction: utils.transaction }).asCallback(callback) 16 waterfall([
17
18 function addEmailColumn (callback) {
19 q.addColumn('Pods', 'email', data, { transaction: utils.transaction }).asCallback(function (err) {
20 return callback(err)
21 })
22 },
23
24 function updateWithFakeEmails (callback) {
25 const query = 'UPDATE "Pods" SET "email" = \'dummy@example.com\''
26 utils.sequelize.query(query, { transaction: utils.transaction }).asCallback(function (err) {
27 return callback(err)
28 })
29 },
30
31 function nullOnDefault (callback) {
32 data.defaultValue = null
33
34 q.changeColumn('Pods', 'email', data, { transaction: utils.transaction }).asCallback(callback)
35 }
36 ], finalCallback)
16} 37}
17 38
18exports.down = function (options, callback) { 39exports.down = function (options, callback) {
diff --git a/server/initializers/migrations/0010-email-user.js b/server/initializers/migrations/0010-email-user.js
new file mode 100644
index 000000000..1ab27133a
--- /dev/null
+++ b/server/initializers/migrations/0010-email-user.js
@@ -0,0 +1,41 @@
1'use strict'
2
3const waterfall = require('async/waterfall')
4
5// utils = { transaction, queryInterface, sequelize, Sequelize }
6exports.up = function (utils, finalCallback) {
7 const q = utils.queryInterface
8 const Sequelize = utils.Sequelize
9
10 const data = {
11 type: Sequelize.STRING(400),
12 allowNull: false,
13 defaultValue: ''
14 }
15
16 waterfall([
17
18 function addEmailColumn (callback) {
19 q.addColumn('Users', 'email', data, { transaction: utils.transaction }).asCallback(function (err) {
20 return callback(err)
21 })
22 },
23
24 function updateWithFakeEmails (callback) {
25 const query = 'UPDATE "Users" SET "email" = CONCAT("username", \'@example.com\')'
26 utils.sequelize.query(query, { transaction: utils.transaction }).asCallback(function (err) {
27 return callback(err)
28 })
29 },
30
31 function nullOnDefault (callback) {
32 data.defaultValue = null
33
34 q.changeColumn('Users', 'email', data, { transaction: utils.transaction }).asCallback(callback)
35 }
36 ], finalCallback)
37}
38
39exports.down = function (options, callback) {
40 throw new Error('Not implemented.')
41}
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) {
diff --git a/server/models/user.js b/server/models/user.js
index 35a98dd6b..24e710fa7 100644
--- a/server/models/user.js
+++ b/server/models/user.js
@@ -33,7 +33,7 @@ module.exports = function (sequelize, DataTypes) {
33 } 33 }
34 }, 34 },
35 email: { 35 email: {
36 type: DataTypes.STRING, 36 type: DataTypes.STRING(400),
37 allowNull: false, 37 allowNull: false,
38 validate: { 38 validate: {
39 isEmail: true 39 isEmail: true