3 var BPromise = require('bluebird');
4 var util = require('util');
5 var logger = require('../config/logger')('migration');
6 var Sequence = function() {};
8 Sequence.prototype.enqueue = function(fn) {
9 this.tail = this.tail ? this.tail.finally(fn) : fn();
12 var Migration = function(queryInterface) {
13 this.queryInterface = queryInterface;
14 this.sequence = new Sequence();
17 Migration.prototype.changeColumn = function(table, column, type) {
19 this.sequence.enqueue(function() {
20 return _this.queryInterface
21 .changeColumn(table, column, type)
23 logger.info('Changed column %s in table %s', column, table);
25 .catch(function(err) {
26 logger.info(JSON.stringify(err));
31 Migration.prototype.addColumn = function(table, column, type) {
33 this.sequence.enqueue(function() {
34 return _this.queryInterface
35 .addColumn(table, column, type)
37 logger.info('Added column %s to %s', column, table);
39 .catch(function(err) {
40 logger.info(JSON.stringify(err));
45 Migration.prototype.dropTable = function(table) {
47 this.sequence.enqueue(function() {
48 return _this.queryInterface
53 logger.info('table dropped %s', table);
55 .catch(function(err) {
56 logger.info(JSON.stringify(err));
61 Migration.prototype.addIndex = function(table, column, indexName) {
63 this.sequence.enqueue(function() {
64 return _this.queryInterface
65 .addIndex(table, column, {
69 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
71 .catch(function(err) {
72 logger.info(JSON.stringify(err));
77 Migration.prototype.removeIndex = function(table, indexName) {
79 this.sequence.enqueue(function() {
80 return _this.queryInterface
81 .removeIndex(table, indexName)
83 logger.info('removeIndex %s %s', table, indexName);
85 .catch(function(err) {
86 logger.info(JSON.stringify(err));
91 Migration.prototype.query = function(sql) {
93 this.sequence.enqueue(function() {
94 return _this.queryInterface.sequelize
97 logger.info('query %s', sql);
99 .catch(function(err) {
100 logger.info(JSON.stringify(err));
105 Migration.prototype.removeColumn = function(table, column) {
107 this.sequence.enqueue(function() {
108 return _this.queryInterface
109 .removeColumn(table, column)
111 logger.info('Removed column %s from %s', column, table);
113 .catch(function(err) {
124 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
126 this.sequence.enqueue(function() {
127 return _this.queryInterface
128 .renameColumn(table, oldColumn, newColumn)
131 'Renamed column from %s to %s on %s',
137 .catch(function(err) {
148 Migration.prototype.final = function(resolve) {
149 this.sequence.enqueue(function() {
156 * Apply up migration for MOTV2-4448
157 * @link https://xcally.atlassian.net/browse/MOTV2-4448
158 * @param {Migration} migration
159 * @returns {Promise<void>}
162 up: function(migration) {
163 return migration.query('ALTER TABLE `chat_websites` DROP INDEX `address`')
166 * Apply down migration for MOTV2-4448
167 * @link https://xcally.atlassian.net/browse/MOTV2-4448
168 * @param {Migration} migration
169 * @returns {Promise<void>}
172 down: function(migration) {
173 return migration.query(
174 'ALTER TABLE `chat_websites` ADD CONSTRAINT address UNIQUE(address)'
178 "WARNING: can't add unique constraint to chat_websites.address: the table contains duplicate values"
186 up: function(queryInterface /*, Sequelize */ ) {
187 return new BPromise(function(resolve) {
188 var migration = new Migration(queryInterface);
189 // Add your migration code here
190 MOTV24448.up(migration)
193 * @see https://xcally.atlassian.net/browse/MOTV2-4430
195 migration.removeColumn('voice_extensions', 'alias');
198 migration.final(resolve);
203 down: function(queryInterface /*, Sequelize */ ) {
204 return new BPromise(function(resolve) {
205 var migration = new Migration(queryInterface);
206 MOTV24448.down(migration)
208 migration.final(resolve);