Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.34.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4
5 var logger = require('../config/logger')('migration');
6 var util = require('util');
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(res) {
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(res) {
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(res) {
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.addIndex(table, column, {
67         indexName: indexName
68       })
69       .then(function(res) {
70         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
71       })
72       .catch(function(err) {
73         logger.info(JSON.stringify(err));
74       });
75   });
76 };
77
78 Migration.prototype.query = function(sql) {
79   var _this = this;
80   this.sequence.enqueue(function() {
81     return _this.queryInterface.sequelize.query(sql)
82       .then(function(res) {
83         logger.info('query %s', sql);
84       })
85       .catch(function(err) {
86         logger.info(JSON.stringify(err));
87       });
88   });
89 };
90
91 Migration.prototype.removeColumn = function(table, column) {
92   var _this = this;
93   this.sequence.enqueue(function() {
94     return _this.queryInterface.removeColumn(table, column)
95       .then(function(res) {
96         logger.info('Removed column %s from %s', column, table);
97       })
98       .catch(function(err) {
99         logger.info(util.inspect(err, {
100           showHidden: false,
101           depth: null
102         }));
103       });
104   });
105 };
106
107 Migration.prototype.final = function(resolve) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return resolve();
111   });
112 };
113
114 module.exports = {
115   up: function(queryInterface, Sequelize) {
116     return new BPromise(function(resolve, reject) {
117       var migration = new Migration(queryInterface);
118
119       // START CHATWEBSITE
120       migration.addColumn('chat_websites', 'key', {
121         type: Sequelize.STRING,
122         allowNull: false
123       });
124       migration.addColumn('chat_websites', 'forwardTranscriptMessage', {
125         type: Sequelize.STRING
126       });
127       migration.addColumn('chat_websites', 'skipMessageButton', {
128         type: Sequelize.STRING,
129         defaultValue: 'Skip'
130       });
131       migration.addColumn('chat_websites', 'enableFeedback', {
132         type: Sequelize.BOOLEAN,
133         defaultValue: false
134       });
135       migration.addColumn('chat_websites', 'enableCustomerWriting', {
136         type: Sequelize.BOOLEAN,
137         defaultValue: false
138       });
139       migration.addColumn('chat_websites', 'conditionAgreement', {
140         type: Sequelize.BOOLEAN,
141         defaultValue: false
142       });
143       migration.addColumn('chat_websites', 'waitingTitle', {
144         type: Sequelize.STRING,
145         defaultValue: 'All of our agents are busy at this time. Your chat is very important to us.'
146       });
147       migration.addColumn('chat_websites', 'waitingMessage', {
148         type: Sequelize.STRING,
149         defaultValue: 'Please hold and we will answer your request as soon as possible.'
150       });
151       migration.addColumn('chat_websites', 'autoclose', {
152         type: Sequelize.BOOLEAN,
153         defaultValue: true
154       });
155       migration.addColumn('chat_websites', 'unmanagedMessage', {
156         type: Sequelize.STRING,
157         defaultValue: 'Your request has not been processed.'
158       });
159       migration.addColumn('chat_websites', 'enableUnmanagedNote', {
160         type: Sequelize.BOOLEAN,
161         defaultValue: true
162       });
163       migration.addColumn('chat_websites', 'sendUnmanaged', {
164         type: Sequelize.STRING,
165         defaultValue: 'Send'
166       });
167       migration.addColumn('chat_websites', 'skipUnmanaged', {
168         type: Sequelize.STRING,
169         defaultValue: 'Skip'
170       });
171       migration.changeColumn('chat_websites', 'closingMessage', {
172         type: Sequelize.STRING,
173         defaultValue: 'Thanks you for your time!'
174       });
175       // END CHATWEBSITE
176
177       // START CHATINTERACTION
178       migration.addColumn('chat_interactions', 'browserName', {
179         type: Sequelize.STRING
180       });
181       migration.addColumn('chat_interactions', 'browserVersion', {
182         type: Sequelize.STRING
183       });
184       migration.addColumn('chat_interactions', 'osName', {
185         type: Sequelize.STRING
186       });
187       migration.addColumn('chat_interactions', 'osVersion', {
188         type: Sequelize.STRING
189       });
190       migration.addColumn('chat_interactions', 'deviceModel', {
191         type: Sequelize.STRING
192       });
193       migration.addColumn('chat_interactions', 'deviceVendor', {
194         type: Sequelize.STRING
195       });
196       migration.addColumn('chat_interactions', 'deviceType', {
197         type: Sequelize.STRING
198       });
199       // END CHATINTERACTION
200
201       // START CHATMESSAGES
202       migration.addColumn('chat_messages', 'AttachmentId', {
203         type: Sequelize.INTEGER
204       });
205       migration.query('ALTER TABLE `chat_messages` \
206             ADD CONSTRAINT `chat_messages_ibfk_5` \
207             FOREIGN KEY (`AttachmentId`) \
208             REFERENCES attachments(`id`) \
209             ON UPDATE CASCADE \
210             ON DELETE SET NULL');
211       // END CHATMESSAGES
212
213       // START RECALLME
214       migration.addColumn('voice_queues', 'dialRecallMeTimeout', {
215         type: Sequelize.INTEGER(5).UNSIGNED,
216         defaultValue: 30
217       });
218       migration.addColumn('voice_queues', 'dialRecallInQueue', {
219         type: Sequelize.BOOLEAN,
220         defaultValue: false
221       });
222       migration.addColumn('cm_hopper', 'recallme', {
223         type: Sequelize.BOOLEAN,
224         defaultValue: false
225       });
226       migration.addColumn('cm_hopper_final', 'recallme', {
227         type: Sequelize.BOOLEAN,
228         defaultValue: false
229       });
230       migration.addColumn('cm_hopper_history', 'recallme', {
231         type: Sequelize.BOOLEAN,
232         defaultValue: false
233       });
234       // STOP RECALLME
235
236       // START RECORDINGS
237       migration.addColumn('voice_recordings', 'userDisposition', {
238         type: Sequelize.STRING
239       });
240       // STOP RECORDINGS
241
242       // START EMAIL
243       migration.dropTable('user_has_mail_rooms');
244
245       migration.query('ALTER TABLE mail_rooms DROP FOREIGN KEY mail_rooms_ibfk_1');
246       migration.query('ALTER TABLE mail_rooms DROP COLUMN MailAccountId');
247       migration.dropTable('mail_rooms');
248
249       migration.query('ALTER TABLE analytics_extracted_reports DROP FOREIGN KEY analytics_extracted_reports_ibfk_1');
250       migration.query('ALTER TABLE mail_messages DROP COLUMN MailMessageId');
251
252       migration.query('ALTER TABLE mail_messages DROP FOREIGN KEY mail_messages_ibfk_1');
253       migration.query('ALTER TABLE mail_messages DROP COLUMN MailRoomId');
254       migration.query('ALTER TABLE mail_messages DROP FOREIGN KEY mail_messages_ibfk_2');
255       migration.query('ALTER TABLE mail_messages DROP COLUMN UserId');
256       migration.dropTable('mail_messages');
257
258       migration.dropTable('mail_attachments');
259       migration.dropTable('mail_dispositions');
260       migration.dropTable('mail_applications');
261       migration.dropTable('user_has_mail_interactions');
262       migration.dropTable('mail_interactions');
263       migration.dropTable('team_has_mail_queues');
264       migration.dropTable('user_has_mail_queues');
265       migration.dropTable('mail_queues');
266
267       migration.removeColumn('mail_accounts', 'fidelity');
268       migration.removeColumn('mail_accounts', 'timeout');
269       migration.removeColumn('mail_accounts', 'whiteLabel');
270       migration.removeColumn('mail_accounts', 'acceptUrl');
271       migration.removeColumn('mail_accounts', 'rejectUrl');
272       migration.removeColumn('mail_accounts', 'acceptMethod');
273       migration.removeColumn('mail_accounts', 'rejectMethod');
274       migration.removeColumn('mail_accounts', 'actions');
275       migration.removeColumn('mail_accounts', 'closeUrl');
276       migration.removeColumn('mail_accounts', 'closeMethod');
277       migration.removeColumn('mail_accounts', 'signature');
278       migration.removeColumn('mail_accounts', 'custom');
279
280       migration.addColumn('mail_accounts', 'active', {
281         type: Sequelize.BOOLEAN,
282         defaultValue: true
283       });
284
285       migration.removeColumn('mail_servers_out', 'state');
286
287       migration.dropTable('mail_servers_in');
288
289       migration.query("CREATE TABLE `mail_interactions` ( \
290       `id` int(11) NOT NULL AUTO_INCREMENT, \
291       `closed` tinyint(1) DEFAULT '0', \
292       `closedAt` datetime DEFAULT NULL, \
293       `disposition` varchar(255) DEFAULT NULL, \
294       `note` varchar(255) DEFAULT NULL, \
295       `inReplyTo` varchar(190) DEFAULT NULL, \
296       `to` varchar(255) DEFAULT NULL, \
297       `cc` longtext, \
298       `subject` text, \
299       `createdAt` datetime NOT NULL, \
300       `updatedAt` datetime NOT NULL, \
301       `UserId` int(11) DEFAULT NULL, \
302       `MailAccountId` int(11) DEFAULT NULL, \
303       `ContactId` int(11) DEFAULT NULL, \
304       PRIMARY KEY (`id`), \
305       KEY `UserId` (`UserId`), \
306       KEY `MailAccountId` (`MailAccountId`), \
307       KEY `ContactId` (`ContactId`), \
308       CONSTRAINT `mail_interactions_ibfk_1` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, \
309       CONSTRAINT `mail_interactions_ibfk_2` FOREIGN KEY (`MailAccountId`) REFERENCES `mail_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, \
310       CONSTRAINT `mail_interactions_ibfk_3` FOREIGN KEY (`ContactId`) REFERENCES `cm_contacts` (`id`) ON UPDATE CASCADE \
311     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
312
313       migration.query("CREATE TABLE `mail_messages` ( \
314               `id` int(11) NOT NULL AUTO_INCREMENT, \
315               `body` longtext COLLATE utf8mb4_unicode_ci NOT NULL, \
316               `read` tinyint(1) DEFAULT '0', \
317               `direction` enum('in','out') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'out', \
318               `messageId` varchar(190) COLLATE utf8mb4_unicode_ci DEFAULT NULL, \
319               `from` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, \
320               `to` longtext COLLATE utf8mb4_unicode_ci, \
321               `cc` longtext COLLATE utf8mb4_unicode_ci, \
322               `bcc` longtext COLLATE utf8mb4_unicode_ci, \
323               `subject` text COLLATE utf8mb4_unicode_ci, \
324               `sentAt` datetime DEFAULT NULL, \
325               `createdAt` datetime NOT NULL, \
326               `updatedAt` datetime NOT NULL, \
327               `MailAccountId` int(11) DEFAULT NULL, \
328               `MailInteractionId` int(11) DEFAULT NULL, \
329               `UserId` int(11) DEFAULT NULL, \
330               `ContactId` int(11) DEFAULT NULL, \
331               PRIMARY KEY (`id`), \
332               KEY `MailAccountId` (`MailAccountId`), \
333               KEY `MailInteractionId` (`MailInteractionId`), \
334               KEY `UserId` (`UserId`), \
335               KEY `ContactId` (`ContactId`), \
336               KEY `messageId` (`messageId`), \
337               CONSTRAINT `mail_messages_ibfk_1` FOREIGN KEY (`MailAccountId`) REFERENCES `mail_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, \
338               CONSTRAINT `mail_messages_ibfk_2` FOREIGN KEY (`MailInteractionId`) REFERENCES `mail_interactions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, \
339               CONSTRAINT `mail_messages_ibfk_3` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, \
340               CONSTRAINT `mail_messages_ibfk_4` FOREIGN KEY (`ContactId`) REFERENCES `cm_contacts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
341           ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
342       // STOP EMAIL
343
344       // START ATTACHMENTS
345       migration.query('CREATE TABLE `attachments` ( \
346         `id` int(11) NOT NULL AUTO_INCREMENT, \
347         `name` varchar(255) DEFAULT NULL, \
348         `basename` varchar(255) DEFAULT NULL, \
349         `type` varchar(255) DEFAULT NULL, \
350         `createdAt` datetime NOT NULL, \
351         `updatedAt` datetime NOT NULL, \
352         `MailMessageId` int(11) DEFAULT NULL, \
353         PRIMARY KEY (`id`), \
354         KEY `MailMessageId` (`MailMessageId`), \
355         CONSTRAINT `attachments_ibfk_1` FOREIGN KEY (`MailMessageId`) REFERENCES `mail_messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
356       ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;');
357
358       migration.dropTable('fax_attachments');
359       // END ATTACHMENTS
360
361       // START SMS ACCOUNT
362       migration.addColumn('sms_accounts', 'phone', {
363         type: Sequelize.STRING
364       });
365       migration.addColumn('sms_accounts', 'accountSid', {
366         type: Sequelize.STRING
367       });
368       migration.addColumn('sms_accounts', 'authToken', {
369         type: Sequelize.STRING
370       });
371       migration.addColumn('sms_accounts', 'key', {
372         type: Sequelize.STRING,
373         allowNull: false
374       });
375       // END SMS ACCOUNT
376
377       // START COLLATE CHANGE
378       migration.query('ALTER TABLE users CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;');
379       // migration.query('ALTER TABLE voice_extensions CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;');
380       //END COLLATE CHANGE
381
382       //START OPENCHANNEL
383       migration.addColumn('openchannel_accounts', 'key', {
384         type: Sequelize.STRING,
385         allowNull: false
386       });
387       //END OPENCHANNEL
388
389       //START FAX
390       migration.addColumn('fax_accounts', 'key', {
391         type: Sequelize.STRING,
392         allowNull: false
393       });
394       //END FAX
395
396       //START MAIL
397       migration.addColumn('mail_accounts', 'key', {
398         type: Sequelize.STRING,
399         allowNull: false
400       });
401       //END MAIL
402
403       // START FINAL
404       migration.final(resolve);
405       // END FINAL
406     });
407   },
408
409   down: function(queryInterface, Sequelize) {
410     var migration = new Migration(queryInterface);
411   }
412 };