Built motion from commit 67e5df37.|2.0.66
[motion2.git] / server / migrations / 2.0.39.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 ejs = require('ejs');
8 var fs = require('fs');
9 var path = require('path');
10 var exec = require('child_process').exec;
11
12 var config = require('../config/environment');
13
14 var Sequence = function() {};
15
16 Sequence.prototype.enqueue = function(fn) {
17     this.tail = this.tail ? this.tail.finally(fn) : fn();
18 };
19
20 var Migration = function(queryInterface) {
21     this.queryInterface = queryInterface;
22     this.sequence = new Sequence();
23 };
24
25 Migration.prototype.changeColumn = function(table, column, type) {
26     var _this = this;
27     this.sequence.enqueue(function() {
28         return _this.queryInterface
29             .changeColumn(table, column, type)
30             .then(function(res) {
31                 logger.info('Changed column %s in table %s', column, table);
32             })
33             .catch(function(err) {
34                 logger.info(JSON.stringify(err));
35             });
36     });
37 };
38
39 Migration.prototype.addColumn = function(table, column, type) {
40     var _this = this;
41     this.sequence.enqueue(function() {
42         return _this.queryInterface
43             .addColumn(table, column, type)
44             .then(function(res) {
45                 logger.info('Added column %s to %s', column, table);
46             })
47             .catch(function(err) {
48                 logger.info(JSON.stringify(err));
49             });
50     });
51 };
52
53 Migration.prototype.dropTable = function(table) {
54     var _this = this;
55     this.sequence.enqueue(function() {
56         return _this.queryInterface
57             .dropTable(table, {
58                 force: true
59             })
60             .then(function(res) {
61                 logger.info('table dropped %s', table);
62             })
63             .catch(function(err) {
64                 logger.info(JSON.stringify(err));
65             });
66     });
67 };
68
69 Migration.prototype.addIndex = function(table, column, indexName) {
70     var _this = this;
71     this.sequence.enqueue(function() {
72         return _this.queryInterface.addIndex(table, column, {
73                 indexName: indexName
74             })
75             .then(function(res) {
76                 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
77             })
78             .catch(function(err) {
79                 logger.info(JSON.stringify(err));
80             });
81     });
82 };
83
84 Migration.prototype.query = function(sql) {
85     var _this = this;
86     this.sequence.enqueue(function() {
87         return _this.queryInterface.sequelize.query(sql)
88             .then(function(res) {
89                 logger.info('query %s', sql);
90             })
91             .catch(function(err) {
92                 logger.info(JSON.stringify(err));
93             });
94     });
95 };
96
97 Migration.prototype.removeColumn = function(table, column) {
98     var _this = this;
99     this.sequence.enqueue(function() {
100         return _this.queryInterface.removeColumn(table, column)
101             .then(function(res) {
102                 logger.info('Removed column %s from %s', column, table);
103             })
104             .catch(function(err) {
105                 logger.info(util.inspect(err, {
106                     showHidden: false,
107                     depth: null
108                 }));
109             });
110     });
111 };
112
113 Migration.prototype.checkVoiceContexts = function() {
114     var _this = this;
115     var VoiceContext = _this.queryInterface.sequelize.import(path.join(config.root, 'server/api/voiceContext/voiceContext.model'));
116     this.sequence.enqueue(function() {
117         return VoiceContext.findOrCreate({
118                 where: {
119                     name: 'xcally-mixmonitor-context'
120                 },
121                 defaults: {
122                     name: 'xcally-mixmonitor-context',
123                     description: 'default entry context (DO NOT DELETE)',
124                     defaultEntry: true
125                 }
126             })
127             .spread(function(entity, created) {
128                 return VoiceContext
129                     .findAll({
130                         raw: true
131                     });
132             })
133             .then(function(contexts) {
134                 var templateContext = fs.readFileSync(path.join(config.root, 'server/files/templates/context.ejs'), 'utf8');
135
136                 var contextsFile = ejs.render(templateContext, {
137                     contexts: contexts || []
138                 });
139
140                 fs.writeFileSync('/etc/asterisk/extensions_xcally_contexts.conf', contextsFile);
141
142                 exec('asterisk -rx "reload chan_sip.so"', function(err, stdout, stderr) {
143                     logger.info('Exec chmod', ' Error :', JSON.stringify(err), stdout, stderr);
144                 });
145
146             })
147             .catch(function(err) {
148                 logger.info(JSON.stringify(err));
149             });
150     });
151 };
152
153 Migration.prototype.final = function(resolve) {
154     var _this = this;
155     this.sequence.enqueue(function() {
156         return resolve();
157     });
158 };
159
160 module.exports = {
161     up: function(queryInterface, Sequelize) {
162         return new BPromise(function(resolve, reject) {
163             var migration = new Migration(queryInterface);
164             // START SETTINGS
165             migration.addColumn('settings', 'defaultPageTitle', {
166                 type: Sequelize.BOOLEAN,
167                 defaultValue: true
168             });
169             migration.addColumn('settings', 'pageTitle', {
170                 type: Sequelize.STRING
171             });
172             // END SETTINGS
173
174             // START VOICECONTEXT
175             migration.checkVoiceContexts();
176             // END VOICECONTEXT
177
178             // START SALESFORCE CONF
179             migration.addColumn('int_salesforce_configurations', 'moduleSearch', {
180                 type: Sequelize.ENUM('contact_lead', 'contact', 'lead'),
181                 defaultValue: 'contact_lead'
182             });
183             migration.addColumn('int_salesforce_configurations', 'moduleCreate', {
184                 type: Sequelize.ENUM('nothing', 'contact', 'lead'),
185                 defaultValue: 'lead'
186             });
187             // END SALESFORCE CONF
188
189             // START FAX
190             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '1', 'NoOp', '', 'system', 'Fax extensions auto generated', NOW(), NOW());");
191
192             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '2', 'Set', 'CDR(type)=outbound-fax', 'system', 'Fax extensions auto generated', NOW(), NOW());");
193
194             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '3', 'Wait', '6', 'system', 'Fax extensions auto generated', NOW(), NOW());");
195
196             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '4', 'Set', 'FAXOPT(ecm)=${XMF-ECM}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
197
198             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '5', 'Set', 'FAXOPT(headerinfo)=${XMF-HEADERINFO}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
199
200             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '6', 'Set', 'FAXOPT(localstationid)=${XMF-LOCALSTATIONID}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
201
202             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '7', 'Set', 'FAXOPT(maxrate)=${XMF-MAXRATE}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
203
204             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '8', 'Set', 'FAXOPT(minrate)=${XMF-MINRATE}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
205
206             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '9', 'Set', 'FAXOPT(modem)=${XMF-MODEM}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
207
208             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '10', 'Set', 'FAXOPT(t38timeout)=${XMF-T38TIMEOUT}', 'system', 'Fax extensions auto generated', NOW(), NOW());");
209
210             migration.query("INSERT INTO `voice_extensions` (`context`, `exten`, `priority`, `app`, `appdata`, `type`, `description`, `createdAt`, `updatedAt`) VALUES ('from-sip', 'xcally-motion-fax', '11', 'SendFax', '${XMF-PATH},d', 'system', 'Fax extensions auto generated', NOW(), NOW());");
211
212             exec('chmod 775 /var/opt/motion2/server/files/attachments', function(err, stdout, stderr) {
213                 logger.info('Exec chmod', ' Error :', JSON.stringify(err), stdout, stderr);
214                 exec('ls -la /var/opt/motion2/server/files', function(err, stdout, stderr) {
215                     logger.info('Ls :', ' Error :', JSON.stringify(err), stdout, stderr);
216                 });
217             });
218             // END FAX
219
220             // START CONTACT
221             migration.addColumn('cm_contacts', 'deletedAt', {
222                 type: Sequelize.DATE,
223                 defaultValue: null
224             });
225             // END CONTACT
226
227             // START FINAL
228             migration.final(resolve);
229             // END FINAL
230         });
231     },
232
233     down: function(queryInterface, Sequelize) {
234         var migration = new Migration(queryInterface);
235     }
236 };