3 var BPromise = require('bluebird');
5 var logger = require('../config/logger')('migration');
6 var util = require('util');
8 var Sequence = function() {};
10 Sequence.prototype.enqueue = function(fn) {
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
14 var Migration = function(queryInterface) {
15 this.queryInterface = queryInterface;
16 this.sequence = new Sequence();
19 Migration.prototype.changeColumn = function(table, column, type) {
21 this.sequence.enqueue(function() {
22 return _this.queryInterface
23 .changeColumn(table, column, type)
25 logger.info('Changed column %s in table %s', column, table);
27 .catch(function(err) {
28 logger.info(JSON.stringify(err));
33 Migration.prototype.addColumn = function(table, column, type) {
35 this.sequence.enqueue(function() {
36 return _this.queryInterface
37 .addColumn(table, column, type)
39 logger.info('Added column %s to %s', column, table);
41 .catch(function(err) {
42 logger.info(JSON.stringify(err));
47 Migration.prototype.dropTable = function(table) {
49 this.sequence.enqueue(function() {
50 return _this.queryInterface
55 logger.info('table dropped %s', table);
57 .catch(function(err) {
58 logger.info(JSON.stringify(err));
63 Migration.prototype.addIndex = function(table, column, indexName) {
65 this.sequence.enqueue(function() {
66 return _this.queryInterface.addIndex(table, column, {
70 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
72 .catch(function(err) {
73 logger.info(JSON.stringify(err));
78 Migration.prototype.query = function(sql) {
80 this.sequence.enqueue(function() {
81 return _this.queryInterface.sequelize.query(sql)
83 logger.info('query %s', sql);
85 .catch(function(err) {
86 logger.info(JSON.stringify(err));
91 Migration.prototype.removeColumn = function(table, column) {
93 this.sequence.enqueue(function() {
94 return _this.queryInterface.removeColumn(table, column)
96 logger.info('Removed column %s from %s', column, table);
98 .catch(function(err) {
99 logger.info(util.inspect(err, {
107 Migration.prototype.final = function(resolve) {
109 this.sequence.enqueue(function() {
115 up: function(queryInterface, Sequelize) {
116 return new BPromise(function(resolve, reject) {
117 var migration = new Migration(queryInterface);
120 migration.addColumn('chat_websites', 'key', {
121 type: Sequelize.STRING,
124 migration.addColumn('chat_websites', 'forwardTranscriptMessage', {
125 type: Sequelize.STRING
127 migration.addColumn('chat_websites', 'skipMessageButton', {
128 type: Sequelize.STRING,
131 migration.addColumn('chat_websites', 'enableFeedback', {
132 type: Sequelize.BOOLEAN,
135 migration.addColumn('chat_websites', 'enableCustomerWriting', {
136 type: Sequelize.BOOLEAN,
139 migration.addColumn('chat_websites', 'conditionAgreement', {
140 type: Sequelize.BOOLEAN,
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.'
147 migration.addColumn('chat_websites', 'waitingMessage', {
148 type: Sequelize.STRING,
149 defaultValue: 'Please hold and we will answer your request as soon as possible.'
151 migration.addColumn('chat_websites', 'autoclose', {
152 type: Sequelize.BOOLEAN,
155 migration.addColumn('chat_websites', 'unmanagedMessage', {
156 type: Sequelize.STRING,
157 defaultValue: 'Your request has not been processed.'
159 migration.addColumn('chat_websites', 'enableUnmanagedNote', {
160 type: Sequelize.BOOLEAN,
163 migration.addColumn('chat_websites', 'sendUnmanaged', {
164 type: Sequelize.STRING,
167 migration.addColumn('chat_websites', 'skipUnmanaged', {
168 type: Sequelize.STRING,
171 migration.changeColumn('chat_websites', 'closingMessage', {
172 type: Sequelize.STRING,
173 defaultValue: 'Thanks you for your time!'
177 // START CHATINTERACTION
178 migration.addColumn('chat_interactions', 'browserName', {
179 type: Sequelize.STRING
181 migration.addColumn('chat_interactions', 'browserVersion', {
182 type: Sequelize.STRING
184 migration.addColumn('chat_interactions', 'osName', {
185 type: Sequelize.STRING
187 migration.addColumn('chat_interactions', 'osVersion', {
188 type: Sequelize.STRING
190 migration.addColumn('chat_interactions', 'deviceModel', {
191 type: Sequelize.STRING
193 migration.addColumn('chat_interactions', 'deviceVendor', {
194 type: Sequelize.STRING
196 migration.addColumn('chat_interactions', 'deviceType', {
197 type: Sequelize.STRING
199 // END CHATINTERACTION
201 // START CHATMESSAGES
202 migration.addColumn('chat_messages', 'AttachmentId', {
203 type: Sequelize.INTEGER
205 migration.query('ALTER TABLE `chat_messages` \
206 ADD CONSTRAINT `chat_messages_ibfk_5` \
207 FOREIGN KEY (`AttachmentId`) \
208 REFERENCES attachments(`id`) \
210 ON DELETE SET NULL');
214 migration.addColumn('voice_queues', 'dialRecallMeTimeout', {
215 type: Sequelize.INTEGER(5).UNSIGNED,
218 migration.addColumn('voice_queues', 'dialRecallInQueue', {
219 type: Sequelize.BOOLEAN,
222 migration.addColumn('cm_hopper', 'recallme', {
223 type: Sequelize.BOOLEAN,
226 migration.addColumn('cm_hopper_final', 'recallme', {
227 type: Sequelize.BOOLEAN,
230 migration.addColumn('cm_hopper_history', 'recallme', {
231 type: Sequelize.BOOLEAN,
237 migration.addColumn('voice_recordings', 'userDisposition', {
238 type: Sequelize.STRING
243 migration.dropTable('user_has_mail_rooms');
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');
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');
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');
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');
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');
280 migration.addColumn('mail_accounts', 'active', {
281 type: Sequelize.BOOLEAN,
285 migration.removeColumn('mail_servers_out', 'state');
287 migration.dropTable('mail_servers_in');
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, \
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;");
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;");
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;');
358 migration.dropTable('fax_attachments');
362 migration.addColumn('sms_accounts', 'phone', {
363 type: Sequelize.STRING
365 migration.addColumn('sms_accounts', 'accountSid', {
366 type: Sequelize.STRING
368 migration.addColumn('sms_accounts', 'authToken', {
369 type: Sequelize.STRING
371 migration.addColumn('sms_accounts', 'key', {
372 type: Sequelize.STRING,
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;');
383 migration.addColumn('openchannel_accounts', 'key', {
384 type: Sequelize.STRING,
390 migration.addColumn('fax_accounts', 'key', {
391 type: Sequelize.STRING,
397 migration.addColumn('mail_accounts', 'key', {
398 type: Sequelize.STRING,
404 migration.final(resolve);
409 down: function(queryInterface, Sequelize) {
410 var migration = new Migration(queryInterface);