3 var BPromise = require('bluebird');
4 var util = require('util');
5 var logger = require('../config/logger')('migration');
7 var Sequence = function() {};
9 Sequence.prototype.enqueue = function(fn) {
10 this.tail = this.tail ? this.tail.finally(fn) : fn();
13 var Migration = function(queryInterface) {
14 this.queryInterface = queryInterface;
15 this.sequence = new Sequence();
18 Migration.prototype.changeColumn = function(table, column, type) {
20 this.sequence.enqueue(function() {
21 return _this.queryInterface
22 .changeColumn(table, column, type)
24 logger.info('Changed column %s in table %s', column, table);
26 .catch(function(err) {
27 logger.info(JSON.stringify(err));
32 Migration.prototype.addColumn = function(table, column, type) {
34 this.sequence.enqueue(function() {
35 return _this.queryInterface
36 .addColumn(table, column, type)
38 logger.info('Added column %s to %s', column, table);
40 .catch(function(err) {
41 logger.info(JSON.stringify(err));
46 Migration.prototype.dropTable = function(table) {
48 this.sequence.enqueue(function() {
49 return _this.queryInterface
54 logger.info('table dropped %s', table);
56 .catch(function(err) {
57 logger.info(JSON.stringify(err));
62 Migration.prototype.addIndex = function(table, column, indexName) {
64 this.sequence.enqueue(function() {
65 return _this.queryInterface
66 .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.removeIndex = function(table, indexName) {
80 this.sequence.enqueue(function() {
81 return _this.queryInterface
82 .removeIndex(table, indexName)
84 logger.info('removeIndex %s %s', table, indexName);
86 .catch(function(err) {
87 logger.info(JSON.stringify(err));
92 Migration.prototype.query = function(sql) {
94 this.sequence.enqueue(function() {
95 return _this.queryInterface.sequelize
98 logger.info('query %s', sql);
100 .catch(function(err) {
101 logger.info(JSON.stringify(err));
106 Migration.prototype.removeColumn = function(table, column) {
108 this.sequence.enqueue(function() {
109 return _this.queryInterface
110 .removeColumn(table, column)
112 logger.info('Removed column %s from %s', column, table);
114 .catch(function(err) {
125 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
127 this.sequence.enqueue(function() {
128 return _this.queryInterface
129 .renameColumn(table, oldColumn, newColumn)
132 'Renamed column from %s to %s on %s',
138 .catch(function(err) {
149 Migration.prototype.final = function(resolve) {
150 this.sequence.enqueue(function() {
156 up: function(queryInterface /* , Sequelize */ ) {
157 return new BPromise(function(resolve) {
158 var migration = new Migration(queryInterface);
161 migration.final(resolve);
166 down: function( /* queryInterface, Sequelize */ ) {
167 // var migration = new Migration(queryInterface);