8387fc506a7837eb54c1720e6b5ff0ff0481084c
[motion2.git] / server / migrations / 2.0.69.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 report_jscripty_answers
148       migration.changeColumn('report_jscripty_answers', 'answer', {
149         type: Sequelize.TEXT('long')
150       });
151       // END report_jscripty_answers
152
153       // START report_jscripty_questions
154       migration.changeColumn('report_jscripty_questions', 'answer', {
155         type: Sequelize.TEXT('long')
156       });
157       // END report_jscripty_questions
158
159       // START tools_sounds
160       migration.query(
161         'UPDATE tools_sounds S1 JOIN \
162          (SELECT name FROM tools_sounds GROUP BY name HAVING COUNT(id) > 1) S2 \
163          ON S1.name = S2.name \
164          SET S1.name = CONCAT(S1.name, \'_\', LOWER(LPAD(CONV(FLOOR(RAND()*POW(36,6)), 10, 36), 6, 0)))'
165       );
166
167       migration.changeColumn('tools_sounds', 'name', {
168         type: Sequelize.STRING,
169         unique: true,
170         allowNull: false
171       });
172       // END tools_sounds
173       // START chat_websites
174       migration.addColumn('chat_websites', 'messageFontSize', {
175         type: Sequelize.INTEGER,
176         defaultValue: 12
177       });
178
179       migration.addColumn('chat_websites', 'backgroundColor', {
180         type: Sequelize.STRING,
181         allowNull: false,
182         defaultValue: '#fafafa',
183         validate: {
184           notEmpty: true
185         }
186       });
187       // END chat_websites
188
189       // START CM CONTACTS
190       migration.addColumn('cm_contacts', 'line', {
191         type: Sequelize.STRING
192       });
193       // END CM CONTACTS
194
195       // START voice_prefixes
196       migration.query('DELETE FROM `voice_prefixes`');
197       migration.query('DELETE FROM `user_has_voice_prefixes`');
198       migration.removeIndex('voice_prefixes', 'prefix');
199       migration.removeColumn('voice_prefixes', 'prefix');
200       migration.addColumn('voice_prefixes', 'callerIdAll', {
201         type: Sequelize.STRING
202       });
203       migration.addColumn('voice_prefixes', 'VoiceExtensionId', {
204         type: Sequelize.INTEGER
205       });
206       // END voice_prefixes
207
208       // START voice_extensions
209       migration.addColumn('voice_extensions', 'VoicePrefixId', {
210         type: Sequelize.INTEGER
211       });
212
213       migration.query(
214         'ALTER TABLE `voice_extensions` \
215          ADD CONSTRAINT `voice_extensions_ibfk_7` \
216          FOREIGN KEY (`VoicePrefixId`) \
217          REFERENCES voice_prefixes(`id`) \
218          ON UPDATE CASCADE \
219          ON DELETE CASCADE'
220       );
221
222       migration.query(
223         'update voice_extensions set app = \'ExecIf\', appdata = CONCAT(\'$[${LEN(${PREFIX-CALLERIDALL})} > 0]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):Set(\', appdata, \')\') where appdata LIKE CONCAT(\'CALLERID(all)\', \'%\') and type = \'outbound\''
224       );
225       // END voice_extensions
226
227       // START REPORT CALL
228       migration.addColumn('report_call', 'prefix', {
229         type: Sequelize.STRING
230       });
231       // END REPORT CALL
232
233       // START FINAL
234       migration.final(resolve);
235       // END FINAL
236     });
237   },
238
239   down: function (queryInterface, Sequelize) {
240     // var migration = new Migration(queryInterface);
241   }
242 };