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