3 var BPromise = require('bluebird');
4 var util = require('util');
6 var logger = require('../config/logger')('migration');
8 var Sequence = function () {};
10 Sequence.prototype.enqueue = function (fn) {
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
14 var Migration = function (queryInterface) {
15 this.queryInterface = queryInterface;
16 this.sequence = new Sequence();
19 Migration.prototype.changeColumn = function (table, column, type) {
21 this.sequence.enqueue(function () {
22 return _this.queryInterface
23 .changeColumn(table, column, type)
25 logger.info('Changed column %s in table %s', column, table);
27 .catch(function (err) {
28 logger.info(JSON.stringify(err));
33 Migration.prototype.addColumn = function (table, column, type) {
35 this.sequence.enqueue(function () {
36 return _this.queryInterface
37 .addColumn(table, column, type)
39 logger.info('Added column %s to %s', column, table);
41 .catch(function (err) {
42 logger.info(JSON.stringify(err));
47 Migration.prototype.dropTable = function (table) {
49 this.sequence.enqueue(function () {
50 return _this.queryInterface
55 logger.info('table dropped %s', table);
57 .catch(function (err) {
58 logger.info(JSON.stringify(err));
63 Migration.prototype.addIndex = function (table, column, options) {
65 this.sequence.enqueue(function () {
66 return _this.queryInterface
67 .addIndex(table, column, {
68 indexName: options.indexName,
69 indicesType: options.indicesType
72 logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);
74 .catch(function (err) {
75 logger.info(JSON.stringify(err));
80 Migration.prototype.removeIndex = function (table, indexName) {
82 this.sequence.enqueue(function () {
83 return _this.queryInterface
84 .removeIndex(table, indexName)
86 logger.info('removeIndex %s %s', table, indexName);
88 .catch(function (err) {
89 logger.info(JSON.stringify(err));
94 Migration.prototype.query = function (sql) {
96 this.sequence.enqueue(function () {
97 return _this.queryInterface.sequelize
100 logger.info('query %s', sql);
102 .catch(function (err) {
103 logger.info(JSON.stringify(err));
108 Migration.prototype.removeColumn = function (table, column) {
110 this.sequence.enqueue(function () {
111 return _this.queryInterface
112 .removeColumn(table, column)
114 logger.info('Removed column %s from %s', column, table);
116 .catch(function (err) {
127 Migration.prototype.renameColumn = function (table, oldColumn, newColumn) {
129 this.sequence.enqueue(function () {
130 return _this.queryInterface
131 .renameColumn(table, oldColumn, newColumn)
133 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
135 .catch(function (err) {
146 Migration.prototype.final = function (resolve) {
147 this.sequence.enqueue(function () {
153 up: function (queryInterface, Sequelize) {
154 return new BPromise(function (resolve) {
156 var migration = new Migration(queryInterface);
158 //START int_zoho_configurations
159 migration.addColumn('int_zoho_configurations', 'moduleSearch', {
160 type: Sequelize.ENUM('contact_lead', 'contact', 'lead'),
161 defaultValue: 'contact_lead'
164 migration.changeColumn('int_zoho_configurations', 'moduleCreate', {
165 type: Sequelize.ENUM('Leads', 'Contacts', 'nothing', 'contact', 'lead')
168 migration.query('UPDATE int_zoho_configurations SET moduleCreate = \'contact\' WHERE moduleCreate = \'Contacts\'');
170 migration.query('UPDATE int_zoho_configurations SET moduleCreate = \'lead\' WHERE moduleCreate = \'Leads\'');
172 migration.changeColumn('int_zoho_configurations', 'moduleCreate', {
173 type: Sequelize.ENUM('nothing', 'contact', 'lead'),
176 //END int_zoho_configurations
181 migration.addColumn('network', 'username', {
182 type: Sequelize.STRING
184 migration.addColumn('network', 'password', {
185 type: Sequelize.STRING
187 migration.changeColumn('network', 'type', {
188 type: Sequelize.ENUM('localnet', 'externip', 'stun', 'turn')
190 migration.query("INSERT INTO network (type,value, createdAt, updatedAt) SELECT 'stun', stunaddr, current_timestamp(), current_timestamp() from settings WHERE stunaddr<>''");
191 migration.query("INSERT INTO network (type,value,username,password, createdAt, updatedAt) SELECT 'turn', turnaddr, turnusername, turnpassword, current_timestamp(), current_timestamp() from settings WHERE turnaddr<>''");
195 migration.final(resolve);
200 down: function (queryInterface, Sequelize) {
201 // var migration = new Migration(queryInterface);