Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.5.2.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 whatsapp
159       migration.query('ALTER TABLE whatsapp_accounts ENCRYPTION=\'Y\';');
160       migration.query('ALTER TABLE whatsapp_interactions ENCRYPTION=\'Y\';');
161       migration.query('ALTER TABLE whatsapp_messages ENCRYPTION=\'Y\';');
162       migration.query('ALTER TABLE report_whatsapp_queue ENCRYPTION=\'Y\';');
163       // END whatsapp
164
165       // START cloud_providers
166       migration.query('ALTER TABLE cloud_providers ENCRYPTION=\'Y\';');
167       // END cloud_providers
168
169       // START voice_extensions
170       migration.query('ALTER TABLE voice_extensions DROP FOREIGN KEY voice_extensions_ibfk_7');
171       migration.query('ALTER TABLE voice_extensions DROP INDEX voice_extensions_ibfk_7');
172       migration.query('ALTER TABLE voice_extensions ADD CONSTRAINT voice_extensions_ibfk_7 FOREIGN KEY (VoicePrefixId) REFERENCES voice_prefixes(id) ON UPDATE CASCADE ON DELETE CASCADE');
173       // END  voice_extensions
174
175       //START voice_queues
176       migration.addColumn('voice_queues', 'dialNoSuchNumberMaxRetry', {
177         type: Sequelize.INTEGER,
178         defaultValue: 3
179       });
180       migration.addColumn('voice_queues', 'dialNoSuchNumberRetryFrequency', {
181         type: Sequelize.INTEGER,
182         defaultValue: 150
183       });
184
185       migration.addColumn('voice_queues', 'dialDropMaxRetry', {
186         type: Sequelize.INTEGER,
187         defaultValue: 3
188       });
189       migration.addColumn('voice_queues', 'dialDropRetryFrequency', {
190         type: Sequelize.INTEGER,
191         defaultValue: 150
192       });
193
194       migration.addColumn('voice_queues', 'dialAbandonedMaxRetry', {
195         type: Sequelize.INTEGER,
196         defaultValue: 3
197       });
198       migration.addColumn('voice_queues', 'dialAbandonedRetryFrequency', {
199         type: Sequelize.INTEGER,
200         defaultValue: 150
201       });
202
203       migration.addColumn('voice_queues', 'dialMachineMaxRetry', {
204         type: Sequelize.INTEGER,
205         defaultValue: 3
206       });
207       migration.addColumn('voice_queues', 'dialMachineRetryFrequency', {
208         type: Sequelize.INTEGER,
209         defaultValue: 150
210       });
211
212       migration.addColumn('voice_queues', 'dialAgentRejectMaxRetry', {
213         type: Sequelize.INTEGER,
214         defaultValue: 3
215       });
216       migration.addColumn('voice_queues', 'dialAgentRejectRetryFrequency', {
217         type: Sequelize.INTEGER,
218         defaultValue: 150
219       });
220       //END voice_queues
221
222       //START campaigns
223       migration.addColumn('campaigns', 'dialNoSuchNumberMaxRetry', {
224         type: Sequelize.INTEGER,
225         defaultValue: 3
226       });
227       migration.addColumn('campaigns', 'dialNoSuchNumberRetryFrequency', {
228         type: Sequelize.INTEGER,
229         defaultValue: 150
230       });
231       migration.addColumn('campaigns', 'dialDropMaxRetry', {
232         type: Sequelize.INTEGER,
233         defaultValue: 3
234       });
235       migration.addColumn('campaigns', 'dialDropRetryFrequency', {
236         type: Sequelize.INTEGER,
237         defaultValue: 150
238       });
239       migration.addColumn('campaigns', 'dialAbandonedMaxRetry', {
240         type: Sequelize.INTEGER,
241         defaultValue: 3
242       });
243       migration.addColumn('campaigns', 'dialAbandonedRetryFrequency', {
244         type: Sequelize.INTEGER,
245         defaultValue: 150
246       });
247       migration.addColumn('campaigns', 'dialMachineMaxRetry', {
248         type: Sequelize.INTEGER,
249         defaultValue: 3
250       });
251       migration.addColumn('campaigns', 'dialMachineRetryFrequency', {
252         type: Sequelize.INTEGER,
253         defaultValue: 150
254       });
255       migration.addColumn('campaigns', 'dialAgentRejectMaxRetry', {
256         type: Sequelize.INTEGER,
257         defaultValue: 3
258       });
259       migration.addColumn('campaigns', 'dialAgentRejectRetryFrequency', {
260         type: Sequelize.INTEGER,
261         defaultValue: 150
262       });
263       //END campaigns            
264
265       //START cm_hopper
266       migration.addColumn('cm_hopper', 'countnosuchnumberretry', {
267         type: Sequelize.INTEGER,
268         defaultValue: 0
269       });
270       migration.addColumn('cm_hopper', 'countdropretry', {
271         type: Sequelize.INTEGER,
272         defaultValue: 0
273       });
274       migration.addColumn('cm_hopper', 'countabandonedretry', {
275         type: Sequelize.INTEGER,
276         defaultValue: 0
277       });
278       migration.addColumn('cm_hopper', 'countmachineretry', {
279         type: Sequelize.INTEGER,
280         defaultValue: 0
281       });
282       migration.addColumn('cm_hopper', 'countagentrejectretry', {
283         type: Sequelize.INTEGER,
284         defaultValue: 0
285       });
286       //END cm_hopper
287
288       //START cm_hopper_final
289       migration.addColumn('cm_hopper_final', 'countnosuchnumberretry', {
290         type: Sequelize.INTEGER,
291         defaultValue: 0
292       });
293       migration.addColumn('cm_hopper_final', 'countdropretry', {
294         type: Sequelize.INTEGER,
295         defaultValue: 0
296       });
297       migration.addColumn('cm_hopper_final', 'countabandonedretry', {
298         type: Sequelize.INTEGER,
299         defaultValue: 0
300       });
301       migration.addColumn('cm_hopper_final', 'countmachineretry', {
302         type: Sequelize.INTEGER,
303         defaultValue: 0
304       });
305       migration.addColumn('cm_hopper_final', 'countagentrejectretry', {
306         type: Sequelize.INTEGER,
307         defaultValue: 0
308       });
309       //END cm_hopper_final
310
311       //START cm_hopper_history
312       migration.addColumn('cm_hopper_history', 'countnosuchnumberretry', {
313         type: Sequelize.INTEGER,
314         defaultValue: 0
315       });
316       migration.addColumn('cm_hopper_history', 'countdropretry', {
317         type: Sequelize.INTEGER,
318         defaultValue: 0
319       });
320       migration.addColumn('cm_hopper_history', 'countabandonedretry', {
321         type: Sequelize.INTEGER,
322         defaultValue: 0
323       });
324       migration.addColumn('cm_hopper_history', 'countmachineretry', {
325         type: Sequelize.INTEGER,
326         defaultValue: 0
327       });
328       migration.addColumn('cm_hopper_history', 'countagentrejectretry', {
329         type: Sequelize.INTEGER,
330         defaultValue: 0
331       });
332       //END cm_hopper_history
333
334       // START FINAL
335       migration.final(resolve);
336       // END FINAL
337     });
338   },
339
340   down: function(queryInterface, Sequelize) {
341     // var migration = new Migration(queryInterface);
342   }
343 };