3 var BPromise = require('bluebird');
4 var rs = require('randomstring');
5 var util = require('util');
7 var logger = require('../config/logger')('migration');
9 var Sequence = function () {};
11 Sequence.prototype.enqueue = function (fn) {
12 this.tail = this.tail ? this.tail.finally(fn) : fn();
15 var Migration = function (queryInterface) {
16 this.queryInterface = queryInterface;
17 this.sequence = new Sequence();
20 Migration.prototype.changeColumn = function (table, column, type) {
22 this.sequence.enqueue(function () {
23 return _this.queryInterface
24 .changeColumn(table, column, type)
26 logger.info('Changed column %s in table %s', column, table);
28 .catch(function (err) {
29 logger.info(JSON.stringify(err));
34 Migration.prototype.addColumn = function (table, column, type) {
36 this.sequence.enqueue(function () {
37 return _this.queryInterface
38 .addColumn(table, column, type)
40 logger.info('Added column %s to %s', column, table);
42 .catch(function (err) {
43 logger.info(JSON.stringify(err));
48 Migration.prototype.dropTable = function (table) {
50 this.sequence.enqueue(function () {
51 return _this.queryInterface
56 logger.info('table dropped %s', table);
58 .catch(function (err) {
59 logger.info(JSON.stringify(err));
64 Migration.prototype.addIndex = function (table, column, indexName) {
66 this.sequence.enqueue(function () {
67 return _this.queryInterface.addIndex(table, column, {
71 logger.info('addIndex %s %s %s', table, column.join(','), indexName);
73 .catch(function (err) {
74 logger.info(JSON.stringify(err));
79 Migration.prototype.removeIndex = function (table, indexName) {
81 this.sequence.enqueue(function () {
82 return _this.queryInterface.removeIndex(table, indexName)
84 logger.info('removeIndex %s %s', table, indexName);
86 .catch(function (err) {
87 logger.info(JSON.stringify(err));
92 Migration.prototype.query = function (sql) {
94 this.sequence.enqueue(function () {
95 return _this.queryInterface.sequelize.query(sql)
97 logger.info('query %s', sql);
99 .catch(function (err) {
100 logger.info(JSON.stringify(err));
105 Migration.prototype.removeColumn = function (table, column) {
107 this.sequence.enqueue(function () {
108 return _this.queryInterface.removeColumn(table, column)
110 logger.info('Removed column %s from %s', column, table);
112 .catch(function (err) {
113 logger.info(util.inspect(err, {
121 Migration.prototype.renameColumn = function (table, oldColumn, newColumn) {
123 this.sequence.enqueue(function () {
124 return _this.queryInterface.renameColumn(table, oldColumn, newColumn)
126 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
128 .catch(function (err) {
129 logger.info(util.inspect(err, {
137 Migration.prototype.final = function (resolve) {
138 this.sequence.enqueue(function () {
144 up: function (queryInterface, Sequelize) {
145 return new BPromise(function (resolve) {
146 var migration = new Migration(queryInterface);
148 // START analytics_report_fields
149 migration.changeColumn('analytics_report_fields', 'field', {
152 // END analytics_report_fields
154 // START tools_schedules
155 migration.addColumn('tools_schedules', 'cc', {
159 migration.addColumn('tools_schedules', 'bcc', {
163 migration.addColumn('settings', 'split', {
164 type: Sequelize.BOOLEAN,
168 migration.addColumn('settings', 'splitSizeCsv', {
169 type: Sequelize.INTEGER,
173 migration.addColumn('settings', 'splitSizePdf', {
174 type: Sequelize.INTEGER,
178 migration.addColumn('settings', 'splitSizeXlsx', {
179 type: Sequelize.INTEGER,
182 // END tools_schedules
184 // START mail_accounts
185 migration.addColumn('mail_accounts', 'queueTransfer', {
186 type: Sequelize.BOOLEAN,
190 migration.addColumn('mail_accounts', 'queueTransferTimeout', {
191 type: Sequelize.INTEGER,
199 migration.addColumn('mail_accounts', 'agentTransfer', {
200 type: Sequelize.BOOLEAN,
204 migration.addColumn('mail_accounts', 'agentTransferTimeout', {
205 type: Sequelize.INTEGER,
215 migration.addColumn('users', 'phoneBarPrefixRequired', {
216 type: Sequelize.BOOLEAN,
221 // START mail_interactions
222 migration.changeColumn('mail_interactions', 'subject', {
223 type: Sequelize.TEXT('') + ' CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci'
226 migration.changeColumn('mail_interactions', 'lastMsgBody', {
227 type: Sequelize.TEXT('long') + ' CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci'
229 // END mail_interactions
232 migration.addColumn('cm_contacts', 'viber', {
233 type: Sequelize.STRING
238 migration.final(resolve);
243 down: function (queryInterface, Sequelize) {
244 // var migration = new Migration(queryInterface);