3530b8ae74dea93e4cfa7ed78bcb8d6e2de8f046
[motion2.git] / server / migrations / 2.6.12.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var util = require('util');
5 var logger = require('../config/logger')('migration');
6
7 var Sequence = function() {};
8
9 Sequence.prototype.enqueue = function(fn) {
10   this.tail = this.tail ? this.tail.finally(fn) : fn();
11 };
12
13 var Migration = function(queryInterface) {
14   this.queryInterface = queryInterface;
15   this.sequence = new Sequence();
16 };
17
18 Migration.prototype.changeColumn = function(table, column, type) {
19   var _this = this;
20   this.sequence.enqueue(function() {
21     return _this.queryInterface
22       .changeColumn(table, column, type)
23       .then(function() {
24         logger.info('Changed column %s in table %s', column, table);
25       })
26       .catch(function(err) {
27         logger.info(JSON.stringify(err));
28       });
29   });
30 };
31
32 Migration.prototype.addColumn = function(table, column, type) {
33   var _this = this;
34   this.sequence.enqueue(function() {
35     return _this.queryInterface
36       .addColumn(table, column, type)
37       .then(function() {
38         logger.info('Added column %s to %s', column, table);
39       })
40       .catch(function(err) {
41         logger.info(JSON.stringify(err));
42       });
43   });
44 };
45
46 Migration.prototype.dropTable = function(table) {
47   var _this = this;
48   this.sequence.enqueue(function() {
49     return _this.queryInterface
50       .dropTable(table, {
51         force: true,
52       })
53       .then(function() {
54         logger.info('table dropped %s', table);
55       })
56       .catch(function(err) {
57         logger.info(JSON.stringify(err));
58       });
59   });
60 };
61
62 Migration.prototype.addIndex = function(table, column, indexName) {
63   var _this = this;
64   this.sequence.enqueue(function() {
65     return _this.queryInterface
66       .addIndex(table, column, {
67         indexName: indexName,
68       })
69       .then(function() {
70         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
71       })
72       .catch(function(err) {
73         logger.info(JSON.stringify(err));
74       });
75   });
76 };
77
78 Migration.prototype.removeIndex = function(table, indexName) {
79   var _this = this;
80   this.sequence.enqueue(function() {
81     return _this.queryInterface
82       .removeIndex(table, indexName)
83       .then(function() {
84         logger.info('removeIndex %s %s', table, indexName);
85       })
86       .catch(function(err) {
87         logger.info(JSON.stringify(err));
88       });
89   });
90 };
91
92 Migration.prototype.query = function(sql) {
93   var _this = this;
94   this.sequence.enqueue(function() {
95     return _this.queryInterface.sequelize
96       .query(sql)
97       .then(function() {
98         logger.info('query %s', sql);
99       })
100       .catch(function(err) {
101         logger.info(JSON.stringify(err));
102       });
103   });
104 };
105
106 Migration.prototype.removeColumn = function(table, column) {
107   var _this = this;
108   this.sequence.enqueue(function() {
109     return _this.queryInterface
110       .removeColumn(table, column)
111       .then(function() {
112         logger.info('Removed column %s from %s', column, table);
113       })
114       .catch(function(err) {
115         logger.info(
116           util.inspect(err, {
117             showHidden: false,
118             depth: null,
119           })
120         );
121       });
122   });
123 };
124
125 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
126   var _this = this;
127   this.sequence.enqueue(function() {
128     return _this.queryInterface
129       .renameColumn(table, oldColumn, newColumn)
130       .then(function() {
131         logger.info(
132           'Renamed column from %s to %s on %s',
133           oldColumn,
134           newColumn,
135           table
136         );
137       })
138       .catch(function(err) {
139         logger.info(
140           util.inspect(err, {
141             showHidden: false,
142             depth: null,
143           })
144         );
145       });
146   });
147 };
148
149 Migration.prototype.final = function(resolve) {
150   this.sequence.enqueue(function() {
151     return resolve();
152   });
153 };
154
155 module.exports = {
156   up: function(queryInterface /* , Sequelize */ ) {
157     return new BPromise(function(resolve) {
158       var migration = new Migration(queryInterface);
159
160       // START FINAL
161       migration.final(resolve);
162       // END FINAL
163     });
164   },
165
166   down: function( /* queryInterface, Sequelize */ ) {
167     // var migration = new Migration(queryInterface);
168   },
169 };