Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.64.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var rs = require('randomstring');
5 var util = require('util');
6
7 var logger = require('../config/logger')('migration');
8
9 var Sequence = function() {};
10
11 Sequence.prototype.enqueue = function(fn) {
12   this.tail = this.tail ? this.tail.finally(fn) : fn();
13 };
14
15 var Migration = function(queryInterface) {
16   this.queryInterface = queryInterface;
17   this.sequence = new Sequence();
18 };
19
20 Migration.prototype.changeColumn = function(table, column, type) {
21   var _this = this;
22   this.sequence.enqueue(function() {
23     return _this.queryInterface
24       .changeColumn(table, column, type)
25       .then(function(res) {
26         logger.info('Changed column %s in table %s', column, table);
27       })
28       .catch(function(err) {
29         logger.info(JSON.stringify(err));
30       });
31   });
32 };
33
34 Migration.prototype.addColumn = function(table, column, type) {
35   var _this = this;
36   this.sequence.enqueue(function() {
37     return _this.queryInterface
38       .addColumn(table, column, type)
39       .then(function() {
40         logger.info('Added column %s to %s', column, table);
41       })
42       .catch(function(err) {
43         logger.info(JSON.stringify(err));
44       });
45   });
46 };
47
48 Migration.prototype.dropTable = function(table) {
49   var _this = this;
50   this.sequence.enqueue(function() {
51     return _this.queryInterface
52       .dropTable(table, {
53         force: true
54       })
55       .then(function() {
56         logger.info('table dropped %s', table);
57       })
58       .catch(function(err) {
59         logger.info(JSON.stringify(err));
60       });
61   });
62 };
63
64 Migration.prototype.addIndex = function(table, column, indexName) {
65   var _this = this;
66   this.sequence.enqueue(function() {
67     return _this.queryInterface.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.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.query(sql)
96       .then(function() {
97         logger.info('query %s', sql);
98       })
99       .catch(function(err) {
100         logger.info(JSON.stringify(err));
101       });
102   });
103 };
104
105 Migration.prototype.removeColumn = function(table, column) {
106   var _this = this;
107   this.sequence.enqueue(function() {
108     return _this.queryInterface.removeColumn(table, column)
109       .then(function() {
110         logger.info('Removed column %s from %s', column, table);
111       })
112       .catch(function(err) {
113         logger.info(util.inspect(err, {
114           showHidden: false,
115           depth: null
116         }));
117       });
118   });
119 };
120
121 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
122   var _this = this;
123   this.sequence.enqueue(function() {
124     return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
125       .then(function() {
126         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
127       })
128       .catch(function(err) {
129         logger.info(util.inspect(err, {
130           showHidden: false,
131           depth: null
132         }));
133       });
134   });
135 };
136
137 Migration.prototype.final = function(resolve) {
138   this.sequence.enqueue(function() {
139     return resolve();
140   });
141 };
142
143 module.exports = {
144   up: function(queryInterface, Sequelize) {
145     return new BPromise(function(resolve) {
146       var migration = new Migration(queryInterface);
147
148       // START USERS
149       migration.addColumn('users', 'phoneBarEnableVideoRecording', {
150         type: Sequelize.BOOLEAN,
151         defaultValue: false
152       });
153       //END USERS
154
155       // START REPORT CALL
156       migration.changeColumn('report_call', 'lastdata', {
157         type: Sequelize.TEXT
158       });
159       // END REPORT CALL
160
161       // START CHAT WEBSITE
162       migration.addColumn('chat_websites', 'customerAlias', {
163         type: Sequelize.STRING,
164         defaultValue: 'Me'
165       });
166       // END CHAT WEBSITE
167
168       // START analytics_report_fields
169       migration.changeColumn('analytics_report_fields', 'field', {
170         type: Sequelize.TEXT,
171         allowNull: false
172       });
173       // END analytics_report_fields
174
175       // START report_call
176       migration.removeIndex('report_call', 'uniqueid');
177       // END report_call
178
179       // START FINAL
180       migration.final(resolve);
181       // END FINAL
182     });
183   },
184
185   down: function(queryInterface, Sequelize) {
186     // var migration = new Migration(queryInterface);
187   }
188 };