Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.44.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4
5 var logger = require('../config/logger')('migration');
6 var util = require('util');
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(res) {
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(res) {
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(res) {
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(res) {
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(res) {
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.final = function(resolve) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return resolve();
111   });
112 };
113
114 module.exports = {
115   up: function(queryInterface, Sequelize) {
116     return new BPromise(function(resolve, reject) {
117       var migration = new Migration(queryInterface);
118
119       // START CHATWEBSITE
120       migration.addColumn('chat_websites', 'closingQuestion', {
121         type: Sequelize.STRING,
122         defaultValue: 'Do you want to close the interaction?'
123       });
124       // END CHATWEBSITE
125
126       // START CHAT INTERACTIONS
127       migration.addColumn('chat_interactions', 'read1stAt', {
128         type: Sequelize.DATE
129       });
130       // END CHAT INTERACTIONS
131
132       // START CHAT MESSAGES
133       migration.addColumn('chat_messages', 'readAt', {
134         type: Sequelize.DATE
135       });
136       // END CHAT MESSAGES
137
138       // START FAX INTERACTIONS
139       migration.addColumn('fax_interactions', 'read1stAt', {
140         type: Sequelize.DATE
141       });
142       // END FAX INTERACTIONS
143
144       // START FAX MESSAGES
145       migration.addColumn('fax_messages', 'readAt', {
146         type: Sequelize.DATE
147       });
148       // END FAX MESSAGES
149
150       // START OPENCHANNEL INTERACTIONS
151       migration.addColumn('openchannel_interactions', 'read1stAt', {
152         type: Sequelize.DATE
153       });
154       migration.addColumn('openchannel_interactions', 'threadId', {
155         type: Sequelize.STRING
156       });
157       migration.addColumn('openchannel_interactions', 'externalUrl', {
158         type: Sequelize.STRING
159       });
160       // END OPENCHANNEL INTERACTIONS
161
162       // START OPENCHANNEL MESSAGES
163       migration.addColumn('openchannel_messages', 'readAt', {
164         type: Sequelize.DATE
165       });
166       // END OPENCHANNEL MESSAGES
167
168       // START MAIL INTERACTIONS
169       migration.addColumn('mail_interactions', 'read1stAt', {
170         type: Sequelize.DATE
171       });
172       migration.addColumn('mail_interactions', 'substatus', {
173         type: Sequelize.STRING(60)
174       });
175       migration.addColumn('mail_interactions', 'substatusAt', {
176         type: Sequelize.DATE
177       });
178       // END MAIL INTERACTIONS
179
180       // START MAIL MESSAGES
181       migration.addColumn('mail_messages', 'readAt', {
182         type: Sequelize.DATE
183       });
184       // END MAIL MESSAGES
185
186       // START SMS INTERACTIONS
187       migration.addColumn('sms_interactions', 'read1stAt', {
188         type: Sequelize.DATE
189       });
190       // END SMS INTERACTIONS
191
192       // START SMS MESSAGES
193       migration.addColumn('sms_messages', 'readAt', {
194         type: Sequelize.DATE
195       });
196       // END SMS MESSAGES
197
198       // START DISPOSITIONS
199       migration.changeColumn('chat_dispositions', 'name', {
200         type: Sequelize.STRING,
201         unique: 'nameAndAccount',
202         allowNull: false
203       });
204       migration.changeColumn('fax_dispositions', 'name', {
205         type: Sequelize.STRING,
206         unique: 'nameAndAccount',
207         allowNull: false
208       });
209       migration.changeColumn('openchannel_dispositions', 'name', {
210         type: Sequelize.STRING,
211         unique: 'nameAndAccount',
212         allowNull: false
213       });
214       migration.changeColumn('sms_dispositions', 'name', {
215         type: Sequelize.STRING,
216         unique: 'nameAndAccount',
217         allowNull: false
218       });
219       migration.changeColumn('voice_dispositions', 'name', {
220         type: Sequelize.STRING,
221         unique: 'nameAndAccount',
222         allowNull: false
223       });
224       migration.query('ALTER TABLE chat_dispositions add UNIQUE INDEX nameAndAccount (name, ChatAccountId)');
225       migration.query('ALTER TABLE fax_dispositions add UNIQUE INDEX nameAndAccount (name, FaxAccountId)');
226       migration.query('ALTER TABLE openchannel_dispositions add UNIQUE INDEX nameAndAccount (name, OpenchannelAccountId)');
227       migration.query('ALTER TABLE sms_dispositions add UNIQUE INDEX nameAndAccount (name, SmsAccountId)');
228       migration.query('ALTER TABLE voice_dispositions add UNIQUE INDEX nameAndAccount (name, ListId)');
229       migration.changeColumn('mail_dispositions', 'name', {
230         type: Sequelize.STRING,
231         unique: 'nameAndAccount',
232         allowNull: false
233       });
234       migration.query('ALTER TABLE mail_dispositions DROP INDEX name');
235       migration.query('ALTER TABLE mail_dispositions add UNIQUE INDEX nameAndAccount (name, MailAccountId)');
236       // END DISPOSITIONS
237
238       // START FINAL
239       migration.final(resolve);
240       // END FINAL
241     });
242   },
243
244   down: function(queryInterface, Sequelize) {
245     var migration = new Migration(queryInterface);
246   }
247 };