Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.4.2.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, options) {
64   var _this = this;
65   this.sequence.enqueue(function() {
66     return _this.queryInterface
67       .addIndex(table, column, {
68         indexName: options.indexName,
69         indicesType: options.indicesType
70       })
71       .then(function() {
72         logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);
73       })
74       .catch(function(err) {
75         logger.info(JSON.stringify(err));
76       });
77   });
78 };
79
80 Migration.prototype.removeIndex = function(table, indexName) {
81   var _this = this;
82   this.sequence.enqueue(function() {
83     return _this.queryInterface
84       .removeIndex(table, indexName)
85       .then(function() {
86         logger.info('removeIndex %s %s', table, indexName);
87       })
88       .catch(function(err) {
89         logger.info(JSON.stringify(err));
90       });
91   });
92 };
93
94 Migration.prototype.query = function(sql) {
95   var _this = this;
96   this.sequence.enqueue(function() {
97     return _this.queryInterface.sequelize
98       .query(sql)
99       .then(function() {
100         logger.info('query %s', sql);
101       })
102       .catch(function(err) {
103         logger.info(JSON.stringify(err));
104       });
105   });
106 };
107
108 Migration.prototype.removeColumn = function(table, column) {
109   var _this = this;
110   this.sequence.enqueue(function() {
111     return _this.queryInterface
112       .removeColumn(table, column)
113       .then(function() {
114         logger.info('Removed column %s from %s', column, table);
115       })
116       .catch(function(err) {
117         logger.info(
118           util.inspect(err, {
119             showHidden: false,
120             depth: null
121           })
122         );
123       });
124   });
125 };
126
127 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
128   var _this = this;
129   this.sequence.enqueue(function() {
130     return _this.queryInterface
131       .renameColumn(table, oldColumn, newColumn)
132       .then(function() {
133         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
134       })
135       .catch(function(err) {
136         logger.info(
137           util.inspect(err, {
138             showHidden: false,
139             depth: null
140           })
141         );
142       });
143   });
144 };
145
146 Migration.prototype.final = function(resolve) {
147   this.sequence.enqueue(function() {
148     return resolve();
149   });
150 };
151
152 module.exports = {
153   up: function(queryInterface, Sequelize) {
154     return new BPromise(function(resolve) {
155       var migration = new Migration(queryInterface);
156
157       // START SETTINGS
158       migration.addColumn('settings', 'allowedLoginAttempts', {
159         type: Sequelize.INTEGER,
160         defaultValue: 5
161       });
162       migration.addColumn('settings', 'blockDuration', {
163         type: Sequelize.INTEGER,
164         defaultValue: 10
165       });
166       migration.query('UPDATE settings SET allowedLoginAttempts = 0, blockDuration = 0;');
167       // END SETTINGS
168
169       // START USERS
170       migration.addColumn('users', 'blocked', {
171         type: Sequelize.BOOLEAN,
172         defaultValue: false
173       });
174       migration.addColumn('users', 'loginAttempts', {
175         type: Sequelize.INTEGER,
176         defaultValue: 0
177       });
178       migration.addColumn('users', 'blockedAt', {
179         type: Sequelize.DATE,
180         defaultValue: null
181       });
182       migration.addColumn('users', 'disabled', {
183         type: Sequelize.BOOLEAN,
184         defaultValue: false
185       });
186       // END USERS
187
188       // START screen_recordings
189       migration.query('ALTER TABLE screen_recordings ENCRYPTION=\'Y\';');
190       // END screen_recordings
191
192       // START FINAL
193       migration.final(resolve);
194       // END FINAL
195     });
196   },
197
198   down: function(queryInterface, Sequelize) {
199     // var migration = new Migration(queryInterface);
200   }
201 };