Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.35.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 REPORT
120       migration.addColumn('analytics_custom_reports', 'joins', {
121         type: Sequelize.TEXT
122       });
123
124       migration.addColumn('analytics_default_reports', 'joins', {
125         type: Sequelize.TEXT
126       });
127       // END REPORT
128
129       // START CHAT MESSAGES
130       migration.query('ALTER TABLE `chat_messages` \
131         ADD CONSTRAINT `chat_messages_ibfk_5` \
132         FOREIGN KEY (`AttachmentId`) \
133         REFERENCES attachments(`id`) \
134         ON UPDATE CASCADE \
135         ON DELETE SET NULL');
136       // END CHAT MESSAGES
137
138       // START CHAT WEBSITE
139       migration.addColumn('chat_websites', 'offline_chat_button', {
140         type: Sequelize.STRING,
141         defaultValue: 'Send'
142       });
143       migration.addColumn('chat_websites', 'enableCustomerAttachment', {
144         type: Sequelize.BOOLEAN,
145         defaultValue: false
146       });
147       // END CHAT WEBSITE
148
149       // START CHAT INTERACTION
150       migration.addColumn('chat_interactions', 'referer', {
151         type: Sequelize.STRING
152       });
153       migration.addColumn('chat_interactions', 'customerIp', {
154         type: Sequelize.STRING
155       });
156       migration.addColumn('chat_interactions', 'ratingType', {
157         type: Sequelize.ENUM('star', 'thumb'),
158         defaultValue: 'star'
159       });
160       migration.query('ALTER TABLE chat_interactions CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
161       // END CHAT INTERACTION
162
163       // START EMAIL
164       migration.addColumn('mail_messages', 'body', {
165         type: Sequelize.TEXT('long'),
166         allowNull: false
167       });
168       migration.addColumn('mail_messages', 'read', {
169         type: Sequelize.BOOLEAN,
170         defaultValue: false
171       });
172       migration.addColumn('mail_messages', 'direction', {
173         type: Sequelize.ENUM('in', 'out'),
174         defaultValue: 'out',
175         allowNull: false
176       });
177       migration.changeColumn('mail_messages', 'messageId', {
178         type: Sequelize.STRING(190)
179       });
180       migration.removeColumn('mail_messages', 'inReplyTo');
181       migration.changeColumn('mail_messages', 'to', {
182         type: Sequelize.TEXT('long')
183       });
184       migration.changeColumn('mail_messages', 'cc', {
185         type: Sequelize.TEXT('long')
186       });
187       migration.changeColumn('mail_messages', 'bcc', {
188         type: Sequelize.TEXT('long')
189       });
190       migration.changeColumn('mail_messages', 'subject', {
191         type: Sequelize.TEXT
192       });
193       migration.addColumn('mail_messages', 'sentAt', {
194         type: Sequelize.DATE
195       });
196       migration.addColumn('mail_messages', 'attach', {
197         type: Sequelize.INTEGER,
198         defaultValue: 0
199       });
200
201       migration.removeColumn('mail_messages', 'fromId');
202       migration.removeColumn('mail_messages', 'attachment');
203       migration.removeColumn('mail_messages', 'text');
204       migration.removeColumn('mail_messages', 'html');
205       migration.removeColumn('mail_messages', 'reason');
206       migration.removeColumn('mail_messages', 'auto');
207       migration.removeColumn('mail_messages', 'status');
208       migration.removeColumn('mail_messages', 'retry');
209       migration.removeColumn('mail_messages', 'voiceSource');
210       migration.removeColumn('mail_messages', 'userName');
211       migration.removeColumn('mail_messages', 'userFullname');
212       migration.removeColumn('mail_messages', 'accountId');
213       migration.removeColumn('mail_messages', 'accountName');
214       migration.removeColumn('mail_messages', 'MailInteractionId');
215
216       migration.addColumn('mail_messages', 'MailAccountId', {
217         type: Sequelize.INTEGER,
218       });
219       migration.query('ALTER TABLE `mail_messages` \
220             ADD CONSTRAINT `mail_messages_ibfk_1` \
221             FOREIGN KEY (`MailAccountId`) \
222             REFERENCES mail_accounts(`id`) \
223             ON UPDATE CASCADE \
224             ON DELETE SET NULL');
225
226       migration.addColumn('mail_messages', 'MailInteractionId', {
227         type: Sequelize.INTEGER,
228       });
229       migration.query('ALTER TABLE `mail_messages` \
230             ADD CONSTRAINT `mail_messages_ibfk_2` \
231             FOREIGN KEY (`MailInteractionId`) \
232             REFERENCES mail_interactions(`id`) \
233             ON UPDATE CASCADE \
234             ON DELETE CASCADE');
235
236       migration.addColumn('mail_messages', 'UserId', {
237         type: Sequelize.INTEGER,
238       });
239       migration.query('ALTER TABLE `mail_messages` \
240             ADD CONSTRAINT `mail_messages_ibfk_3` \
241             FOREIGN KEY (`UserId`) \
242             REFERENCES users(`id`) \
243             ON UPDATE CASCADE \
244             ON DELETE SET NULL');
245
246       migration.addColumn('mail_messages', 'ContactId', {
247         type: Sequelize.INTEGER,
248       });
249       migration.query('ALTER TABLE `mail_messages` \
250             ADD CONSTRAINT `mail_messages_ibfk_4` \
251             FOREIGN KEY (`ContactId`) \
252             REFERENCES cm_contacts(`id`) \
253             ON UPDATE CASCADE \
254             ON DELETE CASCADE');
255
256       migration.query('ALTER TABLE mail_messages CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
257
258       migration.addColumn('mail_interactions', 'attach', {
259         type: Sequelize.BOOLEAN,
260         defaultValue: false
261       });
262
263       migration.addColumn('mail_accounts', 'signature', {
264         type: Sequelize.TEXT
265       });
266       // END MAIL
267
268       // START TEMPLATE
269       migration.removeColumn('tools_templates', 'subject');
270       // END TEMPLATE
271
272       // START USER
273       migration.addColumn('users', 'phoneBarEnableDtmfTone', {
274         type: Sequelize.BOOLEAN,
275         defaultValue: false
276       });
277       migration.addColumn('users', 'phoneBarAutoAnswerDelay', {
278         type: Sequelize.INTEGER,
279         defaultValue: 0
280       });
281       // END USER
282
283       // START REPORT QUEUE MULTICHANNEL
284       migration.dropTable('report_openchannel');
285       migration.dropTable('report_openchannel_session');
286       migration.dropTable('report_chat');
287       migration.dropTable('report_chat_session');
288       migration.dropTable('report_fax');
289       migration.dropTable('report_fax_session');
290       migration.dropTable('report_mail');
291       migration.dropTable('report_mail_session');
292       migration.dropTable('report_sms');
293       migration.dropTable('report_sms_session');
294       // END REPORT QUEUE MULTICHANNEL
295
296       // START EDIT HOPPER
297       migration.addColumn('cm_hopper_history', 'editedat', {
298         type: Sequelize.DATE
299       });
300       migration.addColumn('cm_hopper_history', 'edited', {
301         type: Sequelize.BOOLEAN,
302         defaultValue: false
303       });
304       // END EDIT HOPPER
305
306       // START EDIT VOICE QUEUE / CAMPAIGN
307       migration.addColumn('voice_queues', 'dialOrderByScheduledAt', {
308         type: Sequelize.ENUM('DESC', 'ASC'),
309         defaultValue: 'DESC'
310       });
311       migration.addColumn('campaigns', 'dialOrderByScheduledAt', {
312         type: Sequelize.ENUM('DESC', 'ASC'),
313         defaultValue: 'DESC'
314       });
315       // END EDIT VOICE QUEUE
316
317       // START FINAL
318       migration.final(resolve);
319       // END FINAL
320     });
321   },
322
323   down: function(queryInterface, Sequelize) {
324     var migration = new Migration(queryInterface);
325   }
326 };