Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.60.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(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() {
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.query = function(sql) {
79   var _this = this;
80   this.sequence.enqueue(function() {
81     return _this.queryInterface.sequelize.query(sql)
82       .then(function() {
83         logger.info('query %s', sql);
84       })
85       .catch(function(err) {
86         logger.info(JSON.stringify(err));
87       });
88   });
89 };
90
91 Migration.prototype.removeColumn = function(table, column) {
92   var _this = this;
93   this.sequence.enqueue(function() {
94     return _this.queryInterface.removeColumn(table, column)
95       .then(function() {
96         logger.info('Removed column %s from %s', column, table);
97       })
98       .catch(function(err) {
99         logger.info(util.inspect(err, {
100           showHidden: false,
101           depth: null
102         }));
103       });
104   });
105 };
106
107 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
111       .then(function() {
112         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
113       })
114       .catch(function(err) {
115         logger.info(util.inspect(err, {
116           showHidden: false,
117           depth: null
118         }));
119       });
120   });
121 };
122
123 Migration.prototype.final = function(resolve) {
124   this.sequence.enqueue(function() {
125     return resolve();
126   });
127 };
128
129 module.exports = {
130   up: function(queryInterface, Sequelize) {
131     return new BPromise(function(resolve) {
132       var migration = new Migration(queryInterface);
133
134       // START CHAT WEBSITES
135       migration.addColumn('chat_websites', 'agentIdentifier', {
136         type: Sequelize.STRING,
137         defaultValue: 'website_alias'
138       });
139
140       migration.addColumn('chat_websites', 'waitForTheAssignedAgent', {
141         type: Sequelize.INTEGER,
142         validate: {
143           min: 1,
144           max: 2147483
145         },
146         defaultValue: 10
147       });
148       // END CHAT WEBSITES
149
150       // START chat_interactions
151       migration.removeColumn('chat_interactions', 'openedBy');
152       // END chat_interactions
153
154       // START mail_interactions
155       migration.removeColumn('mail_interactions', 'openedBy');
156
157       migration.addColumn('mail_interactions', 'lastMsgBody', {
158         type: Sequelize.TEXT('long')
159       });
160       // END mail_interactions
161
162       // START sms_interactions
163       migration.removeColumn('sms_interactions', 'openedBy');
164       // END sms_interactions
165
166       // START fax_interactions
167       migration.removeColumn('fax_interactions', 'openedBy');
168       // END fax_interactions
169
170       // START openchannel_interactions
171       migration.removeColumn('openchannel_interactions', 'openedBy');
172       // END openchannel_interactions
173
174       // START OPENCHANNEL INTERACTION
175       migration.addColumn('openchannel_interactions', 'from', {
176         type: Sequelize.STRING
177       });
178       // END OPENCHANNEL INTERACTION
179
180       // START MAIL ACCOUNT
181       migration.addColumn('mail_accounts', 'waitForTheAssignedAgent', {
182         type: Sequelize.INTEGER,
183         validate: {
184           min: 1,
185           max: 2147483
186         },
187         defaultValue: 10
188       });
189       // END MAIL ACCOUNT
190
191       // START OPENCHANNEL ACCOUNT
192       migration.addColumn('openchannel_accounts', 'waitForTheAssignedAgent', {
193         type: Sequelize.INTEGER,
194         validate: {
195           min: 1,
196           max: 2147483
197         },
198         defaultValue: 10
199       });
200       // END OPENCHANNEL ACCOUNT
201
202       // START OPENCHANNEL ACCOUNT
203       migration.addColumn('fax_accounts', 'waitForTheAssignedAgent', {
204         type: Sequelize.INTEGER,
205         validate: {
206           min: 1,
207           max: 2147483
208         },
209         defaultValue: 10
210       });
211       // END OPENCHANNEL ACCOUNT
212
213       // START SMS ACCOUNT
214       migration.addColumn('sms_accounts', 'waitForTheAssignedAgent', {
215         type: Sequelize.INTEGER,
216         validate: {
217           min: 1,
218           max: 2147483
219         },
220         defaultValue: 10
221       });
222       // END SMS ACCOUNT
223
224       // START USERS
225       migration.addColumn('users', 'phoneBarEnableJaws', {
226         type: Sequelize.BOOLEAN,
227         defaultValue: false
228       });
229       //END USERS
230
231       // START FINAL
232       migration.final(resolve);
233       // END FINAL
234     });
235   },
236
237   down: function(queryInterface, Sequelize) {
238     // var migration = new Migration(queryInterface);
239   }
240 };