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.renameColumn = function(table, oldColumn, newColumn) {
111 this.sequence.enqueue(function() {
112 return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
113 .then(function(res) {
114 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
116 .catch(function(err) {
117 logger.info(util.inspect(err, {
125 Migration.prototype.final = function(resolve) {
127 this.sequence.enqueue(function() {
133 up: function(queryInterface, Sequelize) {
134 return new BPromise(function(resolve, reject) {
136 var migration = new Migration(queryInterface);
139 migration.changeColumn('voice_queues', 'strategy', {
140 type: Sequelize.ENUM('roundrobin', 'ringall', 'leastrecent', 'fewestcalls', 'random', 'rrmemory', 'linear', 'wrandom', 'rrordered'),
142 defaultValue: 'ringall'
146 // START EXTRACTED REPORT
147 migration.addColumn('analytics_extracted_reports', 'basename', {
148 type: Sequelize.STRING,
151 // END EXTRACTED REPORT
153 // START MAIL ACCOUNT
154 migration.addColumn('mail_accounts', 'notificationTemplate', {
157 migration.addColumn('mail_accounts', 'notificationSound', {
158 type: Sequelize.BOOLEAN,
161 migration.addColumn('mail_accounts', 'notificationShake', {
162 type: Sequelize.BOOLEAN,
167 // START CHAT ACCOUNT
168 migration.addColumn('chat_websites', 'notificationTemplate', {
171 migration.addColumn('chat_websites', 'notificationSound', {
172 type: Sequelize.BOOLEAN,
175 migration.addColumn('chat_websites', 'notificationShake', {
176 type: Sequelize.BOOLEAN,
182 migration.addColumn('fax_accounts', 'notificationTemplate', {
185 migration.addColumn('fax_accounts', 'notificationSound', {
186 type: Sequelize.BOOLEAN,
189 migration.addColumn('fax_accounts', 'notificationShake', {
190 type: Sequelize.BOOLEAN,
196 migration.addColumn('sms_accounts', 'notificationTemplate', {
199 migration.addColumn('sms_accounts', 'notificationSound', {
200 type: Sequelize.BOOLEAN,
203 migration.addColumn('sms_accounts', 'notificationShake', {
204 type: Sequelize.BOOLEAN,
209 // START OPENCHANNEL ACCOUNT
210 migration.addColumn('openchannel_accounts', 'notificationTemplate', {
213 migration.addColumn('openchannel_accounts', 'notificationSound', {
214 type: Sequelize.BOOLEAN,
217 migration.addColumn('openchannel_accounts', 'notificationShake', {
218 type: Sequelize.BOOLEAN,
221 // END OPENCHANNEL ACCOUNT
223 // START ACCOUNTS UPDATE
224 migration.query('UPDATE chat_websites SET notificationTemplate=\'Account: {{account.name}}<br/>{{#queue}}Queue: {{queue.name}}<br/>{{/queue}}From : {{from}}\'');
225 migration.query('UPDATE mail_accounts SET notificationTemplate=\'Account: {{account.name}}<br/>{{#queue}}Queue: {{queue.name}}<br/>{{/queue}}From : {{from}}<br/>Subject : {{message.subject}}\'');
226 migration.query('UPDATE sms_accounts SET notificationTemplate=\'Account: {{account.name}}<br/>{{#queue}}Queue: {{queue.name}}<br/>{{/queue}}From : {{from}}\'');
227 migration.query('UPDATE fax_accounts SET notificationTemplate=\'Account: {{account.name}}<br/>{{#queue}}Queue: {{queue.name}}<br/>{{/queue}}From : {{from}}\'');
228 migration.query('UPDATE openchannel_accounts SET notificationTemplate=\'Account: {{account.name}}<br/>{{#queue}}Queue: {{queue.name}}<br/>{{/queue}}From : {{from}}\'');
229 // END ACCOUNTS UPDATE
232 migration.final(resolve);
237 down: function(queryInterface, Sequelize) {
238 var migration = new Migration(queryInterface);