3 var BPromise = require('bluebird');
4 var util = require('util');
6 var logger = require('../config/logger')('migration');
8 var Sequence = function() {};
10 Sequence.prototype.enqueue = function(fn) {
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
14 var Migration = function(queryInterface) {
15 this.queryInterface = queryInterface;
16 this.sequence = new Sequence();
19 Migration.prototype.changeColumn = function(table, column, type) {
21 this.sequence.enqueue(function() {
22 return _this.queryInterface
23 .changeColumn(table, column, type)
25 logger.info('Changed column %s in table %s', column, table);
27 .catch(function(err) {
28 logger.info(JSON.stringify(err));
33 Migration.prototype.addColumn = function(table, column, type) {
35 this.sequence.enqueue(function() {
36 return _this.queryInterface
37 .addColumn(table, column, type)
39 logger.info('Added column %s to %s', column, table);
41 .catch(function(err) {
42 logger.info(JSON.stringify(err));
47 Migration.prototype.dropTable = function(table) {
49 this.sequence.enqueue(function() {
50 return _this.queryInterface
55 logger.info('table dropped %s', table);
57 .catch(function(err) {
58 logger.info(JSON.stringify(err));
63 Migration.prototype.addIndex = function(table, column, indexName) {
65 this.sequence.enqueue(function() {
66 return _this.queryInterface
67 .addIndex(table, column, {
71 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
73 .catch(function(err) {
74 logger.info(JSON.stringify(err));
79 Migration.prototype.removeIndex = function(table, indexName) {
81 this.sequence.enqueue(function() {
82 return _this.queryInterface
83 .removeIndex(table, indexName)
85 logger.info('removeIndex %s %s', table, indexName);
87 .catch(function(err) {
88 logger.info(JSON.stringify(err));
93 Migration.prototype.query = function(sql) {
95 this.sequence.enqueue(function() {
96 return _this.queryInterface.sequelize
99 logger.info('query %s', sql);
101 .catch(function(err) {
102 logger.info(JSON.stringify(err));
107 Migration.prototype.removeColumn = function(table, column) {
109 this.sequence.enqueue(function() {
110 return _this.queryInterface
111 .removeColumn(table, column)
113 logger.info('Removed column %s from %s', column, table);
115 .catch(function(err) {
126 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
128 this.sequence.enqueue(function() {
129 return _this.queryInterface
130 .renameColumn(table, oldColumn, newColumn)
133 'Renamed column from %s to %s on %s',
139 .catch(function(err) {
150 Migration.prototype.final = function(resolve) {
151 this.sequence.enqueue(function() {
157 up: function(queryInterface, Sequelize) {
158 return new BPromise(function(resolve) {
159 var migration = new Migration(queryInterface);
161 // START mail_servers_out
163 'UPDATE `mail_servers_out` T1' +
164 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
165 " SET T1.host = 'smtp.office365.com', T1.port = 587, T1.secure = 0, T1.service = NULL" +
166 " WHERE T2.provider = 'outlook365'"
170 'UPDATE `mail_servers_out` T1' +
171 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
172 " SET T1.host = 'smtp-mail.outlook.com', T1.port = 587, T1.secure = 0, T1.service = NULL" +
173 " WHERE T2.provider = 'hotmail'"
177 'UPDATE `mail_servers_out` T1' +
178 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
179 " SET T1.host = 'smtp.mail.yahoo.com', T1.port = 587, T1.secure = 0, T1.service = NULL" +
180 " WHERE T2.provider = 'yahoo'"
184 'UPDATE `mail_servers_out` T1' +
185 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
186 " SET T1.host = 'smtp.gmail.com', T1.port = 587, T1.secure = 0, T1.service = NULL" +
187 " WHERE T2.provider = 'gmail'"
189 // END mail_servers_out
191 // START mail_servers_in
193 'UPDATE `mail_servers_in` T1' +
194 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
195 " SET T1.host = '127.0.0.1', T1.port = 993, T1.user = 'username', T1.password = 'password'" +
196 " WHERE T2.service = 1"
200 'UPDATE `mail_servers_in` T1' +
201 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
202 " SET T1.host = 'outlook.office365.com', T1.port = 993, T1.service = NULL" +
203 " WHERE T2.provider = 'outlook365' AND T2.service = 0"
207 'UPDATE `mail_servers_in` T1' +
208 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
209 " SET T1.host = 'imap-mail.outlook.com', T1.port = 993, T1.service = NULL" +
210 " WHERE T2.provider = 'hotmail' AND T2.service = 0"
214 'UPDATE `mail_servers_in` T1' +
215 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
216 " SET T1.host = 'imap.mail.yahoo.com', T1.port = 993, T1.service = NULL" +
217 " WHERE T2.provider = 'yahoo' AND T2.service = 0"
221 'UPDATE `mail_servers_in` T1' +
222 ' JOIN `mail_accounts` T2 ON (T1.MailAccountId = T2.id)' +
223 " SET T1.host = 'imap.gmail.com', T1.port = 993, T1.service = NULL" +
224 " WHERE T2.provider = 'gmail' AND T2.service = 0"
226 // END mail_servers_in
229 migration.final(resolve);
234 down: function(queryInterface, Sequelize) {
235 // var migration = new Migration(queryInterface);