Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.67.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       var toolsDispositionsQuery = 'CREATE TABLE `tools_dispositions` (\
147         `id` int(11) NOT NULL AUTO_INCREMENT,\
148         `name` varchar(255) NOT NULL,\
149         `createdAt` datetime NOT NULL,\
150         `updatedAt` datetime NOT NULL,\
151         `MailAccountId` int(11) DEFAULT NULL,\
152         `FaxAccountId` int(11) DEFAULT NULL,\
153         `SmsAccountId` int(11) DEFAULT NULL,\
154         `OpenchannelAccountId` int(11) DEFAULT NULL,\
155         `ChatWebsiteId` int(11) DEFAULT NULL,\
156         `ListId` int(11) DEFAULT NULL,\
157         PRIMARY KEY (`id`),\
158         UNIQUE KEY `name_voice` (`name`,`ListId`),\
159         UNIQUE KEY `name_chat` (`name`,`ChatWebsiteId`),\
160         UNIQUE KEY `name_mail` (`name`,`MailAccountId`),\
161         UNIQUE KEY `name_sms` (`name`,`SmsAccountId`),\
162         UNIQUE KEY `name_fax` (`name`,`FaxAccountId`),\
163         UNIQUE KEY `name_openchannel` (`name`,`OpenchannelAccountId`),\
164         KEY `MailAccountId` (`MailAccountId`),\
165         KEY `FaxAccountId` (`FaxAccountId`),\
166         KEY `SmsAccountId` (`SmsAccountId`),\
167         KEY `OpenchannelAccountId` (`OpenchannelAccountId`),\
168         KEY `ChatWebsiteId` (`ChatWebsiteId`),\
169         KEY `ListId` (`ListId`),\
170         CONSTRAINT `tools_dispositions_ibfk_1` FOREIGN KEY (`MailAccountId`) REFERENCES `mail_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,\
171         CONSTRAINT `tools_dispositions_ibfk_2` FOREIGN KEY (`FaxAccountId`) REFERENCES `fax_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,\
172         CONSTRAINT `tools_dispositions_ibfk_3` FOREIGN KEY (`SmsAccountId`) REFERENCES `sms_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,\
173         CONSTRAINT `tools_dispositions_ibfk_4` FOREIGN KEY (`OpenchannelAccountId`) REFERENCES `openchannel_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,\
174         CONSTRAINT `tools_dispositions_ibfk_5` FOREIGN KEY (`ChatWebsiteId`) REFERENCES `chat_websites` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,\
175         CONSTRAINT `tools_dispositions_ibfk_6` FOREIGN KEY (`ListId`) REFERENCES `cm_lists` (`id`) ON DELETE SET NULL ON UPDATE CASCADE\
176       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'
177
178       // START tools_dispositions
179       migration.query(toolsDispositionsQuery);
180       migration.query('INSERT INTO tools_dispositions (`name`, `createdAt`, `updatedAt`, `ChatWebsiteId`) \
181       SELECT `name`, `createdAt`, `updatedAt`, `ChatWebsiteId` FROM chat_dispositions;');
182       migration.query('INSERT INTO tools_dispositions (`name`, `createdAt`, `updatedAt`, `MailAccountId`) \
183       SELECT `name`, `createdAt`, `updatedAt`, `MailAccountId` FROM mail_dispositions;');
184       migration.query('INSERT INTO tools_dispositions (`name`, `createdAt`, `updatedAt`, `FaxAccountId`) \
185       SELECT `name`, `createdAt`, `updatedAt`, `FaxAccountId` FROM fax_dispositions;');
186       migration.query('INSERT INTO tools_dispositions (`name`, `createdAt`, `updatedAt`, `SmsAccountId`) \
187       SELECT `name`, `createdAt`, `updatedAt`, `SmsAccountId` FROM sms_dispositions;');
188       migration.query('INSERT INTO tools_dispositions (`name`, `createdAt`, `updatedAt`, `OpenchannelAccountId`) \
189       SELECT `name`, `createdAt`, `updatedAt`, `OpenchannelAccountId` FROM openchannel_dispositions;');
190       migration.query('INSERT INTO tools_dispositions (`name`, `createdAt`, `updatedAt`, `ListId`) \
191       SELECT `name`, `createdAt`, `updatedAt`, `ListId` FROM voice_dispositions;');
192       // END tools_dispositions
193
194       // START user_has_chat_websites
195       migration.query('CREATE TABLE `user_has_chat_websites` ( \
196         `createdAt` datetime NOT NULL, \
197         `updatedAt` datetime NOT NULL, \
198         `ChatWebsiteId` int(11) NOT NULL, \
199         `UserId` int(11) NOT NULL, \
200         PRIMARY KEY (`ChatWebsiteId`,`UserId`), \
201         KEY `UserId` (`UserId`), \
202         CONSTRAINT `user_has_chat_websites_ibfk_1` FOREIGN KEY (`ChatWebsiteId`) REFERENCES `chat_websites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, \
203         CONSTRAINT `user_has_chat_websites_ibfk_2` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
204       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
205       migration.query('INSERT INTO user_has_chat_websites (`UserId`, `ChatWebsiteId`, `createdAt`, `updatedAt`) \
206       SELECT u.id as `UserId`, a.id as `ChatWebsiteId`, NOW() as `createdAt`, NOW() as `updatedAt` \
207       FROM users as u, chat_websites as a \
208       ORDER BY u.id, a.id;');
209       // END user_has_chat_websites
210
211       // START user_has_mail_accounts
212       migration.query('CREATE TABLE `user_has_mail_accounts` ( \
213         `createdAt` datetime NOT NULL, \
214         `updatedAt` datetime NOT NULL, \
215         `MailAccountId` int(11) NOT NULL, \
216         `UserId` int(11) NOT NULL, \
217         PRIMARY KEY (`MailAccountId`,`UserId`), \
218         KEY `UserId` (`UserId`), \
219         CONSTRAINT `user_has_mail_accounts_ibfk_1` FOREIGN KEY (`MailAccountId`) REFERENCES `mail_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, \
220         CONSTRAINT `user_has_mail_accounts_ibfk_2` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
221       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
222       migration.query('INSERT INTO user_has_mail_accounts (`UserId`, `MailAccountId`, `createdAt`, `updatedAt`) \
223       SELECT u.id as `UserId`, a.id as `MailAccountId`, NOW() as `createdAt`, NOW() as `updatedAt` \
224       FROM users as u, mail_accounts as a \
225       ORDER BY u.id, a.id;');
226       // END user_has_mail_accounts
227
228       // START user_has_fax_accounts
229       migration.query('CREATE TABLE `user_has_fax_accounts` ( \
230         `createdAt` datetime NOT NULL, \
231         `updatedAt` datetime NOT NULL, \
232         `FaxAccountId` int(11) NOT NULL, \
233         `UserId` int(11) NOT NULL, \
234         PRIMARY KEY (`FaxAccountId`,`UserId`), \
235         KEY `UserId` (`UserId`), \
236         CONSTRAINT `user_has_fax_accounts_ibfk_1` FOREIGN KEY (`FaxAccountId`) REFERENCES `fax_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, \
237         CONSTRAINT `user_has_fax_accounts_ibfk_2` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
238       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
239       migration.query('INSERT INTO user_has_fax_accounts (`UserId`, `FaxAccountId`, `createdAt`, `updatedAt`) \
240       SELECT u.id as `UserId`, a.id as `FaxAccountId`, NOW() as `createdAt`, NOW() as `updatedAt` \
241       FROM users as u, fax_accounts as a \
242       ORDER BY u.id, a.id;');
243       // END user_has_fax_accounts
244
245       // START user_has_sms_accounts
246       migration.query('CREATE TABLE `user_has_sms_accounts` ( \
247         `createdAt` datetime NOT NULL, \
248         `updatedAt` datetime NOT NULL, \
249         `SmsAccountId` int(11) NOT NULL, \
250         `UserId` int(11) NOT NULL, \
251         PRIMARY KEY (`SmsAccountId`,`UserId`), \
252         KEY `UserId` (`UserId`), \
253         CONSTRAINT `user_has_sms_accounts_ibfk_1` FOREIGN KEY (`SmsAccountId`) REFERENCES `sms_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, \
254         CONSTRAINT `user_has_sms_accounts_ibfk_2` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
255       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
256       migration.query('INSERT INTO user_has_sms_accounts (`UserId`, `SmsAccountId`, `createdAt`, `updatedAt`) \
257       SELECT u.id as `UserId`, a.id as `SmsAccountId`, NOW() as `createdAt`, NOW() as `updatedAt` \
258       FROM users as u, sms_accounts as a \
259       ORDER BY u.id, a.id;');
260       // END user_has_sms_accounts
261
262       // START user_has_openchannel_accounts
263       migration.query('CREATE TABLE `user_has_openchannel_accounts` ( \
264         `createdAt` datetime NOT NULL, \
265         `updatedAt` datetime NOT NULL, \
266         `OpenchannelAccountId` int(11) NOT NULL, \
267         `UserId` int(11) NOT NULL, \
268         PRIMARY KEY (`OpenchannelAccountId`,`UserId`), \
269         KEY `UserId` (`UserId`), \
270         CONSTRAINT `user_has_openchannel_accounts_ibfk_1` FOREIGN KEY (`OpenchannelAccountId`) REFERENCES `openchannel_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, \
271         CONSTRAINT `user_has_openchannel_accounts_ibfk_2` FOREIGN KEY (`UserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE \
272       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
273       migration.query('INSERT INTO user_has_openchannel_accounts (`UserId`, `OpenchannelAccountId`, `createdAt`, `updatedAt`) \
274       SELECT u.id as `UserId`, a.id as `OpenchannelAccountId`, NOW() as `createdAt`, NOW() as `updatedAt` \
275       FROM users as u, openchannel_accounts as a \
276       ORDER BY u.id, a.id;');
277       // END user_has_openchannel_accounts
278
279       // START mail_messages
280       migration.addColumn('mail_messages', 'originTo', {
281         type: Sequelize.TEXT('long')
282       });
283
284       migration.addColumn('mail_messages', 'originCc', {
285         type: Sequelize.TEXT('long')
286       });
287       // START mail_messages
288
289       // START FINAL
290       migration.final(resolve);
291       // END FINAL
292     });
293   },
294
295   down: function(queryInterface, Sequelize) {
296     // var migration = new Migration(queryInterface);
297   }
298 };