Built motion from commit e6806ed6.|2.6.0
[motion2.git] / server / migrations / 2.6.0.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
67       .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
83       .removeIndex(table, indexName)
84       .then(function() {
85         logger.info('removeIndex %s %s', table, indexName);
86       })
87       .catch(function(err) {
88         logger.info(JSON.stringify(err));
89       });
90   });
91 };
92
93 Migration.prototype.query = function(sql) {
94   var _this = this;
95   this.sequence.enqueue(function() {
96     return _this.queryInterface.sequelize
97       .query(sql)
98       .then(function() {
99         logger.info('query %s', sql);
100       })
101       .catch(function(err) {
102         logger.info(JSON.stringify(err));
103       });
104   });
105 };
106
107 Migration.prototype.removeColumn = function(table, column) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return _this.queryInterface
111       .removeColumn(table, column)
112       .then(function() {
113         logger.info('Removed column %s from %s', column, table);
114       })
115       .catch(function(err) {
116         logger.info(
117           util.inspect(err, {
118             showHidden: false,
119             depth: null,
120           })
121         );
122       });
123   });
124 };
125
126 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
127   var _this = this;
128   this.sequence.enqueue(function() {
129     return _this.queryInterface
130       .renameColumn(table, oldColumn, newColumn)
131       .then(function() {
132         logger.info(
133           'Renamed column from %s to %s on %s',
134           oldColumn,
135           newColumn,
136           table
137         );
138       })
139       .catch(function(err) {
140         logger.info(
141           util.inspect(err, {
142             showHidden: false,
143             depth: null,
144           })
145         );
146       });
147   });
148 };
149
150 Migration.prototype.final = function(resolve) {
151   this.sequence.enqueue(function() {
152     return resolve();
153   });
154 };
155
156 module.exports = {
157   up: function(queryInterface, Sequelize) {
158     return new BPromise(function(resolve) {
159       var migration = new Migration(queryInterface);
160
161       // START tools_contact_item_types
162       migration.query(
163         "CREATE TABLE `tools_contact_item_types` (" +
164         "  `id` INT AUTO_INCREMENT NOT NULL," +
165         "  `name` varchar(255) NOT NULL," +
166         "  `OrderBy` INT NULL," +
167         "  `rgbBackgroundColor` varchar(20) NULL," +
168         "  `createdAt` datetime NOT NULL," +
169         "  `updatedAt` datetime NOT NULL," +
170         "  UNIQUE  INDEX `item_type_id` (id) " +
171         ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"
172       );
173       // END tools_contact_item_types
174
175       // START cm_contact_has_items
176       migration.query(
177         "CREATE TABLE `cm_contact_has_items` (" +
178         "  `CmContactId` INT NOT NULL," +
179         "  `item` varchar(255) NOT NULL," +
180         "  `ItemTypeId` INT NULL," +
181         "  `ItemClass` VARCHAR(20) NOT NULL," +
182         "  `OrderBy` INT NULL," +
183         "  `description` varchar(255) DEFAULT NULL," +
184         "  `createdAt` datetime NOT NULL," +
185         "  `updatedAt` datetime NOT NULL," +
186         "  UNIQUE  INDEX `contact_item` (CmContactId,item), " +
187         "  CONSTRAINT `cm_contact_has_items_ibfk_1` FOREIGN KEY (`CmContactId`) REFERENCES `cm_contacts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, " +
188         "  CONSTRAINT `cm_contact_has_items_ibfk_2` FOREIGN KEY (`ItemTypeId`) REFERENCES `tools_contact_item_types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE" +
189         ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"
190       );
191       // END cm_contact_has_items
192
193       // START cm_hopper_additional_phones
194       migration.query(
195         "CREATE TABLE `cm_hopper_additional_phones` (" +
196         "  `id` INT AUTO_INCREMENT NOT NULL," +
197         "  `CmHopperId` INT NOT NULL," +
198         "  `phone` varchar(255) NOT NULL," +
199         "  `OrderBy` INT NULL default 0," +
200         "  `scheduledat` datetime NULL," +
201         "  `countbusyretry` INT NULL default 0," +
202         "  `countcongestionretry` INT NULL default 0," +
203         "  `countnoanswerretry` INT NULL default 0," +
204         "  `countnosuchnumberretry` INT NULL default 0," +
205         "  `countdropretry` INT NULL default 0," +
206         "  `countabandonedretry` INT NULL default 0," +
207         "  `countmachineretry` INT NULL default 0," +
208         "  `countagentrejectretry` INT NULL default 0," +
209         "  `createdAt` datetime NOT NULL," +
210         "  `updatedAt` datetime NOT NULL," +
211         "  UNIQUE  INDEX `hopper_additional_phone_id` (id), " +
212         "  INDEX `cm_hopper_phone_item` (CmHopperId), " +
213         "  CONSTRAINT `cm_hopper_phone_item1` FOREIGN KEY (`CmHopperId`) REFERENCES `cm_hopper` (`id`) ON DELETE CASCADE ON UPDATE CASCADE " +
214         ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"
215       );
216       // END cm_hopper_additional_phones
217
218       // START cm_hopper
219       migration.addColumn("cm_hopper", "OrderBy", {
220         type: Sequelize.INTEGER,
221         allowNull: true,
222         defaultValue: 0
223       });
224       // END cm_hopper
225
226       // START cm_contacts
227       migration.addColumn("cm_contacts", "AdditionalPhones", {
228         type: Sequelize.STRING,
229         allowNull: true
230       });
231       // START cm_contacts
232
233       // START voice_queues
234       migration.addColumn("voice_queues", "callAdditionalPhoneAfterMin", {
235         type: Sequelize.INTEGER,
236         defaultValue: 3
237       });
238       // END voice_queues
239
240       // START FINAL
241       migration.final(resolve);
242       // END FINAL
243     });
244   },
245
246   down: function(queryInterface, Sequelize) {
247     // var migration = new Migration(queryInterface);
248   },
249 };