3 var BPromise = require('bluebird');
\r
4 var util = require('util');
\r
6 var logger = require('../config/logger')('migration');
\r
8 var Sequence = function () {};
\r
10 Sequence.prototype.enqueue = function (fn) {
\r
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
\r
14 var Migration = function (queryInterface) {
\r
15 this.queryInterface = queryInterface;
\r
16 this.sequence = new Sequence();
\r
19 Migration.prototype.changeColumn = function (table, column, type) {
\r
21 this.sequence.enqueue(function () {
\r
22 return _this.queryInterface
\r
23 .changeColumn(table, column, type)
\r
25 logger.info('Changed column %s in table %s', column, table);
\r
27 .catch(function (err) {
\r
28 logger.info(JSON.stringify(err));
\r
33 Migration.prototype.addColumn = function (table, column, type) {
\r
35 this.sequence.enqueue(function () {
\r
36 return _this.queryInterface
\r
37 .addColumn(table, column, type)
\r
39 logger.info('Added column %s to %s', column, table);
\r
41 .catch(function (err) {
\r
42 logger.info(JSON.stringify(err));
\r
47 Migration.prototype.dropTable = function (table) {
\r
49 this.sequence.enqueue(function () {
\r
50 return _this.queryInterface
\r
55 logger.info('table dropped %s', table);
\r
57 .catch(function (err) {
\r
58 logger.info(JSON.stringify(err));
\r
63 Migration.prototype.addIndex = function (table, column, options) {
\r
65 this.sequence.enqueue(function () {
\r
66 return _this.queryInterface
\r
67 .addIndex(table, column, {
\r
68 indexName: options.indexName,
\r
69 indicesType: options.indicesType
\r
72 logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);
\r
74 .catch(function (err) {
\r
75 logger.info(JSON.stringify(err));
\r
80 Migration.prototype.removeIndex = function (table, indexName) {
\r
82 this.sequence.enqueue(function () {
\r
83 return _this.queryInterface
\r
84 .removeIndex(table, indexName)
\r
86 logger.info('removeIndex %s %s', table, indexName);
\r
88 .catch(function (err) {
\r
89 logger.info(JSON.stringify(err));
\r
94 Migration.prototype.query = function (sql) {
\r
96 this.sequence.enqueue(function () {
\r
97 return _this.queryInterface.sequelize
\r
100 logger.info('query %s', sql);
\r
102 .catch(function (err) {
\r
103 logger.info(JSON.stringify(err));
\r
108 Migration.prototype.removeColumn = function (table, column) {
\r
110 this.sequence.enqueue(function () {
\r
111 return _this.queryInterface
\r
112 .removeColumn(table, column)
\r
113 .then(function () {
\r
114 logger.info('Removed column %s from %s', column, table);
\r
116 .catch(function (err) {
\r
118 util.inspect(err, {
\r
127 Migration.prototype.renameColumn = function (table, oldColumn, newColumn) {
\r
129 this.sequence.enqueue(function () {
\r
130 return _this.queryInterface
\r
131 .renameColumn(table, oldColumn, newColumn)
\r
132 .then(function () {
\r
133 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
\r
135 .catch(function (err) {
\r
137 util.inspect(err, {
\r
146 Migration.prototype.final = function (resolve) {
\r
147 this.sequence.enqueue(function () {
\r
153 up: function (queryInterface, Sequelize) {
\r
154 return new BPromise(function (resolve) {
\r
156 var migration = new Migration(queryInterface);
\r
159 migration.final(resolve);
\r
164 down: function (queryInterface, Sequelize) {
\r
165 // var migration = new Migration(queryInterface);
\r