Built motion from commit 639a1a7f.|2.6.19
[motion2.git] / server / migrations / 2.6.19.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var util = require('util');
5 var logger = require('../config/logger')('migration');
6 var Sequence = function() {};
7
8 Sequence.prototype.enqueue = function(fn) {
9   this.tail = this.tail ? this.tail.finally(fn) : fn();
10 };
11
12 var Migration = function(queryInterface) {
13   this.queryInterface = queryInterface;
14   this.sequence = new Sequence();
15 };
16
17 Migration.prototype.changeColumn = function(table, column, type) {
18   var _this = this;
19   this.sequence.enqueue(function() {
20     return _this.queryInterface
21       .changeColumn(table, column, type)
22       .then(function() {
23         logger.info('Changed column %s in table %s', column, table);
24       })
25       .catch(function(err) {
26         logger.info(JSON.stringify(err));
27       });
28   });
29 };
30
31 Migration.prototype.addColumn = function(table, column, type) {
32   var _this = this;
33   this.sequence.enqueue(function() {
34     return _this.queryInterface
35       .addColumn(table, column, type)
36       .then(function() {
37         logger.info('Added column %s to %s', column, table);
38       })
39       .catch(function(err) {
40         logger.info(JSON.stringify(err));
41       });
42   });
43 };
44
45 Migration.prototype.dropTable = function(table) {
46   var _this = this;
47   this.sequence.enqueue(function() {
48     return _this.queryInterface
49       .dropTable(table, {
50         force: true,
51       })
52       .then(function() {
53         logger.info('table dropped %s', table);
54       })
55       .catch(function(err) {
56         logger.info(JSON.stringify(err));
57       });
58   });
59 };
60
61 Migration.prototype.addIndex = function(table, column, indexName) {
62   var _this = this;
63   this.sequence.enqueue(function() {
64     return _this.queryInterface
65       .addIndex(table, column, {
66         indexName: indexName,
67       })
68       .then(function() {
69         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
70       })
71       .catch(function(err) {
72         logger.info(JSON.stringify(err));
73       });
74   });
75 };
76
77 Migration.prototype.removeIndex = function(table, indexName) {
78   var _this = this;
79   this.sequence.enqueue(function() {
80     return _this.queryInterface
81       .removeIndex(table, indexName)
82       .then(function() {
83         logger.info('removeIndex %s %s', table, indexName);
84       })
85       .catch(function(err) {
86         logger.info(JSON.stringify(err));
87       });
88   });
89 };
90
91 Migration.prototype.query = function(sql) {
92   var _this = this;
93   this.sequence.enqueue(function() {
94     return _this.queryInterface.sequelize
95       .query(sql)
96       .then(function() {
97         logger.info('query %s', sql);
98       })
99       .catch(function(err) {
100         logger.info(JSON.stringify(err));
101       });
102   });
103 };
104
105 Migration.prototype.removeColumn = function(table, column) {
106   var _this = this;
107   this.sequence.enqueue(function() {
108     return _this.queryInterface
109       .removeColumn(table, column)
110       .then(function() {
111         logger.info('Removed column %s from %s', column, table);
112       })
113       .catch(function(err) {
114         logger.info(
115           util.inspect(err, {
116             showHidden: false,
117             depth: null,
118           })
119         );
120       });
121   });
122 };
123
124 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
125   var _this = this;
126   this.sequence.enqueue(function() {
127     return _this.queryInterface
128       .renameColumn(table, oldColumn, newColumn)
129       .then(function() {
130         logger.info(
131           'Renamed column from %s to %s on %s',
132           oldColumn,
133           newColumn,
134           table
135         );
136       })
137       .catch(function(err) {
138         logger.info(
139           util.inspect(err, {
140             showHidden: false,
141             depth: null,
142           })
143         );
144       });
145   });
146 };
147
148 Migration.prototype.final = function(resolve) {
149   this.sequence.enqueue(function() {
150     return resolve();
151   });
152 };
153
154 module.exports = {
155   up: function(queryInterface, Sequelize) {
156     return new BPromise(function(resolve) {
157       var migration = new Migration(queryInterface);
158
159       // START change field canned answer to medium
160       migration.query('ALTER TABLE `tools_canned_answers` MODIFY COLUMN value mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;');
161       // END change field canned answer to medium
162       // START chat_websites
163       migration.addColumn('chat_websites', 'waitForTheAssignedQueue', {
164         type: Sequelize.INTEGER,
165         validate: {
166           min: 1,
167           max: 2147483
168         },
169         defaultValue: 10
170       });
171       // END chat_websites
172
173       // START mail_accounts
174       migration.addColumn('mail_accounts', 'waitForTheAssignedQueue', {
175         type: Sequelize.INTEGER,
176         validate: {
177           min: 1,
178           max: 2147483
179         },
180         defaultValue: 10
181       });
182       // END mail_accounts
183
184       // START sms_accounts
185       migration.addColumn('sms_accounts', 'waitForTheAssignedQueue', {
186         type: Sequelize.INTEGER,
187         validate: {
188           min: 1,
189           max: 2147483
190         },
191         defaultValue: 10
192       });
193       // END sms_accounts
194
195       // START openchannel_accounts
196       migration.addColumn('openchannel_accounts', 'waitForTheAssignedQueue', {
197         type: Sequelize.INTEGER,
198         validate: {
199           min: 1,
200           max: 2147483
201         },
202         defaultValue: 10
203       });
204       // END openchannel_accounts
205
206       // START fax_accounts
207       migration.addColumn('fax_accounts', 'waitForTheAssignedQueue', {
208         type: Sequelize.INTEGER,
209         validate: {
210           min: 1,
211           max: 2147483
212         },
213         defaultValue: 10
214       });
215       // END fax_accounts
216
217       // START whatsapp_accounts
218       migration.addColumn('whatsapp_accounts', 'waitForTheAssignedQueue', {
219         type: Sequelize.INTEGER,
220         validate: {
221           min: 1,
222           max: 2147483
223         },
224         defaultValue: 10
225       });
226       // END whatsapp_accounts     
227
228       // START chat_interactions
229       migration.addColumn('chat_interactions', 'queueId', {
230         type: Sequelize.INTEGER,
231         allowNull: true
232       });
233       // END chat_interactions
234
235       // START mail_interactions
236       migration.addColumn('mail_interactions', 'queueId', {
237         type: Sequelize.INTEGER,
238         allowNull: true
239       });
240       // END mail_interactions           
241
242       // START sms_interactions
243       migration.addColumn('sms_interactions', 'queueId', {
244         type: Sequelize.INTEGER,
245         allowNull: true
246       });
247       // END sms_interactions  
248
249       // START openchannel_interactions
250       migration.addColumn('openchannel_interactions', 'queueId', {
251         type: Sequelize.INTEGER,
252         allowNull: true
253       });
254       // END openchannel_interactions  
255
256       // START fax_interactions
257       migration.addColumn('fax_interactions', 'queueId', {
258         type: Sequelize.INTEGER,
259         allowNull: true
260       });
261       // END fax_interactions  
262
263       // START whatsapp_interactions
264       migration.addColumn('whatsapp_interactions', 'queueId', {
265         type: Sequelize.INTEGER,
266         allowNull: true
267       });
268       // END whatsapp_interactions  
269
270       // START users
271       migration.addColumn('users', 'messengerSoundNotification', {
272         type: Sequelize.BOOLEAN,
273         defaultValue: false
274       });
275       // END users
276
277
278       // START FINAL
279       migration.final(resolve);
280       // END FINAL
281     });
282   },
283
284   down: function( /* queryInterface, Sequelize */ ) {
285     // var migration = new Migration(queryInterface);
286   },
287 };