3 var BPromise = require('bluebird');
5 var logger = require('../config/logger')('migration');
6 var util = require('util');
8 var Sequence = function() {};
10 Sequence.prototype.enqueue = function(fn) {
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
14 var Migration = function(queryInterface) {
15 this.queryInterface = queryInterface;
16 this.sequence = new Sequence();
19 Migration.prototype.changeColumn = function(table, column, type) {
21 this.sequence.enqueue(function() {
22 return _this.queryInterface
23 .changeColumn(table, column, type)
25 logger.info('Changed column %s in table %s', column, table);
27 .catch(function(err) {
28 logger.info(JSON.stringify(err));
33 Migration.prototype.addColumn = function(table, column, type) {
35 this.sequence.enqueue(function() {
36 return _this.queryInterface
37 .addColumn(table, column, type)
39 logger.info('Added column %s to %s', column, table);
41 .catch(function(err) {
42 logger.info(JSON.stringify(err));
47 Migration.prototype.dropTable = function(table) {
49 this.sequence.enqueue(function() {
50 return _this.queryInterface
55 logger.info('table dropped %s', table);
57 .catch(function(err) {
58 logger.info(JSON.stringify(err));
63 Migration.prototype.addIndex = function(table, column, indexName) {
65 this.sequence.enqueue(function() {
66 return _this.queryInterface.addIndex(table, column, {
70 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
72 .catch(function(err) {
73 logger.info(JSON.stringify(err));
78 Migration.prototype.query = function(sql) {
80 this.sequence.enqueue(function() {
81 return _this.queryInterface.sequelize.query(sql)
83 logger.info('query %s', sql);
85 .catch(function(err) {
86 logger.info(JSON.stringify(err));
91 Migration.prototype.removeColumn = function(table, column) {
93 this.sequence.enqueue(function() {
94 return _this.queryInterface.removeColumn(table, column)
96 logger.info('Removed column %s from %s', column, table);
98 .catch(function(err) {
99 logger.info(util.inspect(err, {
107 Migration.prototype.final = function(resolve) {
109 this.sequence.enqueue(function() {
115 up: function(queryInterface, Sequelize) {
116 return new BPromise(function(resolve, reject) {
117 var migration = new Migration(queryInterface);
120 migration.addColumn('chat_websites', 'closingQuestion', {
121 type: Sequelize.STRING,
122 defaultValue: 'Do you want to close the interaction?'
126 // START CHAT INTERACTIONS
127 migration.addColumn('chat_interactions', 'read1stAt', {
130 // END CHAT INTERACTIONS
132 // START CHAT MESSAGES
133 migration.addColumn('chat_messages', 'readAt', {
138 // START FAX INTERACTIONS
139 migration.addColumn('fax_interactions', 'read1stAt', {
142 // END FAX INTERACTIONS
144 // START FAX MESSAGES
145 migration.addColumn('fax_messages', 'readAt', {
150 // START OPENCHANNEL INTERACTIONS
151 migration.addColumn('openchannel_interactions', 'read1stAt', {
154 migration.addColumn('openchannel_interactions', 'threadId', {
155 type: Sequelize.STRING
157 migration.addColumn('openchannel_interactions', 'externalUrl', {
158 type: Sequelize.STRING
160 // END OPENCHANNEL INTERACTIONS
162 // START OPENCHANNEL MESSAGES
163 migration.addColumn('openchannel_messages', 'readAt', {
166 // END OPENCHANNEL MESSAGES
168 // START MAIL INTERACTIONS
169 migration.addColumn('mail_interactions', 'read1stAt', {
172 migration.addColumn('mail_interactions', 'substatus', {
173 type: Sequelize.STRING(60)
175 migration.addColumn('mail_interactions', 'substatusAt', {
178 // END MAIL INTERACTIONS
180 // START MAIL MESSAGES
181 migration.addColumn('mail_messages', 'readAt', {
186 // START SMS INTERACTIONS
187 migration.addColumn('sms_interactions', 'read1stAt', {
190 // END SMS INTERACTIONS
192 // START SMS MESSAGES
193 migration.addColumn('sms_messages', 'readAt', {
198 // START DISPOSITIONS
199 migration.changeColumn('chat_dispositions', 'name', {
200 type: Sequelize.STRING,
201 unique: 'nameAndAccount',
204 migration.changeColumn('fax_dispositions', 'name', {
205 type: Sequelize.STRING,
206 unique: 'nameAndAccount',
209 migration.changeColumn('openchannel_dispositions', 'name', {
210 type: Sequelize.STRING,
211 unique: 'nameAndAccount',
214 migration.changeColumn('sms_dispositions', 'name', {
215 type: Sequelize.STRING,
216 unique: 'nameAndAccount',
219 migration.changeColumn('voice_dispositions', 'name', {
220 type: Sequelize.STRING,
221 unique: 'nameAndAccount',
224 migration.query('ALTER TABLE chat_dispositions add UNIQUE INDEX nameAndAccount (name, ChatAccountId)');
225 migration.query('ALTER TABLE fax_dispositions add UNIQUE INDEX nameAndAccount (name, FaxAccountId)');
226 migration.query('ALTER TABLE openchannel_dispositions add UNIQUE INDEX nameAndAccount (name, OpenchannelAccountId)');
227 migration.query('ALTER TABLE sms_dispositions add UNIQUE INDEX nameAndAccount (name, SmsAccountId)');
228 migration.query('ALTER TABLE voice_dispositions add UNIQUE INDEX nameAndAccount (name, ListId)');
229 migration.changeColumn('mail_dispositions', 'name', {
230 type: Sequelize.STRING,
231 unique: 'nameAndAccount',
234 migration.query('ALTER TABLE mail_dispositions DROP INDEX name');
235 migration.query('ALTER TABLE mail_dispositions add UNIQUE INDEX nameAndAccount (name, MailAccountId)');
239 migration.final(resolve);
244 down: function(queryInterface, Sequelize) {
245 var migration = new Migration(queryInterface);