Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.59.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var fs = require('fs');
5 var path = require('path');
6 var util = require('util');
7 var _ = require('lodash');
8
9 var logger = require('../config/logger')('migration');
10
11 var Sequence = function() {};
12
13 Sequence.prototype.enqueue = function(fn) {
14   this.tail = this.tail ? this.tail.finally(fn) : fn();
15 };
16
17 var Migration = function(queryInterface) {
18   this.queryInterface = queryInterface;
19   this.sequence = new Sequence();
20 };
21
22 Migration.prototype.changeColumn = function(table, column, type) {
23   var _this = this;
24   this.sequence.enqueue(function() {
25     return _this.queryInterface
26       .changeColumn(table, column, type)
27       .then(function(res) {
28         logger.info('Changed column %s in table %s', column, table);
29       })
30       .catch(function(err) {
31         logger.info(JSON.stringify(err));
32       });
33   });
34 };
35
36 Migration.prototype.addColumn = function(table, column, type) {
37   var _this = this;
38   this.sequence.enqueue(function() {
39     return _this.queryInterface
40       .addColumn(table, column, type)
41       .then(function(res) {
42         logger.info('Added column %s to %s', column, table);
43       })
44       .catch(function(err) {
45         logger.info(JSON.stringify(err));
46       });
47   });
48 };
49
50 Migration.prototype.dropTable = function(table) {
51   var _this = this;
52   this.sequence.enqueue(function() {
53     return _this.queryInterface
54       .dropTable(table, {
55         force: true
56       })
57       .then(function(res) {
58         logger.info('table dropped %s', table);
59       })
60       .catch(function(err) {
61         logger.info(JSON.stringify(err));
62       });
63   });
64 };
65
66 Migration.prototype.addIndex = function(table, column, indexName) {
67   var _this = this;
68   this.sequence.enqueue(function() {
69     return _this.queryInterface.addIndex(table, column, {
70         indexName: indexName
71       })
72       .then(function(res) {
73         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
74       })
75       .catch(function(err) {
76         logger.info(JSON.stringify(err));
77       });
78   });
79 };
80
81 Migration.prototype.query = function(sql) {
82   var _this = this;
83   this.sequence.enqueue(function() {
84     return _this.queryInterface.sequelize.query(sql)
85       .then(function(res) {
86         logger.info('query %s', sql);
87       })
88       .catch(function(err) {
89         logger.info(JSON.stringify(err));
90       });
91   });
92 };
93
94 Migration.prototype.removeColumn = function(table, column) {
95   var _this = this;
96   this.sequence.enqueue(function() {
97     return _this.queryInterface.removeColumn(table, column)
98       .then(function(res) {
99         logger.info('Removed column %s from %s', column, table);
100       })
101       .catch(function(err) {
102         logger.info(util.inspect(err, {
103           showHidden: false,
104           depth: null
105         }));
106       });
107   });
108 };
109
110 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
111   var _this = this;
112   this.sequence.enqueue(function() {
113     return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
114       .then(function(res) {
115         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
116       })
117       .catch(function(err) {
118         logger.info(util.inspect(err, {
119           showHidden: false,
120           depth: null
121         }));
122       });
123   });
124 };
125
126 Migration.prototype.final = function(resolve) {
127   var _this = this;
128   this.sequence.enqueue(function() {
129     return resolve();
130   });
131 };
132
133 module.exports = {
134   up: function(queryInterface, Sequelize) {
135     return new BPromise(function(resolve, reject) {
136
137       var migration = new Migration(queryInterface);
138
139       // START SETTINGS
140       migration.addColumn('settings', 'phoneBarAutoUpdater', {
141         type: Sequelize.BOOLEAN,
142         defaultValue: true
143       });
144       migration.addColumn('settings', 'phoneBarAutoUpdaterUrl', {
145         type: Sequelize.STRING,
146         defaultValue: 'https://www.xcally.com/bar/phonebar/autoupdater.xml'
147       });
148       // END SETTINGS
149
150       // START PREFIX
151       migration.addColumn('campaigns', 'dialPrefix', {
152         type: Sequelize.STRING
153       });
154       // END PREFIX
155
156       // START CM LIST
157       migration.addColumn('cm_lists', 'dialPrefix', {
158         type: Sequelize.STRING
159       });
160       // END CM LIST
161
162       // START USER
163       migration.addColumn('users', 'alias', {
164         type: Sequelize.STRING
165       });
166       // END USER
167
168       // START CM CONTACTS
169       migration.addColumn('cm_contacts', 'skype', {
170         type: Sequelize.STRING
171       });
172       migration.addColumn('cm_contacts', 'UserId', {
173         type: Sequelize.INTEGER
174       });
175       migration.addColumn('cm_contacts', 'priority', {
176         type: Sequelize.INTEGER(2).UNSIGNED,
177         defaultValue: 2,
178         validate: {
179           min: 0,
180           max: 4
181         }
182       });
183       migration.addColumn('cm_contacts', 'scheduledat', {
184         type: Sequelize.DATE,
185         defaultValue: Sequelize.NOW
186       });
187       // END CM CONTACTS
188
189       // START MAIL INTERACTION
190       migration.addColumn('mail_interactions', 'openedBy', {
191         type: Sequelize.STRING,
192         defaultValue: ''
193       });
194       // END MAIL INTERACTION
195
196       // START CHAT INTERACTION
197       migration.addColumn('chat_interactions', 'openedBy', {
198         type: Sequelize.STRING,
199         defaultValue: ''
200       });
201       // END CHAT INTERACTION
202
203       // START SMS INTERACTION
204       migration.addColumn('sms_interactions', 'openedBy', {
205         type: Sequelize.STRING,
206         defaultValue: ''
207       });
208       // END SMS INTERACTION
209
210       // START OPENCHANNEL INTERACTION
211       migration.addColumn('openchannel_interactions', 'openedBy', {
212         type: Sequelize.STRING,
213         defaultValue: ''
214       });
215       // END OPENCHANNEL INTERACTION
216
217       // START FAX INTERACTION
218       migration.addColumn('fax_interactions', 'openedBy', {
219         type: Sequelize.STRING,
220         defaultValue: ''
221       });
222       // END FAX INTERACTION
223
224       // START VOICEMAIL
225       migration.query('DELETE FROM `voice_voicemail_messages` WHERE `mailboxuser` NOT IN (SELECT `mailbox` FROM `voice_voicemail`)');
226
227       migration.query('ALTER TABLE `voice_voicemail_messages` \
228       ADD CONSTRAINT `voice_voicemail_messages_ibfk_1` \
229       FOREIGN KEY (`mailboxuser`) \
230       REFERENCES voice_voicemail(`mailbox`) \
231       ON UPDATE CASCADE \
232       ON DELETE CASCADE');
233       // END VOICEMAIL
234
235       // START FINAL
236       migration.final(resolve);
237       // END FINAL
238     });
239   },
240
241   down: function(queryInterface, Sequelize) {
242     var migration = new Migration(queryInterface);
243   }
244 };