3 var BPromise = require('bluebird');
\r
4 var util = require('util');
\r
6 var logger = require('../config/logger')('migration');
\r
8 var Sequence = function () { };
\r
10 Sequence.prototype.enqueue = function (fn) {
\r
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
\r
14 var Migration = function (queryInterface) {
\r
15 this.queryInterface = queryInterface;
\r
16 this.sequence = new Sequence();
\r
19 Migration.prototype.changeColumn = function (table, column, type) {
\r
21 this.sequence.enqueue(function () {
\r
22 return _this.queryInterface
\r
23 .changeColumn(table, column, type)
\r
25 logger.info('Changed column %s in table %s', column, table);
\r
27 .catch(function (err) {
\r
28 logger.info(JSON.stringify(err));
\r
33 Migration.prototype.addColumn = function (table, column, type) {
\r
35 this.sequence.enqueue(function () {
\r
36 return _this.queryInterface
\r
37 .addColumn(table, column, type)
\r
39 logger.info('Added column %s to %s', column, table);
\r
41 .catch(function (err) {
\r
42 logger.info(JSON.stringify(err));
\r
47 Migration.prototype.dropTable = function (table) {
\r
49 this.sequence.enqueue(function () {
\r
50 return _this.queryInterface
\r
55 logger.info('table dropped %s', table);
\r
57 .catch(function (err) {
\r
58 logger.info(JSON.stringify(err));
\r
63 Migration.prototype.addIndex = function (table, column, indexName) {
\r
65 this.sequence.enqueue(function () {
\r
66 return _this.queryInterface
\r
67 .addIndex(table, column, {
\r
68 indexName: indexName
\r
71 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
\r
73 .catch(function (err) {
\r
74 logger.info(JSON.stringify(err));
\r
79 Migration.prototype.removeIndex = function (table, indexName) {
\r
81 this.sequence.enqueue(function () {
\r
82 return _this.queryInterface
\r
83 .removeIndex(table, indexName)
\r
85 logger.info('removeIndex %s %s', table, indexName);
\r
87 .catch(function (err) {
\r
88 logger.info(JSON.stringify(err));
\r
93 Migration.prototype.query = function (sql) {
\r
95 this.sequence.enqueue(function () {
\r
96 return _this.queryInterface.sequelize
\r
99 logger.info('query %s', sql);
\r
101 .catch(function (err) {
\r
102 logger.info(JSON.stringify(err));
\r
107 Migration.prototype.removeColumn = function (table, column) {
\r
109 this.sequence.enqueue(function () {
\r
110 return _this.queryInterface
\r
111 .removeColumn(table, column)
\r
112 .then(function () {
\r
113 logger.info('Removed column %s from %s', column, table);
\r
115 .catch(function (err) {
\r
117 util.inspect(err, {
\r
126 Migration.prototype.renameColumn = function (table, oldColumn, newColumn) {
\r
128 this.sequence.enqueue(function () {
\r
129 return _this.queryInterface
\r
130 .renameColumn(table, oldColumn, newColumn)
\r
131 .then(function () {
\r
132 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
\r
134 .catch(function (err) {
\r
136 util.inspect(err, {
\r
145 Migration.prototype.final = function (resolve) {
\r
146 this.sequence.enqueue(function () {
\r
152 up: function (queryInterface, Sequelize) {
\r
153 return new BPromise(function (resolve) {
\r
154 var migration = new Migration(queryInterface);
\r
157 migration.addColumn('settings', 'chatInteractionsReloadTimeout', {
\r
158 type: Sequelize.INTEGER,
\r
162 migration.addColumn('settings', 'mailInteractionsReloadTimeout', {
\r
163 type: Sequelize.INTEGER,
\r
167 migration.addColumn('settings', 'openchannelInteractionsReloadTimeout', {
\r
168 type: Sequelize.INTEGER,
\r
172 migration.addColumn('settings', 'smsInteractionsReloadTimeout', {
\r
173 type: Sequelize.INTEGER,
\r
177 migration.addColumn('settings', 'whatsappInteractionsReloadTimeout', {
\r
178 type: Sequelize.INTEGER,
\r
183 // START mail_applications
\r
184 migration.query('update mail_applications \
\r
185 set appdata = CONCAT(\'1,1,\', (select SUBSTRING(t.appdata,3) from ( \
\r
186 select * from mail_applications \
\r
187 where app = \'autoreply\' and appdata LIKE \'1,%\' LIMIT 1) as t)) \
\r
188 where app = \'autoreply\' and (appdata LIKE \'1,%\' and SUBSTRING(appdata,1,4) <> \'1,1,\')');
\r
190 migration.query('update mail_applications \
\r
191 set appdata = CONCAT(\'0,1,\', (select SUBSTRING(t.appdata,3) from ( \
\r
192 select * from mail_applications \
\r
193 where app = \'autoreply\' and appdata LIKE \'0,%\' LIMIT 1) as t)) \
\r
194 where app = \'autoreply\' and (appdata LIKE \'0,%\' and SUBSTRING(appdata,1,4) <> \'0,1,\')');
\r
195 // END mail_applications
\r
198 migration.final(resolve);
\r
203 down: function (queryInterface, Sequelize) {
\r
204 // var migration = new Migration(queryInterface);
\r