Built motion from commit 67e5df37.|2.0.66
[motion2.git] / server / migrations / 2.0.54.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var fs = require('fs');
5 var util = require('util');
6 var _ = require('lodash');
7
8 var logger = require('../config/logger')('migration');
9
10 var Sequence = function() {};
11
12 Sequence.prototype.enqueue = function(fn) {
13     this.tail = this.tail ? this.tail.finally(fn) : fn();
14 };
15
16 var Migration = function(queryInterface) {
17     this.queryInterface = queryInterface;
18     this.sequence = new Sequence();
19 };
20
21 Migration.prototype.changeColumn = function(table, column, type) {
22     var _this = this;
23     this.sequence.enqueue(function() {
24         return _this.queryInterface
25             .changeColumn(table, column, type)
26             .then(function(res) {
27                 logger.info('Changed column %s in table %s', column, table);
28             })
29             .catch(function(err) {
30                 logger.info(JSON.stringify(err));
31             });
32     });
33 };
34
35 Migration.prototype.addColumn = function(table, column, type) {
36     var _this = this;
37     this.sequence.enqueue(function() {
38         return _this.queryInterface
39             .addColumn(table, column, type)
40             .then(function(res) {
41                 logger.info('Added column %s to %s', column, table);
42             })
43             .catch(function(err) {
44                 logger.info(JSON.stringify(err));
45             });
46     });
47 };
48
49 Migration.prototype.dropTable = function(table) {
50     var _this = this;
51     this.sequence.enqueue(function() {
52         return _this.queryInterface
53             .dropTable(table, {
54                 force: true
55             })
56             .then(function(res) {
57                 logger.info('table dropped %s', table);
58             })
59             .catch(function(err) {
60                 logger.info(JSON.stringify(err));
61             });
62     });
63 };
64
65 Migration.prototype.addIndex = function(table, column, indexName) {
66     var _this = this;
67     this.sequence.enqueue(function() {
68         return _this.queryInterface.addIndex(table, column, {
69                 indexName: indexName
70             })
71             .then(function(res) {
72                 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
73             })
74             .catch(function(err) {
75                 logger.info(JSON.stringify(err));
76             });
77     });
78 };
79
80 Migration.prototype.query = function(sql) {
81     var _this = this;
82     this.sequence.enqueue(function() {
83         return _this.queryInterface.sequelize.query(sql)
84             .then(function(res) {
85                 logger.info('query %s', sql);
86             })
87             .catch(function(err) {
88                 logger.info(JSON.stringify(err));
89             });
90     });
91 };
92
93 Migration.prototype.removeColumn = function(table, column) {
94     var _this = this;
95     this.sequence.enqueue(function() {
96         return _this.queryInterface.removeColumn(table, column)
97             .then(function(res) {
98                 logger.info('Removed column %s from %s', column, table);
99             })
100             .catch(function(err) {
101                 logger.info(util.inspect(err, {
102                     showHidden: false,
103                     depth: null
104                 }));
105             });
106     });
107 };
108
109 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
110     var _this = this;
111     this.sequence.enqueue(function() {
112         return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
113             .then(function(res) {
114                 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
115             })
116             .catch(function(err) {
117                 logger.info(util.inspect(err, {
118                     showHidden: false,
119                     depth: null
120                 }));
121             });
122     });
123 };
124
125 Migration.prototype.final = function(resolve) {
126     var _this = this;
127     this.sequence.enqueue(function() {
128         return resolve();
129     });
130 };
131
132 module.exports = {
133     up: function(queryInterface, Sequelize) {
134         return new BPromise(function(resolve, reject) {
135
136             var migration = new Migration(queryInterface);
137
138             // START CHAT WEBSITES
139             migration.addColumn('chat_websites', 'textColor', {
140                 type: Sequelize.STRING,
141                 allowNull: false,
142                 defaultValue: '#ffffff',
143                 validate: {
144                     notEmpty: true
145                 }
146             });
147             migration.addColumn('chat_websites', 'fontSize', {
148                 type: Sequelize.INTEGER,
149                 defaultValue: 15
150             });
151             migration.addColumn('chat_websites', 'timezone', {
152                 type: Sequelize.STRING
153             });
154             // END CHAT WEBSITES
155
156             // START USERS
157             migration.query('UPDATE users SET nat=\'force_rport,comedia\' WHERE nat IS NULL');
158             migration.query('UPDATE users SET transport=\'udp\' WHERE avpf IS NULL AND transport IS NULL');
159             migration.query('UPDATE users SET transport=\'wss\' WHERE avpf IS NOT NULL');
160             // STOP USERS
161
162             // START SCHEDULED REPORTS
163             migration.changeColumn('tools_schedules', 'output', {
164                 type: Sequelize.ENUM('csv', 'pdf', 'xls', 'xlsx'),
165                 allowNull: false,
166                 defaultValue: 'csv'
167             });
168             migration.query('UPDATE tools_schedules SET output=\'xlsx\' WHERE output=\'xls\'');
169             migration.changeColumn('tools_schedules', 'output', {
170                 type: Sequelize.ENUM('csv', 'pdf', 'xlsx'),
171                 allowNull: false,
172                 defaultValue: 'csv'
173             });
174             // END SCHEDULED REPORTS
175
176             // START MAIL ACCOUNT
177             migration.addColumn('mail_accounts', 'markAsUnread', {
178                 type: Sequelize.BOOLEAN,
179                 defaultValue: false
180             });
181
182             migration.addColumn('mail_accounts', 'fontFamily', {
183                 type: Sequelize.STRING,
184                 allowNull: false,
185                 defaultValue: 'Arial,Helvetica,sans-serif'
186             });
187
188             migration.addColumn('mail_accounts', 'fontSize', {
189                 type: Sequelize.INTEGER(2).UNSIGNED,
190                 defaultValue: 13,
191                 validate: {
192                     min: 8,
193                     max: 72
194                 }
195             });
196
197             migration.renameColumn('mail_accounts', 'signature', 'template');
198             // END MAIL ACCOUNT
199
200             // START FINAL
201             migration.final(resolve);
202             // END FINAL
203         });
204     },
205
206     down: function(queryInterface, Sequelize) {
207         var migration = new Migration(queryInterface);
208     }
209 };