3 var BPromise = require('bluebird');
4 var fs = require('fs');
5 var util = require('util');
6 var _ = require('lodash');
8 var logger = require('../config/logger')('migration');
10 var Sequence = function() {};
12 Sequence.prototype.enqueue = function(fn) {
13 this.tail = this.tail ? this.tail.finally(fn) : fn();
16 var Migration = function(queryInterface) {
17 this.queryInterface = queryInterface;
18 this.sequence = new Sequence();
21 Migration.prototype.changeColumn = function(table, column, type) {
23 this.sequence.enqueue(function() {
24 return _this.queryInterface
25 .changeColumn(table, column, type)
27 logger.info('Changed column %s in table %s', column, table);
29 .catch(function(err) {
30 logger.info(JSON.stringify(err));
35 Migration.prototype.addColumn = function(table, column, type) {
37 this.sequence.enqueue(function() {
38 return _this.queryInterface
39 .addColumn(table, column, type)
41 logger.info('Added column %s to %s', column, table);
43 .catch(function(err) {
44 logger.info(JSON.stringify(err));
49 Migration.prototype.dropTable = function(table) {
51 this.sequence.enqueue(function() {
52 return _this.queryInterface
57 logger.info('table dropped %s', table);
59 .catch(function(err) {
60 logger.info(JSON.stringify(err));
65 Migration.prototype.addIndex = function(table, column, indexName) {
67 this.sequence.enqueue(function() {
68 return _this.queryInterface.addIndex(table, column, {
72 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
74 .catch(function(err) {
75 logger.info(JSON.stringify(err));
80 Migration.prototype.query = function(sql) {
82 this.sequence.enqueue(function() {
83 return _this.queryInterface.sequelize.query(sql)
85 logger.info('query %s', sql);
87 .catch(function(err) {
88 logger.info(JSON.stringify(err));
93 Migration.prototype.removeColumn = function(table, column) {
95 this.sequence.enqueue(function() {
96 return _this.queryInterface.removeColumn(table, column)
98 logger.info('Removed column %s from %s', column, table);
100 .catch(function(err) {
101 logger.info(util.inspect(err, {
109 Migration.prototype.final = function(resolve) {
111 this.sequence.enqueue(function() {
117 up: function(queryInterface, Sequelize) {
118 return new BPromise(function(resolve, reject) {
120 var migration = new Migration(queryInterface);
123 migration.addColumn('settings', 'callRecordingEncryption', {
124 type: Sequelize.BOOLEAN,
127 migration.addColumn('settings', 'passwordExpiresDays', {
128 type: Sequelize.INTEGER(11),
131 migration.query('UPDATE settings SET securePassword=false WHERE id=1');
134 //START CANNED ANSWERS
135 migration.addColumn('tools_canned_answers', 'MailAccountId', {
136 type: Sequelize.INTEGER
138 migration.addColumn('tools_canned_answers', 'FaxAccountId', {
139 type: Sequelize.INTEGER
141 migration.addColumn('tools_canned_answers', 'SmsAccountId', {
142 type: Sequelize.INTEGER
144 migration.addColumn('tools_canned_answers', 'OpenchannelAccountId', {
145 type: Sequelize.INTEGER
147 migration.addColumn('tools_canned_answers', 'ChatWebsiteId', {
148 type: Sequelize.INTEGER
151 migration.query('ALTER TABLE `tools_canned_answers` \
152 ADD CONSTRAINT `tools_canned_answers_ibfk_1` \
153 FOREIGN KEY (`MailAccountId`) \
154 REFERENCES mail_accounts(`id`) \
157 migration.query('ALTER TABLE `tools_canned_answers` \
158 ADD CONSTRAINT `tools_canned_answers_ibfk_2` \
159 FOREIGN KEY (`FaxAccountId`) \
160 REFERENCES fax_accounts(`id`) \
163 migration.query('ALTER TABLE `tools_canned_answers` \
164 ADD CONSTRAINT `tools_canned_answers_ibfk_3` \
165 FOREIGN KEY (`SmsAccountId`) \
166 REFERENCES sms_accounts(`id`) \
169 migration.query('ALTER TABLE `tools_canned_answers` \
170 ADD CONSTRAINT `tools_canned_answers_ibfk_4` \
171 FOREIGN KEY (`OpenchannelAccountId`) \
172 REFERENCES openchannel_accounts(`id`) \
176 migration.query('ALTER TABLE `tools_canned_answers` \
177 ADD CONSTRAINT `tools_canned_answers_ibfk_5` \
178 FOREIGN KEY (`ChatWebsiteId`) \
179 REFERENCES chat_websites(`id`) \
182 //START CANNED ANSWERS
185 migration.addColumn('users', 'passwordResetAt', {
186 type: Sequelize.DATE,
189 migration.query('UPDATE users SET passwordResetAt=NOW()');
193 migration.addColumn('report_dial', 'duration', {
194 type: Sequelize.INTEGER
196 migration.addColumn('report_dial', 'holdtime', {
197 type: Sequelize.INTEGER
199 migration.addColumn('report_dial', 'billableseconds', {
200 type: Sequelize.INTEGER
205 migration.changeColumn('sms_accounts', 'type', {
206 type: Sequelize.ENUM('twilio', 'skebby', 'connectel', 'clicksend')
210 //START CUSTOM DASHBOARDS
211 migration.addColumn('dashboard_items', 'background', {
212 type: Sequelize.STRING,
214 defaultValue: '#ffffff',
220 migration.addColumn('dashboard_items', 'foreground', {
221 type: Sequelize.STRING,
223 defaultValue: '#2196f3',
229 migration.addColumn('dashboard_items', 'link', {
230 type: Sequelize.STRING
232 //END CUSTOM DASHBOARDS
235 migration.final(resolve);
240 down: function(queryInterface, Sequelize) {
241 var migration = new Migration(queryInterface);