Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.71.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var util = require('util');
5
6 var logger = require('../config/logger')('migration');
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() {
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() {
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() {
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() {
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.removeIndex = function(table, indexName) {
79   var _this = this;
80   this.sequence.enqueue(function() {
81     return _this.queryInterface.removeIndex(table, indexName)
82       .then(function() {
83         logger.info('removeIndex %s %s', table, indexName);
84       })
85       .catch(function(err) {
86         logger.info(JSON.stringify(err));
87       });
88   });
89 };
90
91 Migration.prototype.query = function(sql) {
92   var _this = this;
93   this.sequence.enqueue(function() {
94     return _this.queryInterface.sequelize.query(sql)
95       .then(function() {
96         logger.info('query %s', sql);
97       })
98       .catch(function(err) {
99         logger.info(JSON.stringify(err));
100       });
101   });
102 };
103
104 Migration.prototype.removeColumn = function(table, column) {
105   var _this = this;
106   this.sequence.enqueue(function() {
107     return _this.queryInterface.removeColumn(table, column)
108       .then(function() {
109         logger.info('Removed column %s from %s', column, table);
110       })
111       .catch(function(err) {
112         logger.info(util.inspect(err, {
113           showHidden: false,
114           depth: null
115         }));
116       });
117   });
118 };
119
120 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
121   var _this = this;
122   this.sequence.enqueue(function() {
123     return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
124       .then(function() {
125         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
126       })
127       .catch(function(err) {
128         logger.info(util.inspect(err, {
129           showHidden: false,
130           depth: null
131         }));
132       });
133   });
134 };
135
136 Migration.prototype.final = function(resolve) {
137   this.sequence.enqueue(function() {
138     return resolve();
139   });
140 };
141
142 module.exports = {
143   up: function(queryInterface, Sequelize) {
144     return new BPromise(function(resolve) {
145       var migration = new Migration(queryInterface);
146
147       //START CHAT_WEBSITE
148       migration.addColumn('chat_websites', 'queueTransfer', {
149         type: Sequelize.BOOLEAN,
150         defaultValue: false
151       });
152       migration.addColumn('chat_websites', 'enableCustomerCheckmarks', {
153         type: Sequelize.BOOLEAN,
154         defaultValue: false
155       });
156       migration.addColumn('chat_websites', 'queueTransferTimeout', {
157         type: Sequelize.INTEGER,
158         validate: {
159           min: 1,
160           max: 2147483
161         },
162         defaultValue: 300
163       });
164       migration.addColumn('chat_websites', 'agentTransfer', {
165         type: Sequelize.BOOLEAN,
166         defaultValue: false
167       });
168       migration.addColumn('chat_websites', 'agentTransferTimeout', {
169         type: Sequelize.INTEGER,
170         validate: {
171           min: 1,
172           max: 2147483
173         },
174         defaultValue: 300
175       });
176       migration.addColumn('chat_websites', 'systemAlias', {
177         type: Sequelize.STRING,
178         defaultValue: 'System'
179       });
180       migration.addColumn('chat_websites', 'systemAvatar', {
181         type: Sequelize.TEXT
182       });
183       //END CHAT_WEBSITE
184
185       //START FAX_ACCOUNT
186       migration.addColumn('fax_accounts', 'queueTransfer', {
187         type: Sequelize.BOOLEAN,
188         defaultValue: false
189       });
190       migration.addColumn('fax_accounts', 'queueTransferTimeout', {
191         type: Sequelize.INTEGER,
192         validate: {
193           min: 1,
194           max: 2147483
195         },
196         defaultValue: 300
197       });
198       migration.addColumn('fax_accounts', 'agentTransfer', {
199         type: Sequelize.BOOLEAN,
200         defaultValue: false
201       });
202       migration.addColumn('fax_accounts', 'agentTransferTimeout', {
203         type: Sequelize.INTEGER,
204         validate: {
205           min: 1,
206           max: 2147483
207         },
208         defaultValue: 300
209       });
210       //END FAX_ACCOUNT
211
212       //START OPENCHANNEL_ACCOUNT
213       migration.addColumn('openchannel_accounts', 'queueTransfer', {
214         type: Sequelize.BOOLEAN,
215         defaultValue: false
216       });
217       migration.addColumn('openchannel_accounts', 'queueTransferTimeout', {
218         type: Sequelize.INTEGER,
219         validate: {
220           min: 1,
221           max: 2147483
222         },
223         defaultValue: 300
224       });
225       migration.addColumn('openchannel_accounts', 'agentTransfer', {
226         type: Sequelize.BOOLEAN,
227         defaultValue: false
228       });
229       migration.addColumn('openchannel_accounts', 'agentTransferTimeout', {
230         type: Sequelize.INTEGER,
231         validate: {
232           min: 1,
233           max: 2147483
234         },
235         defaultValue: 300
236       });
237       //END OPENCHANNEL_ACCOUNT
238
239       //START SMS_ACCOUNT
240       migration.addColumn('sms_accounts', 'queueTransfer', {
241         type: Sequelize.BOOLEAN,
242         defaultValue: false
243       });
244       migration.addColumn('sms_accounts', 'queueTransferTimeout', {
245         type: Sequelize.INTEGER,
246         validate: {
247           min: 1,
248           max: 2147483
249         },
250         defaultValue: 300
251       });
252       migration.addColumn('sms_accounts', 'agentTransfer', {
253         type: Sequelize.BOOLEAN,
254         defaultValue: false
255       });
256       migration.addColumn('sms_accounts', 'agentTransferTimeout', {
257         type: Sequelize.INTEGER,
258         validate: {
259           min: 1,
260           max: 2147483
261         },
262         defaultValue: 300
263       });
264       //END SMS_ACCOUNT
265
266       // START SETTINGS
267       migration.addColumn('settings', 'chatTimeout', {
268         type: Sequelize.INTEGER,
269         defaultValue: 30
270       });
271       migration.query('UPDATE settings SET chatTimeout = 0;');
272       migration.addColumn('settings', 'phoneBarRememberMeEnabled', {
273         type: Sequelize.BOOLEAN,
274         defaultValue: true
275       });
276       // END SETTINGS
277
278       // START LICENSE
279       migration.addColumn('license', 'chatLicenseExceeded', {
280         type: Sequelize.INTEGER,
281         defaultValue: 0
282       });
283       // END LICENSE
284
285       // START TOOLS_ACTIONS
286       migration.addColumn('tools_actions', 'data7', {
287         type: Sequelize.TEXT
288       });
289       // END TOOLS_ACTIONS
290
291       // START FINAL
292       migration.final(resolve);
293       // END FINAL
294     });
295   },
296
297   down: function(queryInterface, Sequelize) {
298     // var migration = new Migration(queryInterface);
299   }
300 };