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.final = function(resolve) {
93 this.sequence.enqueue(function() {
99 up: function(queryInterface, Sequelize) {
100 return new BPromise(function(resolve, reject) {
102 var migration = new Migration(queryInterface);
105 migration.dropTable('sms_messages');
106 migration.dropTable('sms_dispositions');
107 migration.dropTable('sms_applications');
108 migration.dropTable('user_has_sms_interactions');
109 migration.dropTable('sms_interactions');
110 migration.dropTable('team_has_sms_queues');
111 migration.dropTable('user_has_sms_queues');
112 migration.dropTable('sms_queues');
113 migration.dropTable('sms_accounts');
117 migration.addColumn('cm_contacts', 'facebook', {
118 type: Sequelize.STRING
121 migration.addColumn('cm_contacts', 'twitter', {
122 type: Sequelize.STRING
126 // START CHAT WEBSITES
127 migration.query('ALTER TABLE chat_websites DROP FOREIGN KEY chat_websites_ibfk_2');
128 migration.query('ALTER TABLE chat_websites DROP COLUMN OfflineTemplateId');
129 migration.addColumn('chat_websites', 'timezone', {
130 type: Sequelize.STRING
132 migration.addColumn('chat_websites', 'token', {
133 type: Sequelize.STRING
135 migration.addColumn('chat_websites', 'IntervalId', {
136 type: Sequelize.INTEGER(11)
138 migration.query('ALTER TABLE chat_websites ADD CONSTRAINT chat_websites_ibfk_2 FOREIGN KEY (IntervalId) REFERENCES tools_intervals(id) ON UPDATE CASCADE ON DELETE SET NULL');
142 migration.addColumn('report_queue', 'transfer', {
143 type: Sequelize.BOOLEAN,
146 migration.addColumn('report_queue', 'transfertype', {
147 type: Sequelize.STRING,
148 comment: 'FORWARD, BLIND, ATTENDED'
150 migration.addColumn('report_queue', 'transferexten', {
151 type: Sequelize.STRING
153 migration.addColumn('report_queue', 'transferuniqueid', {
154 type: Sequelize.STRING
159 migration.addColumn('report_jscripty_answers', 'questionId', {
160 type: Sequelize.STRING
162 migration.addColumn('report_jscripty_questions', 'questionId', {
163 type: Sequelize.STRING
167 // START USER PHONEBAR
168 migration.addColumn('users', 'phoneBarUnansweredCallBadge', {
169 type: Sequelize.BOOLEAN,
174 migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '1', 'NoOp', '', 'system', 'Dialer extensions auto generated', NOW(), NOW());");
176 migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '2', 'set', 'CDR(type)=dialer', 'system', 'Dialer extensions auto generated', NOW(), NOW());");
178 migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '3', 'set', 'CALLERID(all)=${XMD-CALLERID}', 'system', 'Dialer extensions auto generated', NOW(), NOW());");
180 migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '4', 'dial', '${XMD-DIALCHANNEL},${XMD-DIALTIMEOUT},${XMD-DIALOPTIONS},${XMD-DIALURL}', 'system', 'Dialer extensions auto generated', NOW(), NOW());");
182 migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '5', 'hangup', 'system', 'dialer extensions auto generated', NOW(), NOW());");
183 // END EXTENSION DIALER
186 migration.final(resolve);
191 down: function(queryInterface, Sequelize) {
192 var migration = new Migration(queryInterface);