Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.31.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.final = function(resolve) {
92   var _this = this;
93   this.sequence.enqueue(function() {
94     return resolve();
95   });
96 };
97
98 module.exports = {
99   up: function(queryInterface, Sequelize) {
100     return new BPromise(function(resolve, reject) {
101
102       var migration = new Migration(queryInterface);
103
104       // START SMS
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');
114       // END SMS
115
116       // START CM CONTACTS
117       migration.addColumn('cm_contacts', 'facebook', {
118         type: Sequelize.STRING
119       });
120
121       migration.addColumn('cm_contacts', 'twitter', {
122         type: Sequelize.STRING
123       });
124       // END CM CONTACTS
125
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
131       });
132       migration.addColumn('chat_websites', 'token', {
133         type: Sequelize.STRING
134       });
135       migration.addColumn('chat_websites', 'IntervalId', {
136         type: Sequelize.INTEGER(11)
137       });
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');
139       // END CHAT WEBSITES
140
141       // START TRANSFER
142       migration.addColumn('report_queue', 'transfer', {
143         type: Sequelize.BOOLEAN,
144         defaultValue: false
145       });
146       migration.addColumn('report_queue', 'transfertype', {
147         type: Sequelize.STRING,
148         comment: 'FORWARD, BLIND, ATTENDED'
149       });
150       migration.addColumn('report_queue', 'transferexten', {
151         type: Sequelize.STRING
152       });
153       migration.addColumn('report_queue', 'transferuniqueid', {
154         type: Sequelize.STRING
155       });
156       // END TRANSFER
157
158       //START JSCRIPTY
159       migration.addColumn('report_jscripty_answers', 'questionId', {
160         type: Sequelize.STRING
161       });
162       migration.addColumn('report_jscripty_questions', 'questionId', {
163         type: Sequelize.STRING
164       });
165       //END JSCRIPTY
166
167       // START USER PHONEBAR
168       migration.addColumn('users', 'phoneBarUnansweredCallBadge', {
169         type: Sequelize.BOOLEAN,
170         defaultValue: true
171       });
172       // END USER PHONEBAR
173
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());");
175
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());");
177
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());");
179
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());");
181
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
184
185       // START FINAL
186       migration.final(resolve);
187       // END FINAL
188     });
189   },
190
191   down: function(queryInterface, Sequelize) {
192     var migration = new Migration(queryInterface);
193   }
194 };