Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.63.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var rs = require('randomstring');
5 var util = require('util');
6
7 var logger = require('../config/logger')('migration');
8
9 var Sequence = function() {};
10
11 Sequence.prototype.enqueue = function(fn) {
12   this.tail = this.tail ? this.tail.finally(fn) : fn();
13 };
14
15 var Migration = function(queryInterface) {
16   this.queryInterface = queryInterface;
17   this.sequence = new Sequence();
18 };
19
20 Migration.prototype.changeColumn = function(table, column, type) {
21   var _this = this;
22   this.sequence.enqueue(function() {
23     return _this.queryInterface
24       .changeColumn(table, column, type)
25       .then(function(res) {
26         logger.info('Changed column %s in table %s', column, table);
27       })
28       .catch(function(err) {
29         logger.info(JSON.stringify(err));
30       });
31   });
32 };
33
34 Migration.prototype.addColumn = function(table, column, type) {
35   var _this = this;
36   this.sequence.enqueue(function() {
37     return _this.queryInterface
38       .addColumn(table, column, type)
39       .then(function() {
40         logger.info('Added column %s to %s', column, table);
41       })
42       .catch(function(err) {
43         logger.info(JSON.stringify(err));
44       });
45   });
46 };
47
48 Migration.prototype.dropTable = function(table) {
49   var _this = this;
50   this.sequence.enqueue(function() {
51     return _this.queryInterface
52       .dropTable(table, {
53         force: true
54       })
55       .then(function() {
56         logger.info('table dropped %s', table);
57       })
58       .catch(function(err) {
59         logger.info(JSON.stringify(err));
60       });
61   });
62 };
63
64 Migration.prototype.addIndex = function(table, column, indexName) {
65   var _this = this;
66   this.sequence.enqueue(function() {
67     return _this.queryInterface.addIndex(table, column, {
68         indexName: indexName
69       })
70       .then(function() {
71         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
72       })
73       .catch(function(err) {
74         logger.info(JSON.stringify(err));
75       });
76   });
77 };
78
79 Migration.prototype.query = function(sql) {
80   var _this = this;
81   this.sequence.enqueue(function() {
82     return _this.queryInterface.sequelize.query(sql)
83       .then(function() {
84         logger.info('query %s', sql);
85       })
86       .catch(function(err) {
87         logger.info(JSON.stringify(err));
88       });
89   });
90 };
91
92 Migration.prototype.removeColumn = function(table, column) {
93   var _this = this;
94   this.sequence.enqueue(function() {
95     return _this.queryInterface.removeColumn(table, column)
96       .then(function() {
97         logger.info('Removed column %s from %s', column, table);
98       })
99       .catch(function(err) {
100         logger.info(util.inspect(err, {
101           showHidden: false,
102           depth: null
103         }));
104       });
105   });
106 };
107
108 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
109   var _this = this;
110   this.sequence.enqueue(function() {
111     return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
112       .then(function() {
113         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
114       })
115       .catch(function(err) {
116         logger.info(util.inspect(err, {
117           showHidden: false,
118           depth: null
119         }));
120       });
121   });
122 };
123
124 Migration.prototype.final = function(resolve) {
125   this.sequence.enqueue(function() {
126     return resolve();
127   });
128 };
129
130 module.exports = {
131   up: function(queryInterface, Sequelize) {
132     return new BPromise(function(resolve) {
133       var migration = new Migration(queryInterface);
134
135       // START CHAT WEBSITE
136       migration.addColumn('chat_websites', 'messagesAlignment', {
137         type: Sequelize.STRING,
138         defaultValue: 'alternate'
139       });
140
141       migration.addColumn('chat_websites', 'defaultTitle', {
142         type: Sequelize.STRING
143       });
144
145       migration.addColumn('chat_websites', 'customerAvatar', {
146         type: Sequelize.TEXT
147       });
148
149       migration.addColumn('chat_websites', 'showCustomerAvatar', {
150         type: Sequelize.BOOLEAN,
151         defaultValue: false
152       });
153       // END CHAT WEBSITE
154
155       //START TAG APPLICATION
156
157       migration.addColumn('chat_applications', 'TagId', {
158         type: Sequelize.INTEGER
159       });
160
161       migration.query(
162         'ALTER TABLE `chat_applications` \
163         ADD CONSTRAINT `chat_applications_ibfk_5` \
164         FOREIGN KEY (`TagId`) \
165         REFERENCES tools_tags(`id`) \
166         ON UPDATE CASCADE'
167       );
168
169       migration.addColumn('fax_applications', 'TagId', {
170         type: Sequelize.INTEGER
171       });
172
173       migration.query(
174         'ALTER TABLE `fax_applications` \
175         ADD CONSTRAINT `fax_applications_ibfk_5` \
176         FOREIGN KEY (`TagId`) \
177         REFERENCES tools_tags(`id`) \
178         ON UPDATE CASCADE'
179       );
180
181       migration.addColumn('mail_applications', 'TagId', {
182         type: Sequelize.INTEGER
183       });
184
185       migration.query(
186         'ALTER TABLE `mail_applications` \
187         ADD CONSTRAINT `mail_applications_ibfk_5` \
188         FOREIGN KEY (`TagId`) \
189         REFERENCES tools_tags(`id`) \
190         ON UPDATE CASCADE'
191       );
192
193       migration.addColumn('openchannel_applications', 'TagId', {
194         type: Sequelize.INTEGER
195       });
196
197       migration.query(
198         'ALTER TABLE `openchannel_applications` \
199         ADD CONSTRAINT `openchannel_applications_ibfk_5` \
200         FOREIGN KEY (`TagId`) \
201         REFERENCES tools_tags(`id`) \
202         ON UPDATE CASCADE'
203       );
204
205       migration.addColumn('sms_applications', 'TagId', {
206         type: Sequelize.INTEGER
207       });
208
209       migration.query(
210         'ALTER TABLE `sms_applications` \
211         ADD CONSTRAINT `sms_applications_ibfk_5` \
212         FOREIGN KEY (`TagId`) \
213         REFERENCES tools_tags(`id`) \
214         ON UPDATE CASCADE'
215       );
216
217       //END TAG APPLICATION
218
219       //START REPORT_DIAL
220       migration.query('ALTER TABLE report_dial CHANGE `hodltime` `holdtime` INT(11) DEFAULT NULL');
221       //END REPORT_DIAL
222
223       //START OPENCHANNEL TOKEN
224       migration.addColumn('openchannel_accounts', 'token', {
225         type: Sequelize.STRING
226       });
227       //END OPENCHANNEL TOKEN
228
229       // START VOICE QUEUE LOG
230       migration.addColumn('voice_queues_log', 'data', {
231         type: Sequelize.STRING,
232         allowNull: false,
233         defaultValue: ''
234       });
235       // END VOICE QUEUE LOG
236
237       // START CUSTOM DASHBOARD WIDGET
238       migration.changeColumn('dashboard_items', 'title', {
239         type: Sequelize.STRING,
240         allowNull: false
241       });
242       // END CUSTOM DASHBOARD WIDGET
243
244       // START FINAL
245       migration.final(resolve);
246       // END FINAL
247     });
248   },
249
250   down: function(queryInterface, Sequelize) {
251     // var migration = new Migration(queryInterface);
252   }
253 };