3 var BPromise = require('bluebird');
4 var rs = require('randomstring');
5 var util = require('util');
7 var logger = require('../config/logger')('migration');
9 var Sequence = function() {};
11 Sequence.prototype.enqueue = function(fn) {
12 this.tail = this.tail ? this.tail.finally(fn) : fn();
15 var Migration = function(queryInterface) {
16 this.queryInterface = queryInterface;
17 this.sequence = new Sequence();
20 Migration.prototype.changeColumn = function(table, column, type) {
22 this.sequence.enqueue(function() {
23 return _this.queryInterface
24 .changeColumn(table, column, type)
26 logger.info('Changed column %s in table %s', column, table);
28 .catch(function(err) {
29 logger.info(JSON.stringify(err));
34 Migration.prototype.addColumn = function(table, column, type) {
36 this.sequence.enqueue(function() {
37 return _this.queryInterface
38 .addColumn(table, column, type)
40 logger.info('Added column %s to %s', column, table);
42 .catch(function(err) {
43 logger.info(JSON.stringify(err));
48 Migration.prototype.dropTable = function(table) {
50 this.sequence.enqueue(function() {
51 return _this.queryInterface
56 logger.info('table dropped %s', table);
58 .catch(function(err) {
59 logger.info(JSON.stringify(err));
64 Migration.prototype.addIndex = function(table, column, indexName) {
66 this.sequence.enqueue(function() {
67 return _this.queryInterface.addIndex(table, column, {
71 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
73 .catch(function(err) {
74 logger.info(JSON.stringify(err));
79 Migration.prototype.query = function(sql) {
81 this.sequence.enqueue(function() {
82 return _this.queryInterface.sequelize.query(sql)
84 logger.info('query %s', sql);
86 .catch(function(err) {
87 logger.info(JSON.stringify(err));
92 Migration.prototype.removeColumn = function(table, column) {
94 this.sequence.enqueue(function() {
95 return _this.queryInterface.removeColumn(table, column)
97 logger.info('Removed column %s from %s', column, table);
99 .catch(function(err) {
100 logger.info(util.inspect(err, {
108 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
110 this.sequence.enqueue(function() {
111 return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
113 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
115 .catch(function(err) {
116 logger.info(util.inspect(err, {
124 Migration.prototype.final = function(resolve) {
125 this.sequence.enqueue(function() {
131 up: function(queryInterface, Sequelize) {
132 return new BPromise(function(resolve) {
133 var migration = new Migration(queryInterface);
135 // START CHAT WEBSITE
136 migration.addColumn('chat_websites', 'messagesAlignment', {
137 type: Sequelize.STRING,
138 defaultValue: 'alternate'
141 migration.addColumn('chat_websites', 'defaultTitle', {
142 type: Sequelize.STRING
145 migration.addColumn('chat_websites', 'customerAvatar', {
149 migration.addColumn('chat_websites', 'showCustomerAvatar', {
150 type: Sequelize.BOOLEAN,
155 //START TAG APPLICATION
157 migration.addColumn('chat_applications', 'TagId', {
158 type: Sequelize.INTEGER
162 'ALTER TABLE `chat_applications` \
163 ADD CONSTRAINT `chat_applications_ibfk_5` \
164 FOREIGN KEY (`TagId`) \
165 REFERENCES tools_tags(`id`) \
169 migration.addColumn('fax_applications', 'TagId', {
170 type: Sequelize.INTEGER
174 'ALTER TABLE `fax_applications` \
175 ADD CONSTRAINT `fax_applications_ibfk_5` \
176 FOREIGN KEY (`TagId`) \
177 REFERENCES tools_tags(`id`) \
181 migration.addColumn('mail_applications', 'TagId', {
182 type: Sequelize.INTEGER
186 'ALTER TABLE `mail_applications` \
187 ADD CONSTRAINT `mail_applications_ibfk_5` \
188 FOREIGN KEY (`TagId`) \
189 REFERENCES tools_tags(`id`) \
193 migration.addColumn('openchannel_applications', 'TagId', {
194 type: Sequelize.INTEGER
198 'ALTER TABLE `openchannel_applications` \
199 ADD CONSTRAINT `openchannel_applications_ibfk_5` \
200 FOREIGN KEY (`TagId`) \
201 REFERENCES tools_tags(`id`) \
205 migration.addColumn('sms_applications', 'TagId', {
206 type: Sequelize.INTEGER
210 'ALTER TABLE `sms_applications` \
211 ADD CONSTRAINT `sms_applications_ibfk_5` \
212 FOREIGN KEY (`TagId`) \
213 REFERENCES tools_tags(`id`) \
217 //END TAG APPLICATION
220 migration.query('ALTER TABLE report_dial CHANGE `hodltime` `holdtime` INT(11) DEFAULT NULL');
223 //START OPENCHANNEL TOKEN
224 migration.addColumn('openchannel_accounts', 'token', {
225 type: Sequelize.STRING
227 //END OPENCHANNEL TOKEN
229 // START VOICE QUEUE LOG
230 migration.addColumn('voice_queues_log', 'data', {
231 type: Sequelize.STRING,
235 // END VOICE QUEUE LOG
237 // START CUSTOM DASHBOARD WIDGET
238 migration.changeColumn('dashboard_items', 'title', {
239 type: Sequelize.STRING,
242 // END CUSTOM DASHBOARD WIDGET
245 migration.final(resolve);
250 down: function(queryInterface, Sequelize) {
251 // var migration = new Migration(queryInterface);