3 var BPromise = require('bluebird');
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';
11 var Sequence = function() {};
13 Sequence.prototype.enqueue = function(fn) {
14 this.tail = this.tail ? this.tail.finally(fn) : fn();
17 var Migration = function(queryInterface) {
18 this.queryInterface = queryInterface;
19 this.sequence = new Sequence();
22 Migration.prototype.changeColumn = function(table, column, type) {
24 this.sequence.enqueue(function() {
25 return _this.queryInterface
26 .changeColumn(table, column, type)
28 logger.info('Changed column %s in table %s', column, table);
30 .catch(function(err) {
31 logger.info(JSON.stringify(err));
36 Migration.prototype.addColumn = function(table, column, type) {
38 this.sequence.enqueue(function() {
39 return _this.queryInterface
40 .addColumn(table, column, type)
42 logger.info('Added column %s to %s', column, table);
44 .catch(function(err) {
45 logger.info(JSON.stringify(err));
50 Migration.prototype.dropTable = function(table) {
52 this.sequence.enqueue(function() {
53 return _this.queryInterface
58 logger.info('table dropped %s', table);
60 .catch(function(err) {
61 logger.info(JSON.stringify(err));
66 Migration.prototype.addIndex = function(table, column, indexName) {
68 this.sequence.enqueue(function() {
69 return _this.queryInterface.addIndex(table, column, {
73 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
75 .catch(function(err) {
76 logger.info(JSON.stringify(err));
81 Migration.prototype.query = function(sql) {
83 this.sequence.enqueue(function() {
84 return _this.queryInterface.sequelize.query(sql)
86 logger.info('query %s', sql);
88 .catch(function(err) {
89 logger.info(JSON.stringify(err));
94 Migration.prototype.removeColumn = function(table, column) {
96 this.sequence.enqueue(function() {
97 return _this.queryInterface.removeColumn(table, column)
99 logger.info('Removed column %s from %s', column, table);
101 .catch(function(err) {
102 logger.info(util.inspect(err, {
111 Migration.prototype.final = function(resolve) {
113 this.sequence.enqueue(function() {
119 up: function(queryInterface, Sequelize) {
120 return new BPromise(function(resolve, reject) {
121 var migration = new Migration(queryInterface);
123 // START DESK ACCOUNTS
124 migration.addColumn('int_desk_accounts', 'serverUrl', {
125 type: Sequelize.STRING
129 // START CHAT INTERACTIONS
130 migration.addColumn('chat_interactions', 'formData', {
131 type: Sequelize.TEXT('long')
133 // END CHAT INTERACTIONS
135 // START CHAT MESSAGES
136 migration.addColumn('chat_messages', 'secret', {
137 type: Sequelize.BOOLEAN,
142 // START CHAT WEBSITES
143 migration.addColumn('chat_websites', 'agentAvatar', {
146 migration.addColumn('chat_websites', 'showAgentAvatar', {
147 type: Sequelize.BOOLEAN,
152 //START CUSTOM FIELDS
153 migration.changeColumn('cm_custom_fields', 'values', {
154 type: Sequelize.TEXT('long')
159 migration.final(resolve);
164 down: function(queryInterface, Sequelize) {
165 var migration = new Migration(queryInterface);