3 var BPromise = require('bluebird');
4 var fs = require('fs');
5 var path = require('path');
6 var util = require('util');
7 var _ = require('lodash');
9 var logger = require('../config/logger')('migration');
11 var Sequence = function() {};
13 Sequence.prototype.enqueue = function(fn) {
14 this.tail = this.tail ? this.tail.finally(fn) : fn();
17 var Migration = function(queryInterface) {
18 this.queryInterface = queryInterface;
19 this.sequence = new Sequence();
22 Migration.prototype.changeColumn = function(table, column, type) {
24 this.sequence.enqueue(function() {
25 return _this.queryInterface
26 .changeColumn(table, column, type)
28 logger.info('Changed column %s in table %s', column, table);
30 .catch(function(err) {
31 logger.info(JSON.stringify(err));
36 Migration.prototype.addColumn = function(table, column, type) {
38 this.sequence.enqueue(function() {
39 return _this.queryInterface
40 .addColumn(table, column, type)
42 logger.info('Added column %s to %s', column, table);
44 .catch(function(err) {
45 logger.info(JSON.stringify(err));
50 Migration.prototype.dropTable = function(table) {
52 this.sequence.enqueue(function() {
53 return _this.queryInterface
58 logger.info('table dropped %s', table);
60 .catch(function(err) {
61 logger.info(JSON.stringify(err));
66 Migration.prototype.addIndex = function(table, column, indexName) {
68 this.sequence.enqueue(function() {
69 return _this.queryInterface.addIndex(table, column, {
73 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
75 .catch(function(err) {
76 logger.info(JSON.stringify(err));
81 Migration.prototype.query = function(sql) {
83 this.sequence.enqueue(function() {
84 return _this.queryInterface.sequelize.query(sql)
86 logger.info('query %s', sql);
88 .catch(function(err) {
89 logger.info(JSON.stringify(err));
94 Migration.prototype.removeColumn = function(table, column) {
96 this.sequence.enqueue(function() {
97 return _this.queryInterface.removeColumn(table, column)
99 logger.info('Removed column %s from %s', column, table);
101 .catch(function(err) {
102 logger.info(util.inspect(err, {
110 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
112 this.sequence.enqueue(function() {
113 return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
114 .then(function(res) {
115 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
117 .catch(function(err) {
118 logger.info(util.inspect(err, {
126 Migration.prototype.final = function(resolve) {
128 this.sequence.enqueue(function() {
134 up: function(queryInterface, Sequelize) {
135 return new BPromise(function(resolve, reject) {
137 var migration = new Migration(queryInterface);
140 migration.addColumn('settings', 'phoneBarAutoUpdater', {
141 type: Sequelize.BOOLEAN,
144 migration.addColumn('settings', 'phoneBarAutoUpdaterUrl', {
145 type: Sequelize.STRING,
146 defaultValue: 'https://www.xcally.com/bar/phonebar/autoupdater.xml'
151 migration.addColumn('campaigns', 'dialPrefix', {
152 type: Sequelize.STRING
157 migration.addColumn('cm_lists', 'dialPrefix', {
158 type: Sequelize.STRING
163 migration.addColumn('users', 'alias', {
164 type: Sequelize.STRING
169 migration.addColumn('cm_contacts', 'skype', {
170 type: Sequelize.STRING
172 migration.addColumn('cm_contacts', 'UserId', {
173 type: Sequelize.INTEGER
175 migration.addColumn('cm_contacts', 'priority', {
176 type: Sequelize.INTEGER(2).UNSIGNED,
183 migration.addColumn('cm_contacts', 'scheduledat', {
184 type: Sequelize.DATE,
185 defaultValue: Sequelize.NOW
189 // START MAIL INTERACTION
190 migration.addColumn('mail_interactions', 'openedBy', {
191 type: Sequelize.STRING,
194 // END MAIL INTERACTION
196 // START CHAT INTERACTION
197 migration.addColumn('chat_interactions', 'openedBy', {
198 type: Sequelize.STRING,
201 // END CHAT INTERACTION
203 // START SMS INTERACTION
204 migration.addColumn('sms_interactions', 'openedBy', {
205 type: Sequelize.STRING,
208 // END SMS INTERACTION
210 // START OPENCHANNEL INTERACTION
211 migration.addColumn('openchannel_interactions', 'openedBy', {
212 type: Sequelize.STRING,
215 // END OPENCHANNEL INTERACTION
217 // START FAX INTERACTION
218 migration.addColumn('fax_interactions', 'openedBy', {
219 type: Sequelize.STRING,
222 // END FAX INTERACTION
225 migration.query('DELETE FROM `voice_voicemail_messages` WHERE `mailboxuser` NOT IN (SELECT `mailbox` FROM `voice_voicemail`)');
227 migration.query('ALTER TABLE `voice_voicemail_messages` \
228 ADD CONSTRAINT `voice_voicemail_messages_ibfk_1` \
229 FOREIGN KEY (`mailboxuser`) \
230 REFERENCES voice_voicemail(`mailbox`) \
236 migration.final(resolve);
241 down: function(queryInterface, Sequelize) {
242 var migration = new Migration(queryInterface);