Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.33.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
108 Migration.prototype.final = function(resolve) {
109   var _this = this;
110   this.sequence.enqueue(function() {
111     return resolve();
112   });
113 };
114
115 module.exports = {
116   up: function(queryInterface, Sequelize) {
117     return new BPromise(function(resolve, reject) {
118       var migration = new Migration(queryInterface);
119
120       // START CHATWEBSITE
121       migration.addColumn('chat_websites', 'agentAlias', {
122         type: Sequelize.STRING,
123         defaultValue: 'Agent'
124       });
125       migration.addColumn('chat_websites', 'mapKeyOffline', {
126         type: Sequelize.STRING,
127         defaultValue: 'email'
128       });
129       migration.addColumn('chat_websites', 'sitepic', {
130         type: Sequelize.TEXT
131       });
132       migration.addColumn('chat_websites', 'closingMessage', {
133         type: Sequelize.TEXT
134       });
135       migration.addColumn('chat_websites', 'closingMessageButton', {
136         type: Sequelize.STRING,
137         defaultValue: 'Send'
138       });
139       migration.addColumn('chat_websites', 'enableRating', {
140         type: Sequelize.BOOLEAN,
141         defaultValue: false
142       });
143       migration.addColumn('chat_websites', 'ratingType', {
144         type: Sequelize.ENUM('star', 'thumb'),
145         defaultValue: 'star'
146       });
147       migration.addColumn('chat_websites', 'ratingStarsNumber', {
148         type: Sequelize.INTEGER,
149         defaultValue: 5
150       });
151       migration.addColumn('chat_interactions', 'ratingValue', {
152         type: Sequelize.INTEGER
153       });
154       migration.addColumn('chat_interactions', 'ratingMessage', {
155         type: Sequelize.TEXT
156       });
157       migration.addColumn('chat_interactions', 'pathTranscript', {
158         type: Sequelize.TEXT
159       });
160       migration.addColumn('chat_interactions', 'mailTranscript', {
161         type: Sequelize.STRING
162       });
163       migration.addColumn('chat_websites', 'forwardTranscript', {
164         type: Sequelize.BOOLEAN,
165         defaultValue: false
166       });
167       migration.addColumn('chat_websites', 'forwardOffline', {
168         type: Sequelize.BOOLEAN,
169         defaultValue: false
170       });
171       migration.addColumn('chat_websites', 'forwardOfflineAddress', {
172         type: Sequelize.STRING,
173         validate: {
174           isEmail: true
175         }
176       });
177       migration.changeColumn('chat_websites', 'color', {
178         type: Sequelize.STRING,
179         allowNull: false,
180         defaultValue: '#9f946a'
181       });
182       migration.changeColumn('chat_websites', 'color_button', {
183         type: Sequelize.STRING,
184         allowNull: false,
185         defaultValue: '#9f946a'
186       });
187       // END CHATWEBSITE
188
189       // START FAX
190       migration.dropTable('fax_attachments');
191       migration.dropTable('user_has_fax_rooms');
192       migration.dropTable('fax_rooms');
193       migration.dropTable('fax_messages');
194       migration.dropTable('fax_dispositions');
195       migration.dropTable('fax_applications');
196       migration.dropTable('user_has_fax_interactions');
197       migration.dropTable('fax_interactions');
198       migration.dropTable('team_has_fax_queues');
199       migration.dropTable('user_has_fax_queues');
200       migration.dropTable('fax_queues');
201       migration.removeColumn('fax_accounts', 'fidelity');
202       migration.removeColumn('fax_accounts', 'timeout');
203       migration.removeColumn('fax_accounts', 'acceptUrl');
204       migration.removeColumn('fax_accounts', 'rejectUrl');
205       migration.removeColumn('fax_accounts', 'acceptMethod');
206       migration.removeColumn('fax_accounts', 'rejectMethod');
207       migration.removeColumn('fax_accounts', 'actions');
208       migration.removeColumn('fax_accounts', 'closeUrl');
209       migration.removeColumn('fax_accounts', 'closeMethod');
210       // END FAX
211
212       //START USER_HAS_CHAT_QUEUES
213       migration.removeColumn('user_has_chat_queues', 'assigned');
214       migration.removeColumn('user_has_chat_queues', 'queue');
215       //END USER_HAS_CHAT_QUEUES
216
217       //START USER_HAS_OPENCHANNEL_QUEUES
218       migration.removeColumn('user_has_openchannel_queues', 'assigned');
219       migration.removeColumn('user_has_openchannel_queues', 'queue');
220       //END USER_HAS_OPENCHANNEL_QUEUES
221
222       //START Priority
223       migration.addColumn('cm_hopper', 'priority', {
224         type: Sequelize.INTEGER(2).UNSIGNED,
225         defaultValue: 2
226       });
227       migration.addIndex('cm_hopper', ['priority'], 'priority');
228       //END Priority
229
230       //START sms_messages
231       migration.dropTable('sms_messages');
232       //START sms_messages
233
234       // START FINAL
235       migration.final(resolve);
236       // END FINAL
237     });
238   },
239
240   down: function(queryInterface, Sequelize) {
241     var migration = new Migration(queryInterface);
242   }
243 };