Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.49.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var fs = require('fs');
5 var util = require('util');
6 var _ = require('lodash');
7
8 var logger = require('../config/logger')('migration');
9
10 var Sequence = function() {};
11
12 Sequence.prototype.enqueue = function(fn) {
13   this.tail = this.tail ? this.tail.finally(fn) : fn();
14 };
15
16 var Migration = function(queryInterface) {
17   this.queryInterface = queryInterface;
18   this.sequence = new Sequence();
19 };
20
21 Migration.prototype.changeColumn = function(table, column, type) {
22   var _this = this;
23   this.sequence.enqueue(function() {
24     return _this.queryInterface
25       .changeColumn(table, column, type)
26       .then(function(res) {
27         logger.info('Changed column %s in table %s', column, table);
28       })
29       .catch(function(err) {
30         logger.info(JSON.stringify(err));
31       });
32   });
33 };
34
35 Migration.prototype.addColumn = function(table, column, type) {
36   var _this = this;
37   this.sequence.enqueue(function() {
38     return _this.queryInterface
39       .addColumn(table, column, type)
40       .then(function(res) {
41         logger.info('Added column %s to %s', column, table);
42       })
43       .catch(function(err) {
44         logger.info(JSON.stringify(err));
45       });
46   });
47 };
48
49 Migration.prototype.dropTable = function(table) {
50   var _this = this;
51   this.sequence.enqueue(function() {
52     return _this.queryInterface
53       .dropTable(table, {
54         force: true
55       })
56       .then(function(res) {
57         logger.info('table dropped %s', table);
58       })
59       .catch(function(err) {
60         logger.info(JSON.stringify(err));
61       });
62   });
63 };
64
65 Migration.prototype.addIndex = function(table, column, indexName) {
66   var _this = this;
67   this.sequence.enqueue(function() {
68     return _this.queryInterface.addIndex(table, column, {
69         indexName: indexName
70       })
71       .then(function(res) {
72         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
73       })
74       .catch(function(err) {
75         logger.info(JSON.stringify(err));
76       });
77   });
78 };
79
80 Migration.prototype.query = function(sql) {
81   var _this = this;
82   this.sequence.enqueue(function() {
83     return _this.queryInterface.sequelize.query(sql)
84       .then(function(res) {
85         logger.info('query %s', sql);
86       })
87       .catch(function(err) {
88         logger.info(JSON.stringify(err));
89       });
90   });
91 };
92
93 Migration.prototype.removeColumn = function(table, column) {
94   var _this = this;
95   this.sequence.enqueue(function() {
96     return _this.queryInterface.removeColumn(table, column)
97       .then(function(res) {
98         logger.info('Removed column %s from %s', column, table);
99       })
100       .catch(function(err) {
101         logger.info(util.inspect(err, {
102           showHidden: false,
103           depth: null
104         }));
105       });
106   });
107 };
108
109 Migration.prototype.final = function(resolve) {
110   var _this = this;
111   this.sequence.enqueue(function() {
112     return resolve();
113   });
114 };
115
116 module.exports = {
117   up: function(queryInterface, Sequelize) {
118     return new BPromise(function(resolve, reject) {
119
120       var migration = new Migration(queryInterface);
121
122       // START SMS
123       migration.addColumn('sms_accounts', 'smsMethod', {
124         type: Sequelize.ENUM('basic', 'classic', 'classic+')
125       });
126       migration.addColumn('sms_accounts', 'username', {
127         type: Sequelize.STRING
128       });
129       migration.addColumn('sms_accounts', 'password', {
130         type: Sequelize.STRING
131       });
132       migration.addColumn('sms_accounts', 'senderString', {
133         type: Sequelize.STRING
134       });
135       migration.addColumn('sms_accounts', 'deliveryReport', {
136         type: Sequelize.BOOLEAN,
137         defaultValue: false
138       });
139       // END SMS
140
141       // START USERS
142       migration.addColumn('users', 'allowmessenger', {
143         type: Sequelize.BOOLEAN,
144         defaultValue: true
145       });
146
147       migration.addColumn('users', 'phoneBarOutboundProxy', {
148         type: Sequelize.STRING
149       });
150       //END USERS
151
152       // START CHATWEBSITE
153       migration.addColumn('chat_websites', 'offlineMessageSubject', {
154         type: Sequelize.STRING,
155         defaultValue: 'New offline message from websiteName'
156       });
157
158       migration.addColumn('chat_websites', 'offlineMessageBody', {
159         type: Sequelize.STRING,
160         defaultValue: 'You received a new offline message from websiteName. Here you can find the details of the request:'
161       });
162       // END CHATWEBSITE
163
164       // START AGI AFTER
165       migration.addColumn('voice_queues', 'dialQueueProject2', {
166         type: Sequelize.STRING
167       });
168
169       migration.addColumn('voice_queues', 'dialAgiAfterHangupClient', {
170         type: Sequelize.BOOLEAN,
171         defaultValue: false
172       });
173
174       migration.addColumn('voice_queues', 'dialAgiAfterHangupAgent', {
175         type: Sequelize.BOOLEAN,
176         defaultValue: false
177       });
178
179       migration.query("DELETE FROM `voice_extensions` WHERE `exten`='xcally-motion-dialer' AND `priority`= 11");
180
181       migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-dialer', '11', 'Execif', '$[\"${XMD-AGIAFTER}\" != \"NONE\" ]?AGI(${XMD-AGIAFTER})', 'system', 'dialer extensions auto generated', NOW(), NOW());");
182
183       migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-dialer', '12', 'Hangup', 'system', 'dialer extensions auto generated', NOW(), NOW());");
184
185       migration.query("DELETE FROM `voice_extensions` WHERE `exten`='xcally-motion-preview' AND `priority`= 4");
186
187       migration.query("DELETE FROM `voice_extensions` WHERE `exten`='xcally-motion-preview' AND `priority`= 5");
188
189       migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '4', 'Execif', '$[\"${XMD-AGI}\" != \"NONE\" ]?AGI(${XMD-AGI})', 'system', 'dialer extensions auto generated', NOW(), NOW());");
190
191       migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '5', 'dial', '${XMD-DIALCHANNEL},${XMD-DIALTIMEOUT},${XMD-DIALOPTIONS},${XMD-DIALURL}', 'system', 'dialer extensions auto generated', NOW(), NOW());");
192
193       migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-preview', '6', 'hangup', 'system', 'dialer extensions auto generated', NOW(), NOW());");
194
195       migration.addColumn('cm_hopper_final', 'agiafterat', {
196         type: Sequelize.DATE
197       });
198
199       // END AGI AFTER
200
201       // START FINAL
202       migration.final(resolve);
203       // END FINAL
204     });
205   },
206
207   down: function(queryInterface, Sequelize) {
208     var migration = new Migration(queryInterface);
209   }
210 };