Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.41.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 exec = require('child_process').exec;
8 var cmd = 'chown motion:asterisk /var/opt/motion2/server/files/attachments';
9 var ls = 'ls -la /var/opt/motion2/server/files';
10
11 var Sequence = function() {};
12
13 Sequence.prototype.enqueue = function(fn) {
14   this.tail = this.tail ? this.tail.finally(fn) : fn();
15 };
16
17 var Migration = function(queryInterface) {
18   this.queryInterface = queryInterface;
19   this.sequence = new Sequence();
20 };
21
22 Migration.prototype.changeColumn = function(table, column, type) {
23   var _this = this;
24   this.sequence.enqueue(function() {
25     return _this.queryInterface
26       .changeColumn(table, column, type)
27       .then(function(res) {
28         logger.info('Changed column %s in table %s', column, table);
29       })
30       .catch(function(err) {
31         logger.info(JSON.stringify(err));
32       });
33   });
34 };
35
36 Migration.prototype.addColumn = function(table, column, type) {
37   var _this = this;
38   this.sequence.enqueue(function() {
39     return _this.queryInterface
40       .addColumn(table, column, type)
41       .then(function(res) {
42         logger.info('Added column %s to %s', column, table);
43       })
44       .catch(function(err) {
45         logger.info(JSON.stringify(err));
46       });
47   });
48 };
49
50 Migration.prototype.dropTable = function(table) {
51   var _this = this;
52   this.sequence.enqueue(function() {
53     return _this.queryInterface
54       .dropTable(table, {
55         force: true
56       })
57       .then(function(res) {
58         logger.info('table dropped %s', table);
59       })
60       .catch(function(err) {
61         logger.info(JSON.stringify(err));
62       });
63   });
64 };
65
66 Migration.prototype.addIndex = function(table, column, indexName) {
67   var _this = this;
68   this.sequence.enqueue(function() {
69     return _this.queryInterface.addIndex(table, column, {
70         indexName: indexName
71       })
72       .then(function(res) {
73         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
74       })
75       .catch(function(err) {
76         logger.info(JSON.stringify(err));
77       });
78   });
79 };
80
81 Migration.prototype.query = function(sql) {
82   var _this = this;
83   this.sequence.enqueue(function() {
84     return _this.queryInterface.sequelize.query(sql)
85       .then(function(res) {
86         logger.info('query %s', sql);
87       })
88       .catch(function(err) {
89         logger.info(JSON.stringify(err));
90       });
91   });
92 };
93
94 Migration.prototype.removeColumn = function(table, column) {
95   var _this = this;
96   this.sequence.enqueue(function() {
97     return _this.queryInterface.removeColumn(table, column)
98       .then(function(res) {
99         logger.info('Removed column %s from %s', column, table);
100       })
101       .catch(function(err) {
102         logger.info(util.inspect(err, {
103           showHidden: false,
104           depth: null
105         }));
106       });
107   });
108 };
109
110
111 Migration.prototype.final = function(resolve) {
112   var _this = this;
113   this.sequence.enqueue(function() {
114     return resolve();
115   });
116 };
117
118 module.exports = {
119   up: function(queryInterface, Sequelize) {
120     return new BPromise(function(resolve, reject) {
121       var migration = new Migration(queryInterface);
122
123       // START DESK ACCOUNTS
124       migration.addColumn('int_desk_accounts', 'serverUrl', {
125         type: Sequelize.STRING
126       });
127       // END DESK ACCOUNTS
128
129       // START CHAT INTERACTIONS
130       migration.addColumn('chat_interactions', 'formData', {
131         type: Sequelize.TEXT('long')
132       });
133       // END CHAT INTERACTIONS
134
135       // START CHAT MESSAGES
136       migration.addColumn('chat_messages', 'secret', {
137         type: Sequelize.BOOLEAN,
138         defaultValue: false
139       });
140       // END CHAT MESSAGES
141
142       // START CHAT WEBSITES
143       migration.addColumn('chat_websites', 'agentAvatar', {
144         type: Sequelize.TEXT
145       });
146       migration.addColumn('chat_websites', 'showAgentAvatar', {
147         type: Sequelize.BOOLEAN,
148         defaultValue: false
149       });
150       // END CHAT WEBSITES
151
152       //START CUSTOM FIELDS
153       migration.changeColumn('cm_custom_fields', 'values', {
154         type: Sequelize.TEXT('long')
155       });
156       //END CUSTOM FIELDS
157
158       // START FINAL
159       migration.final(resolve);
160       // END FINAL
161     });
162   },
163
164   down: function(queryInterface, Sequelize) {
165     var migration = new Migration(queryInterface);
166   }
167 };