Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.5.5.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, options) {
64   var _this = this;
65   this.sequence.enqueue(function() {
66     return _this.queryInterface
67       .addIndex(table, column, {
68         indexName: options.indexName,
69         indicesType: options.indicesType
70       })
71       .then(function() {
72         logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);
73       })
74       .catch(function(err) {
75         logger.info(JSON.stringify(err));
76       });
77   });
78 };
79
80 Migration.prototype.removeIndex = function(table, indexName) {
81   var _this = this;
82   this.sequence.enqueue(function() {
83     return _this.queryInterface
84       .removeIndex(table, indexName)
85       .then(function() {
86         logger.info('removeIndex %s %s', table, indexName);
87       })
88       .catch(function(err) {
89         logger.info(JSON.stringify(err));
90       });
91   });
92 };
93
94 Migration.prototype.query = function(sql) {
95   var _this = this;
96   this.sequence.enqueue(function() {
97     return _this.queryInterface.sequelize
98       .query(sql)
99       .then(function() {
100         logger.info('query %s', sql);
101       })
102       .catch(function(err) {
103         logger.info(JSON.stringify(err));
104       });
105   });
106 };
107
108 Migration.prototype.removeColumn = function(table, column) {
109   var _this = this;
110   this.sequence.enqueue(function() {
111     return _this.queryInterface
112       .removeColumn(table, column)
113       .then(function() {
114         logger.info('Removed column %s from %s', column, table);
115       })
116       .catch(function(err) {
117         logger.info(
118           util.inspect(err, {
119             showHidden: false,
120             depth: null
121           })
122         );
123       });
124   });
125 };
126
127 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
128   var _this = this;
129   this.sequence.enqueue(function() {
130     return _this.queryInterface
131       .renameColumn(table, oldColumn, newColumn)
132       .then(function() {
133         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
134       })
135       .catch(function(err) {
136         logger.info(
137           util.inspect(err, {
138             showHidden: false,
139             depth: null
140           })
141         );
142       });
143   });
144 };
145
146 Migration.prototype.final = function(resolve) {
147   this.sequence.enqueue(function() {
148     return resolve();
149   });
150 };
151
152 module.exports = {
153   up: function(queryInterface, Sequelize) {
154     return new BPromise(function(resolve) {
155
156       var migration = new Migration(queryInterface);
157
158       // START tools_actions
159       migration.query("UPDATE tools_actions SET data3 = 'phone' WHERE action = 'contactManager' AND data3 = '0'");
160       migration.query("UPDATE tools_actions SET data4 = NULL WHERE action = 'contactManager' AND data2 = '0' AND data4 = '0'");
161       migration.query("UPDATE tools_actions SET data4 = 'phone' WHERE action = 'contactManager' AND data4 = '0'");
162       // END tools_actions
163
164       // START settings
165       migration.addColumn('settings', 'enforcePasswordHistory', {
166         type: Sequelize.BOOLEAN,
167         defaultValue: true
168       });
169
170       migration.addColumn('settings', 'passwordHistoryLimit', {
171         type: Sequelize.INTEGER(2).UNSIGNED,
172         defaultValue: 3,
173         validate: {
174           min: 1,
175           max: 5
176         }
177       });
178       // END settings
179
180       // START users
181       migration.addColumn('users', 'previousPasswords', {
182         type: Sequelize.STRING,
183         after: 'passwordResetAt'
184       });
185
186       migration.addColumn('users', 'settingsEnabled', {
187         type: Sequelize.BOOLEAN,
188         defaultValue: true
189       });
190       // END users
191
192       // START cm_custom_fields
193       migration.addColumn('cm_custom_fields', 'clickToAction', {
194         type: Sequelize.BOOLEAN,
195         defaultValue: false
196       });
197
198       migration.addColumn('cm_custom_fields', 'actionType', {
199         type: Sequelize.ENUM('voice'),
200         allowNull: false,
201         defaultValue: 'voice'
202       });
203       // END cm_custom_fields
204
205       // START tools_disposition
206       migration.addColumn('tools_dispositions', 'level', {
207         type: Sequelize.ENUM('first', 'second', 'third'),
208         allowNull: false,
209         defaultValue: 'first',
210         after: "name"
211       });
212
213       migration.addColumn('tools_dispositions', 'description', {
214         type: Sequelize.STRING,
215         after: "level"
216       });
217
218       migration.addColumn('tools_dispositions', 'ParentId', {
219         type: Sequelize.INTEGER,
220         after: "updatedAt"
221       });
222
223       migration.query('ALTER TABLE tools_dispositions ADD CONSTRAINT `tools_dispositions_ibfk_8` FOREIGN KEY (`ParentId`) REFERENCES `tools_dispositions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE');
224       // END tools_disposition
225
226       // START report_call
227       migration.addColumn('report_call', 'userSecondDisposition', {
228         type: Sequelize.STRING,
229         after: "userDisposition"
230       });
231
232       migration.addColumn('report_call', 'userThirdDisposition', {
233         type: Sequelize.STRING,
234         after: "userSecondDisposition"
235       });
236       // END report_call
237
238       // START report_queue
239       migration.addColumn('report_queue', 'secondDisposition', {
240         type: Sequelize.STRING,
241         after: "disposition"
242       });
243
244       migration.addColumn('report_queue', 'thirdDisposition', {
245         type: Sequelize.STRING,
246         after: "secondDisposition"
247       });
248       // END report_queue
249
250       // START voice_recordings
251       migration.addColumn('voice_recordings', 'userSecondDisposition', {
252         type: Sequelize.STRING,
253         after: "userDisposition"
254       });
255
256       migration.addColumn('voice_recordings', 'userThirdDisposition', {
257         type: Sequelize.STRING,
258         after: "userSecondDisposition"
259       });
260       // END voice_recordings
261
262       // START chat_interactions
263       migration.addColumn('chat_interactions', 'secondDisposition', {
264         type: Sequelize.STRING,
265         after: "disposition"
266       });
267
268       migration.addColumn('chat_interactions', 'thirdDisposition', {
269         type: Sequelize.STRING,
270         after: "secondDisposition"
271       });
272       // END chat_interactions
273
274       // START fax_interactions
275       migration.addColumn('fax_interactions', 'secondDisposition', {
276         type: Sequelize.STRING,
277         after: "disposition"
278       });
279
280       migration.addColumn('fax_interactions', 'thirdDisposition', {
281         type: Sequelize.STRING,
282         after: "secondDisposition"
283       });
284       // END fax_interactions
285
286       // START mail_interactions
287       migration.addColumn('mail_interactions', 'secondDisposition', {
288         type: Sequelize.STRING,
289         after: "disposition"
290       });
291
292       migration.addColumn('mail_interactions', 'thirdDisposition', {
293         type: Sequelize.STRING,
294         after: "secondDisposition"
295       });
296       // END mail_interactions
297
298       // START openchannel_interactions
299       migration.addColumn('openchannel_interactions', 'secondDisposition', {
300         type: Sequelize.STRING,
301         after: "disposition"
302       });
303
304       migration.addColumn('openchannel_interactions', 'thirdDisposition', {
305         type: Sequelize.STRING,
306         after: "secondDisposition"
307       });
308       // END openchannel_interactions
309
310       // START sms_interactions
311       migration.addColumn('sms_interactions', 'secondDisposition', {
312         type: Sequelize.STRING,
313         after: "disposition"
314       });
315
316       migration.addColumn('sms_interactions', 'thirdDisposition', {
317         type: Sequelize.STRING,
318         after: "secondDisposition"
319       });
320       // END sms_interactions
321
322       // START whatsapp_interactions
323       migration.addColumn('whatsapp_interactions', 'secondDisposition', {
324         type: Sequelize.STRING,
325         after: "disposition"
326       });
327
328       migration.addColumn('whatsapp_interactions', 'thirdDisposition', {
329         type: Sequelize.STRING,
330         after: "secondDisposition"
331       });
332       // END whatsapp_interactions
333
334       // START cm_hopper_final
335       migration.addColumn('cm_hopper_final', 'secondDisposition', {
336         type: Sequelize.STRING,
337         after: "disposition"
338       });
339
340       migration.addColumn('cm_hopper_final', 'thirdDisposition', {
341         type: Sequelize.STRING,
342         after: "secondDisposition"
343       });
344       // END cm_hopper_final
345
346       // START FINAL
347       migration.final(resolve);
348       // END FINAL
349     });
350   },
351
352   down: function(queryInterface, Sequelize) {
353     // var migration = new Migration(queryInterface);
354   }
355 };