Built motion from commit 092f8824.|2.5.49
[motion2.git] / server / migrations / 2.5.49.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var util = require('util');
5
6 var logger = require('../config/logger')('migration');
7
8 var Sequence = function() {};
9
10 Sequence.prototype.enqueue = function(fn) {
11   this.tail = this.tail ? this.tail.finally(fn) : fn();
12 };
13
14 var Migration = function(queryInterface) {
15   this.queryInterface = queryInterface;
16   this.sequence = new Sequence();
17 };
18
19 Migration.prototype.changeColumn = function(table, column, type) {
20   var _this = this;
21   this.sequence.enqueue(function() {
22     return _this.queryInterface
23       .changeColumn(table, column, type)
24       .then(function() {
25         logger.info('Changed column %s in table %s', column, table);
26       })
27       .catch(function(err) {
28         logger.info(JSON.stringify(err));
29       });
30   });
31 };
32
33 Migration.prototype.addColumn = function(table, column, type) {
34   var _this = this;
35   this.sequence.enqueue(function() {
36     return _this.queryInterface
37       .addColumn(table, column, type)
38       .then(function() {
39         logger.info('Added column %s to %s', column, table);
40       })
41       .catch(function(err) {
42         logger.info(JSON.stringify(err));
43       });
44   });
45 };
46
47 Migration.prototype.dropTable = function(table) {
48   var _this = this;
49   this.sequence.enqueue(function() {
50     return _this.queryInterface
51       .dropTable(table, {
52         force: true,
53       })
54       .then(function() {
55         logger.info('table dropped %s', table);
56       })
57       .catch(function(err) {
58         logger.info(JSON.stringify(err));
59       });
60   });
61 };
62
63 Migration.prototype.addIndex = function(table, column, indexName) {
64   var _this = this;
65   this.sequence.enqueue(function() {
66     return _this.queryInterface
67       .addIndex(table, column, {
68         indexName: indexName,
69       })
70       .then(function() {
71         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
72       })
73       .catch(function(err) {
74         logger.info(JSON.stringify(err));
75       });
76   });
77 };
78
79 Migration.prototype.removeIndex = function(table, indexName) {
80   var _this = this;
81   this.sequence.enqueue(function() {
82     return _this.queryInterface
83       .removeIndex(table, indexName)
84       .then(function() {
85         logger.info('removeIndex %s %s', table, indexName);
86       })
87       .catch(function(err) {
88         logger.info(JSON.stringify(err));
89       });
90   });
91 };
92
93 Migration.prototype.query = function(sql) {
94   var _this = this;
95   this.sequence.enqueue(function() {
96     return _this.queryInterface.sequelize
97       .query(sql)
98       .then(function() {
99         logger.info('query %s', sql);
100       })
101       .catch(function(err) {
102         logger.info(JSON.stringify(err));
103       });
104   });
105 };
106
107 Migration.prototype.removeColumn = function(table, column) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return _this.queryInterface
111       .removeColumn(table, column)
112       .then(function() {
113         logger.info('Removed column %s from %s', column, table);
114       })
115       .catch(function(err) {
116         logger.info(
117           util.inspect(err, {
118             showHidden: false,
119             depth: null,
120           })
121         );
122       });
123   });
124 };
125
126 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
127   var _this = this;
128   this.sequence.enqueue(function() {
129     return _this.queryInterface
130       .renameColumn(table, oldColumn, newColumn)
131       .then(function() {
132         logger.info(
133           'Renamed column from %s to %s on %s',
134           oldColumn,
135           newColumn,
136           table
137         );
138       })
139       .catch(function(err) {
140         logger.info(
141           util.inspect(err, {
142             showHidden: false,
143             depth: null,
144           })
145         );
146       });
147   });
148 };
149
150 Migration.prototype.final = function(resolve) {
151   this.sequence.enqueue(function() {
152     return resolve();
153   });
154 };
155
156 module.exports = {
157   up: function(queryInterface, Sequelize) {
158     return new BPromise(function(resolve) {
159       var migration = new Migration(queryInterface);
160
161       migration.addColumn('users', 'chatAutoanswer', {
162         type: Sequelize.BOOLEAN,
163         defaultValue: false
164       });
165       migration.addColumn('users', 'chatAutoanswerDelay', {
166         type: Sequelize.INTEGER,
167         defaultValue: 0
168       });
169
170       migration.addColumn('users', 'emailAutoanswer', {
171         type: Sequelize.BOOLEAN,
172         defaultValue: false
173       });
174       migration.addColumn('users', 'emailAutoanswerDelay', {
175         type: Sequelize.INTEGER,
176         defaultValue: 0
177       });
178
179       migration.addColumn('users', 'smsAutoanswer', {
180         type: Sequelize.BOOLEAN,
181         defaultValue: false
182       });
183       migration.addColumn('users', 'smsAutoanswerDelay', {
184         type: Sequelize.INTEGER,
185         defaultValue: 0
186       });
187
188       migration.addColumn('users', 'openchannelAutoanswer', {
189         type: Sequelize.BOOLEAN,
190         defaultValue: false
191       });
192       migration.addColumn('users', 'openchannelAutoanswerDelay', {
193         type: Sequelize.INTEGER,
194         defaultValue: 0
195       });
196
197       migration.addColumn('users', 'faxAutoanswer', {
198         type: Sequelize.BOOLEAN,
199         defaultValue: false
200       });
201       migration.addColumn('users', 'faxAutoanswerDelay', {
202         type: Sequelize.INTEGER,
203         defaultValue: 0
204       });
205
206       migration.addColumn('users', 'whatsappAutoanswer', {
207         type: Sequelize.BOOLEAN,
208         defaultValue: false
209       });
210       migration.addColumn('users', 'whatsappAutoanswerDelay', {
211         type: Sequelize.INTEGER,
212         defaultValue: 0
213       });
214
215       // START FINAL
216       migration.final(resolve);
217       // END FINAL
218     });
219   },
220
221   down: function(queryInterface, Sequelize) {
222     // var migration = new Migration(queryInterface);
223   },
224 };