diff options
-rw-r--r-- | server.js | 30 | ||||
-rw-r--r-- | server/initializers/constants.js | 8 | ||||
-rw-r--r-- | server/initializers/migrations/0005-email-pod.js | 35 | ||||
-rw-r--r-- | server/initializers/migrations/0010-email-user.js | 41 | ||||
-rw-r--r-- | server/initializers/migrator.js | 56 | ||||
-rw-r--r-- | server/models/user.js | 2 |
6 files changed, 130 insertions, 42 deletions
@@ -20,7 +20,7 @@ const constants = require('./server/initializers/constants') | |||
20 | const logger = require('./server/helpers/logger') | 20 | const logger = require('./server/helpers/logger') |
21 | // Initialize database and models | 21 | // Initialize database and models |
22 | const db = require('./server/initializers/database') | 22 | const db = require('./server/initializers/database') |
23 | db.init() | 23 | db.init(onDatabaseInitDone) |
24 | 24 | ||
25 | // ----------- Checker ----------- | 25 | // ----------- Checker ----------- |
26 | const checker = require('./server/initializers/checker') | 26 | const checker = require('./server/initializers/checker') |
@@ -119,25 +119,27 @@ app.use(function (err, req, res, next) { | |||
119 | 119 | ||
120 | // ----------- Run ----------- | 120 | // ----------- Run ----------- |
121 | 121 | ||
122 | const port = constants.CONFIG.LISTEN.PORT | 122 | function onDatabaseInitDone () { |
123 | installer.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 | ||
143 | module.exports = app | 145 | module.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 | ||
8 | const LAST_MIGRATION_VERSION = 10 | ||
9 | |||
10 | // --------------------------------------------------------------------------- | ||
11 | |||
8 | // API version | 12 | // API version |
9 | const API_VERSION = 'v1' | 13 | const API_VERSION = 'v1' |
10 | 14 | ||
@@ -95,10 +99,6 @@ const FRIEND_SCORE = { | |||
95 | 99 | ||
96 | // --------------------------------------------------------------------------- | 100 | // --------------------------------------------------------------------------- |
97 | 101 | ||
98 | const 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 |
103 | const PODS_SCORE = { | 103 | const 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 } | 3 | const waterfall = require('async/waterfall') |
6 | exports.up = function (utils, callback) { | 4 | |
5 | // utils = { transaction, queryInterface, sequelize, Sequelize } | ||
6 | exports.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 | ||
18 | exports.down = function (options, callback) { | 39 | exports.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 | |||
3 | const waterfall = require('async/waterfall') | ||
4 | |||
5 | // utils = { transaction, queryInterface, sequelize, Sequelize } | ||
6 | exports.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 | |||
39 | exports.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 | ||
3 | const waterfall = require('async/waterfall') | ||
3 | const eachSeries = require('async/eachSeries') | 4 | const eachSeries = require('async/eachSeries') |
4 | const fs = require('fs') | 5 | const fs = require('fs') |
5 | const path = require('path') | 6 | const path = require('path') |
@@ -12,30 +13,52 @@ const migrator = { | |||
12 | migrate: migrate | 13 | migrate: migrate |
13 | } | 14 | } |
14 | 15 | ||
15 | function migrate (callback) { | 16 | function 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 |