3 var BPromise = require('bluebird');
4 var fs = require('fs');
5 var util = require('util');
6 var _ = require('lodash');
8 var logger = require('../config/logger')('migration');
10 var Sequence = function() {};
12 Sequence.prototype.enqueue = function(fn) {
13 this.tail = this.tail ? this.tail.finally(fn) : fn();
16 var Migration = function(queryInterface) {
17 this.queryInterface = queryInterface;
18 this.sequence = new Sequence();
21 Migration.prototype.changeColumn = function(table, column, type) {
23 this.sequence.enqueue(function() {
24 return _this.queryInterface
25 .changeColumn(table, column, type)
27 logger.info('Changed column %s in table %s', column, table);
29 .catch(function(err) {
30 logger.info(JSON.stringify(err));
35 Migration.prototype.addColumn = function(table, column, type) {
37 this.sequence.enqueue(function() {
38 return _this.queryInterface
39 .addColumn(table, column, type)
41 logger.info('Added column %s to %s', column, table);
43 .catch(function(err) {
44 logger.info(JSON.stringify(err));
49 Migration.prototype.dropTable = function(table) {
51 this.sequence.enqueue(function() {
52 return _this.queryInterface
57 logger.info('table dropped %s', table);
59 .catch(function(err) {
60 logger.info(JSON.stringify(err));
65 Migration.prototype.addIndex = function(table, column, indexName) {
67 this.sequence.enqueue(function() {
68 return _this.queryInterface.addIndex(table, column, {
72 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
74 .catch(function(err) {
75 logger.info(JSON.stringify(err));
80 Migration.prototype.query = function(sql) {
82 this.sequence.enqueue(function() {
83 return _this.queryInterface.sequelize.query(sql)
85 logger.info('query %s', sql);
87 .catch(function(err) {
88 logger.info(JSON.stringify(err));
93 Migration.prototype.removeColumn = function(table, column) {
95 this.sequence.enqueue(function() {
96 return _this.queryInterface.removeColumn(table, column)
98 logger.info('Removed column %s from %s', column, table);
100 .catch(function(err) {
101 logger.info(util.inspect(err, {
109 Migration.prototype.final = function(resolve) {
111 this.sequence.enqueue(function() {
117 up: function(queryInterface, Sequelize) {
118 return new BPromise(function(resolve, reject) {
120 var migration = new Migration(queryInterface);
123 migration.addColumn('settings', 'turnaddr', {
124 type: Sequelize.STRING
126 migration.addColumn('settings', 'turnusername', {
127 type: Sequelize.STRING
129 migration.addColumn('settings', 'turnpassword', {
130 type: Sequelize.STRING
135 migration.addColumn('users', 'rtcp_mux', {
136 type: Sequelize.ENUM('yes', 'no'),
140 migration.query('UPDATE users SET rtcp_mux=\'yes\' WHERE showWebBar=2');
143 // START SMS INTERACTIONS
144 migration.addColumn('sms_interactions', 'firstMsgDirection', {
145 type: Sequelize.ENUM('in', 'out'),
150 // Update the firstMsgDirection column with correct value (direction of the first message for each interactions)
151 migration.query("UPDATE sms_interactions t1, (SELECT SmsInteractionId, MIN(id) AS messageid, ANY_VALUE(direction) AS direction FROM sms_messages WHERE SmsInteractionId IS NOT NULL GROUP BY SmsInteractionId) t2 SET t1.firstMsgDirection = t2.direction WHERE t1.id = t2.SmsInteractionId;");
152 // END SMS INTERACTIONS
154 // START MAIL INTERACTIONS
155 migration.addColumn('mail_interactions', 'firstMsgDirection', {
156 type: Sequelize.ENUM('in', 'out'),
161 // Update the firstMsgDirection column with correct value (direction of the first message for each interactions)
162 migration.query("UPDATE mail_interactions t1, (SELECT MailInteractionId, MIN(id) AS messageid, ANY_VALUE(direction) AS direction FROM mail_messages WHERE MailInteractionId IS NOT NULL GROUP BY MailInteractionId) t2 SET t1.firstMsgDirection = t2.direction WHERE t1.id = t2.MailInteractionId;");
163 // END MAIL INTERACTIONS
165 // START FAX INTERACTIONS
166 migration.addColumn('fax_interactions', 'firstMsgDirection', {
167 type: Sequelize.ENUM('in', 'out'),
172 // Update the firstMsgDirection column with correct value (direction of the first message for each interactions)
173 migration.query("UPDATE fax_interactions t1, (SELECT FaxInteractionId, MIN(id) AS messageid, ANY_VALUE(direction) AS direction FROM fax_messages WHERE FaxInteractionId IS NOT NULL GROUP BY FaxInteractionId) t2 SET t1.firstMsgDirection = t2.direction WHERE t1.id = t2.FaxInteractionId;");
174 // END FAX INTERACTIONS
178 fs.writeFileSync('/etc/asterisk/stun_xcally.conf', '; STUN server list\n', {
181 fs.writeFileSync('/etc/asterisk/turn_xcally.conf', '; TURN server list\n', {
185 var lines = fs.readFileSync('/etc/asterisk/rtp.conf', {
189 lines = _.replace(lines, '[ice_host_candidates]', '#include "stun_xcally.conf"\n#include "turn_xcally.conf"\n[ice_host_candidates]');
191 fs.writeFileSync('/etc/asterisk/rtp.conf', lines, {
194 fs.appendFileSync('/etc/asterisk/res_stun_monitor.conf', '#include "stun_xcally.conf"', {
198 logger.info(JSON.stringify(err));
203 migration.final(resolve);
208 down: function(queryInterface, Sequelize) {
209 var migration = new Migration(queryInterface);