Built motion from commit f7863d46.|2.5.41
[motion2.git] / server / migrations / 2.5.41.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var util = require('util');
5
6 var logger = require('../config/logger')('migration');
7
8 var Sequence = function() {};
9
10 Sequence.prototype.enqueue = function(fn) {
11   this.tail = this.tail ? this.tail.finally(fn) : fn();
12 };
13
14 var Migration = function(queryInterface) {
15   this.queryInterface = queryInterface;
16   this.sequence = new Sequence();
17 };
18
19 Migration.prototype.changeColumn = function(table, column, type) {
20   var _this = this;
21   this.sequence.enqueue(function() {
22     return _this.queryInterface
23       .changeColumn(table, column, type)
24       .then(function() {
25         logger.info('Changed column %s in table %s', column, table);
26       })
27       .catch(function(err) {
28         logger.info(JSON.stringify(err));
29       });
30   });
31 };
32
33 Migration.prototype.addColumn = function(table, column, type) {
34   var _this = this;
35   this.sequence.enqueue(function() {
36     return _this.queryInterface
37       .addColumn(table, column, type)
38       .then(function() {
39         logger.info('Added column %s to %s', column, table);
40       })
41       .catch(function(err) {
42         logger.info(JSON.stringify(err));
43       });
44   });
45 };
46
47 Migration.prototype.dropTable = function(table) {
48   var _this = this;
49   this.sequence.enqueue(function() {
50     return _this.queryInterface
51       .dropTable(table, {
52         force: true
53       })
54       .then(function() {
55         logger.info('table dropped %s', table);
56       })
57       .catch(function(err) {
58         logger.info(JSON.stringify(err));
59       });
60   });
61 };
62
63 Migration.prototype.addIndex = function(table, column, indexName) {
64   var _this = this;
65   this.sequence.enqueue(function() {
66     return _this.queryInterface
67       .addIndex(table, column, {
68         indexName: indexName
69       })
70       .then(function() {
71         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
72       })
73       .catch(function(err) {
74         logger.info(JSON.stringify(err));
75       });
76   });
77 };
78
79 Migration.prototype.removeIndex = function(table, indexName) {
80   var _this = this;
81   this.sequence.enqueue(function() {
82     return _this.queryInterface
83       .removeIndex(table, indexName)
84       .then(function() {
85         logger.info('removeIndex %s %s', table, indexName);
86       })
87       .catch(function(err) {
88         logger.info(JSON.stringify(err));
89       });
90   });
91 };
92
93 Migration.prototype.query = function(sql) {
94   var _this = this;
95   this.sequence.enqueue(function() {
96     return _this.queryInterface.sequelize
97       .query(sql)
98       .then(function() {
99         logger.info('query %s', sql);
100       })
101       .catch(function(err) {
102         logger.info(JSON.stringify(err));
103       });
104   });
105 };
106
107 Migration.prototype.removeColumn = function(table, column) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return _this.queryInterface
111       .removeColumn(table, column)
112       .then(function() {
113         logger.info('Removed column %s from %s', column, table);
114       })
115       .catch(function(err) {
116         logger.info(
117           util.inspect(err, {
118             showHidden: false,
119             depth: null
120           })
121         );
122       });
123   });
124 };
125
126 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
127   var _this = this;
128   this.sequence.enqueue(function() {
129     return _this.queryInterface
130       .renameColumn(table, oldColumn, newColumn)
131       .then(function() {
132         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
133       })
134       .catch(function(err) {
135         logger.info(
136           util.inspect(err, {
137             showHidden: false,
138             depth: null
139           })
140         );
141       });
142   });
143 };
144
145 Migration.prototype.final = function(resolve) {
146   this.sequence.enqueue(function() {
147     return resolve();
148   });
149 };
150
151 module.exports = {
152   up: function(queryInterface, Sequelize) {
153     return new BPromise(function(resolve) {
154       var migration = new Migration(queryInterface);
155
156       // START settings
157       migration.addColumn('settings', 'limitTabs', {
158         type: Sequelize.INTEGER,
159         defaultValue: 20
160       });
161
162       migration.addColumn('settings', 'disabledCookie', {
163         type: Sequelize.BOOLEAN,
164         defaultValue: false
165       });
166
167       migration.addColumn('settings', 'previewRecallmeReminderInterval', {
168         type: Sequelize.INTEGER,
169         defaultValue: 5
170       });
171       // END settings 
172
173       // START cm_hopper
174       migration.addColumn('cm_hopper', 'recallmeNotifiedDate', {
175         type: Sequelize.DATE,
176         allowNull: true
177       });
178       // END cm_hopper 
179
180       // START voice_queues
181       migration.addColumn('voice_queues', 'dialPreviewRecallmeReminder', {
182         type: Sequelize.BOOLEAN,
183         default: false
184       });
185       // END voice_queues 
186
187       // START cloud_providers
188       migration.changeColumn('cloud_providers', 'service', {
189         type: Sequelize.ENUM('AmazonAWS', 'Google', 'MicrosoftAzure')
190       });
191
192       migration.addColumn('cloud_providers', 'type', {
193         type: Sequelize.ENUM('Outlook365', 'Dynamics365'),
194         after: 'service'
195       });
196
197       migration.addColumn('cloud_providers', 'data3', {
198         type: Sequelize.STRING,
199         after: 'data2'
200       });
201
202       migration.addColumn('cloud_providers', 'data4', {
203         type: Sequelize.STRING,
204         after: 'data3'
205       });
206
207       migration.addColumn('cloud_providers', 'data5', {
208         type: Sequelize.TEXT,
209         after: 'data4'
210       });
211
212       migration.addColumn('cloud_providers', 'data6', {
213         type: Sequelize.TEXT,
214         after: 'data5'
215       });
216
217       migration.addColumn('cloud_providers', 'data7', {
218         type: Sequelize.TEXT,
219         after: 'data6'
220       });
221       // END cloud_providers 
222
223       // START mail_accounts
224       migration.addColumn('mail_accounts', 'provider', {
225         type: Sequelize.ENUM('gmail', 'hotmail', 'yahoo', 'outlook365'),
226         after: 'name',
227         defaultValue: null
228       });
229       // END mail_accounts 
230
231       // START mail_servers_in
232       migration.query('UPDATE mail_servers_in SET host = \'imap.gmail.com\' WHERE service = \'gmail\' AND host IS NULL');
233       migration.query('UPDATE mail_servers_in SET port = 993 WHERE service = \'gmail\' AND port IS NULL');
234
235       migration.query('UPDATE mail_servers_in SET host = \'imap-mail.outlook.com\' WHERE service = \'hotmail\' AND host IS NULL');
236       migration.query('UPDATE mail_servers_in SET port = 993 WHERE service = \'hotmail\' AND port IS NULL');
237
238       migration.query('UPDATE mail_servers_in SET host = \'imap.mail.yahoo.com\' WHERE service = \'yahoo\' AND host IS NULL');
239       migration.query('UPDATE mail_servers_in SET port = 993 WHERE service = \'yahoo\' AND port IS NULL');
240
241       migration.query('UPDATE mail_servers_in SET host = \'outlook.office365.com\' WHERE service = \'outlook365\' AND host IS NULL');
242       migration.query('UPDATE mail_servers_in SET port = 993 WHERE service = \'outlook365\' AND port IS NULL');
243
244       migration.addColumn('mail_servers_in', 'modernAuthentication', {
245         type: Sequelize.BOOLEAN,
246         defaultValue: false,
247         after: 'authentication'
248       });
249
250       migration.addColumn('mail_servers_in', 'CloudProviderId', {
251         type: Sequelize.INTEGER
252       });
253
254       migration.addIndex('mail_servers_in', ['CloudProviderId'], 'CloudProviderId');
255
256       migration.query('ALTER TABLE `mail_servers_in` ADD CONSTRAINT `mail_servers_in_ibfk_2` FOREIGN KEY (`CloudProviderId`) REFERENCES `cloud_providers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE');
257       // END mail_servers_in 
258
259       // START mail_servers_out
260       migration.query('UPDATE mail_servers_out SET host = \'smtp.gmail.com\' WHERE service = \'gmail\' AND host IS NULL');
261       migration.query('UPDATE mail_servers_out SET port = 465 WHERE service = \'gmail\' AND port IS NULL AND secure = 0');
262       migration.query('UPDATE mail_servers_out SET port = 587 WHERE service = \'gmail\' AND port IS NULL AND secure = 1');
263
264       migration.query('UPDATE mail_servers_out SET host = \'smtp-mail.outlook.com\' WHERE service = \'hotmail\' AND host IS NULL');
265       migration.query('UPDATE mail_servers_out SET port = 587 WHERE service = \'hotmail\' AND port IS NULL');
266
267       migration.query('UPDATE mail_servers_out SET host = \'smtp.mail.yahoo.com\' WHERE service = \'yahoo\' AND host IS NULL');
268       migration.query('UPDATE mail_servers_out SET port = 465 WHERE service = \'yahoo\' AND port IS NULL');
269
270       migration.query('UPDATE mail_servers_out SET host = \'smtp.office365.com\' WHERE service = \'outlook365\' AND host IS NULL');
271       migration.query('UPDATE mail_servers_out SET port = 587 WHERE service = \'outlook365\' AND port IS NULL');
272
273       migration.addColumn('mail_servers_out', 'modernAuthentication', {
274         type: Sequelize.BOOLEAN,
275         defaultValue: false,
276         after: 'authentication'
277       });
278
279       migration.addColumn('mail_servers_out', 'CloudProviderId', {
280         type: Sequelize.INTEGER
281       });
282
283       migration.addIndex('mail_servers_out', ['CloudProviderId'], 'CloudProviderId');
284
285       migration.query('ALTER TABLE `mail_servers_out` ADD CONSTRAINT `mail_servers_out_ibfk_2` FOREIGN KEY (`CloudProviderId`) REFERENCES `cloud_providers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE');
286       // END mail_servers_out 
287
288       // START FINAL
289       migration.final(resolve);
290       // END FINAL
291     });
292   },
293
294   down: function(queryInterface, Sequelize) {
295     // var migration = new Migration(queryInterface);
296   }
297 };