Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.2.6.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 mail_interactions
157       migration.query('ALTER TABLE mail_interactions CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
158       //END mail_interactions
159
160       // START analytics_custom_reports
161       migration.query('UPDATE analytics_custom_reports SET joins = REPLACE(joins,\'"type":"outer_join"\',\'"type":"left_join"\') WHERE joins LIKE \'%"type":"outer_join"%\'');
162       // END analytics_custom_reports
163
164       // START square_projects
165       migration.query('UPDATE square_projects SET preproduction = REPLACE(preproduction,\'labelBackgroundColor=#ffffff;\',\'\')');
166       migration.query('UPDATE square_projects SET production = REPLACE(production,\'labelBackgroundColor=#ffffff;\',\'\')');
167       // END square_projects
168
169       // START mandatoryDisposition
170       migration.addColumn('voice_queues', 'mandatoryDisposition', {
171         type: Sequelize.BOOLEAN,
172         defaultValue: false
173       });
174       migration.addColumn('voice_queues', 'mandatoryDispositionPauseId', {
175         type: Sequelize.INTEGER
176       });
177       migration.query('ALTER TABLE voice_queues \
178                                 ADD CONSTRAINT `voice_queues_ibfk_4` \
179                                 FOREIGN KEY (`mandatoryDispositionPauseId`) \
180                                 REFERENCES tools_pauses(`id`) \
181                                 ON UPDATE CASCADE \
182                                 ON DELETE CASCADE');
183
184       migration.addColumn('chat_websites', 'mandatoryDisposition', {
185         type: Sequelize.BOOLEAN,
186         defaultValue: false
187       });
188       migration.addColumn('chat_websites', 'mandatoryDispositionPauseId', {
189         type: Sequelize.INTEGER
190       });
191       migration.query('ALTER TABLE chat_websites \
192                                 ADD CONSTRAINT `chat_websites_ibfk_3` \
193                                 FOREIGN KEY (`mandatoryDispositionPauseId`) \
194                                 REFERENCES tools_pauses(`id`) \
195                                 ON UPDATE CASCADE \
196                                 ON DELETE CASCADE');
197
198
199       migration.addColumn('fax_accounts', 'mandatoryDisposition', {
200         type: Sequelize.BOOLEAN,
201         defaultValue: false
202       });
203       migration.addColumn('fax_accounts', 'mandatoryDispositionPauseId', {
204         type: Sequelize.INTEGER
205       });
206       migration.query('ALTER TABLE fax_accounts \
207                                 ADD CONSTRAINT `fax_accounts_ibfk_3` \
208                                 FOREIGN KEY (`mandatoryDispositionPauseId`) \
209                                 REFERENCES tools_pauses(`id`) \
210                                 ON UPDATE CASCADE \
211                                 ON DELETE CASCADE');
212
213
214       migration.addColumn('mail_accounts', 'mandatoryDisposition', {
215         type: Sequelize.BOOLEAN,
216         defaultValue: false
217       });
218       migration.addColumn('mail_accounts', 'mandatoryDispositionPauseId', {
219         type: Sequelize.INTEGER
220       });
221       migration.query('ALTER TABLE mail_accounts \
222                                 ADD CONSTRAINT `mail_accounts_ibfk_3` \
223                                 FOREIGN KEY (`mandatoryDispositionPauseId`) \
224                                 REFERENCES tools_pauses(`id`) \
225                                 ON UPDATE CASCADE \
226                                 ON DELETE CASCADE');
227
228
229       migration.addColumn('openchannel_accounts', 'mandatoryDisposition', {
230         type: Sequelize.BOOLEAN,
231         defaultValue: false
232       });
233       migration.addColumn('openchannel_accounts', 'mandatoryDispositionPauseId', {
234         type: Sequelize.INTEGER
235       });
236       migration.query('ALTER TABLE openchannel_accounts \
237                                 ADD CONSTRAINT `openchannel_accounts_ibfk_2` \
238                                 FOREIGN KEY (`mandatoryDispositionPauseId`) \
239                                 REFERENCES tools_pauses(`id`) \
240                                 ON UPDATE CASCADE \
241                                 ON DELETE CASCADE');
242
243
244       migration.addColumn('sms_accounts', 'mandatoryDisposition', {
245         type: Sequelize.BOOLEAN,
246         defaultValue: false
247       });
248       migration.addColumn('sms_accounts', 'mandatoryDispositionPauseId', {
249         type: Sequelize.INTEGER
250       });
251       migration.query('ALTER TABLE sms_accounts \
252                                 ADD CONSTRAINT `sms_accounts_ibfk_2` \
253                                 FOREIGN KEY (`mandatoryDispositionPauseId`) \
254                                 REFERENCES tools_pauses(`id`) \
255                                 ON UPDATE CASCADE \
256                                 ON DELETE CASCADE');
257
258
259       // END mandatoryDisposition
260
261       // START FINAL
262       migration.final(resolve);
263       // END FINAL
264     });
265   },
266
267   down: function(queryInterface, Sequelize) {
268     // var migration = new Migration(queryInterface);
269   }
270 };