Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.42.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4
5 var logger = require('../config/logger')('migration');
6 var util = require('util');
7 var path = require('path');
8 var config = require('../config/environment');
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.getCustomFields = function() {
110   var _this = this;
111   var CmCustomField = _this.queryInterface.sequelize.import(path.join(config.root, 'server/api/cmCustomField/cmCustomField.model'));
112   return CmCustomField.findAll({
113       where: {
114         type: {
115           $notIn: ['number', 'switch']
116         }
117       },
118       attributes: ['id']
119     })
120     .then(function(entities) {
121       return entities;
122     })
123     .catch(function(err) {
124       logger.info(JSON.stringify(err));
125     });
126 };
127
128
129 Migration.prototype.final = function(resolve) {
130   var _this = this;
131   this.sequence.enqueue(function() {
132     return resolve();
133   });
134 };
135
136 module.exports = {
137   up: function(queryInterface, Sequelize) {
138     return new BPromise(function(resolve, reject) {
139       var migration = new Migration(queryInterface);
140
141       // START TOOLS TAGS
142       migration.addColumn('tools_tags', 'color', {
143         type: Sequelize.STRING,
144         defaultValue: '#0091EA'
145       });
146       // END TOOLS TAGS
147
148       // START OPENCHANNEL MESSAGES
149       migration.addColumn('openchannel_messages', 'secret', {
150         type: Sequelize.BOOLEAN,
151         defaultValue: false
152       });
153       // END OPENCHANNEL MESSAGES
154
155       //START CONTACTS
156       migration.getCustomFields()
157         .then(function(entities) {
158           if (entities) {
159             for (var i = 0, length = entities.length; i < length; i++) {
160               migration.changeColumn('cm_contacts', 'cf_' + entities[i].id, {
161                 type: Sequelize.TEXT
162               });
163             }
164           }
165         });
166       //END CONTACTS
167
168       // START FAX
169       migration.addColumn('fax_messages', 'failMessage', {
170         type: Sequelize.TEXT
171       });
172
173       // END FAX
174
175       // START USER
176       migration.addColumn('users', 'extensionMonitor', {
177         type: Sequelize.STRING,
178         defaultValue: ''
179       });
180
181       // END USER
182
183       // START FINAL
184       migration.final(resolve);
185       // END FINAL
186     });
187   },
188
189   down: function(queryInterface, Sequelize) {
190     var migration = new Migration(queryInterface);
191   }
192 };