Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.0.30.js
1 'use strict';
2 var BPromise = require('bluebird');
3
4 var logger = require('../config/logger')('migration');
5 var util = require('util');
6
7 var Sequence = function() {};
8
9 Sequence.prototype.enqueue = function(fn) {
10   this.tail = this.tail ? this.tail.finally(fn) : fn();
11 };
12
13 var Migration = function(queryInterface) {
14   this.queryInterface = queryInterface;
15   this.sequence = new Sequence();
16 };
17
18 Migration.prototype.changeColumn = function(table, column, type) {
19   var _this = this;
20   this.sequence.enqueue(function() {
21     return _this.queryInterface
22       .changeColumn(table, column, type)
23       .then(function(res) {
24         logger.info('Changed column %s in table %s', column, table);
25       })
26       .catch(function(err) {
27         logger.info(JSON.stringify(err));
28       });
29   });
30 };
31
32 Migration.prototype.addColumn = function(table, column, type) {
33   var _this = this;
34   this.sequence.enqueue(function() {
35     return _this.queryInterface
36       .addColumn(table, column, type)
37       .then(function(res) {
38         logger.info('Added column %s to %s', column, table);
39       })
40       .catch(function(err) {
41         logger.info(JSON.stringify(err));
42       });
43   });
44 };
45
46 Migration.prototype.dropTable = function(table) {
47   var _this = this;
48   this.sequence.enqueue(function() {
49     return _this.queryInterface
50       .dropTable(table, {
51         force: true
52       })
53       .then(function(res) {
54         logger.info('table dropped %s', table);
55       })
56       .catch(function(err) {
57         logger.info(JSON.stringify(err));
58       });
59   });
60 };
61
62 Migration.prototype.addIndex = function(table, column, indexName) {
63   var _this = this;
64   this.sequence.enqueue(function() {
65     return _this.queryInterface.addIndex(table, column, {
66         indexName: indexName
67       })
68       .then(function(res) {
69         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
70       })
71       .catch(function(err) {
72         logger.info(JSON.stringify(err));
73       });
74   });
75 };
76
77 Migration.prototype.query = function(sql) {
78   var _this = this;
79   this.sequence.enqueue(function() {
80     return _this.queryInterface.sequelize.query(sql)
81       .then(function(res) {
82         logger.info('query %s', sql);
83       })
84       .catch(function(err) {
85         logger.info(JSON.stringify(err));
86       });
87   });
88 };
89
90 Migration.prototype.final = function(resolve) {
91   var _this = this;
92   this.sequence.enqueue(function() {
93     return resolve();
94   });
95 };
96
97 module.exports = {
98
99   up: function(queryInterface, Sequelize) {
100     return new BPromise(function(resolve, reject) {
101
102       var migration = new Migration(queryInterface);
103
104       // START CHAT
105       migration.dropTable('user_has_chat_rooms');
106       migration.dropTable('chat_enquiries');
107       migration.dropTable('chat_messages');
108       migration.dropTable('chat_proactive_actions');
109       migration.dropTable('chat_dispositions');
110       migration.dropTable('chat_applications');
111       migration.dropTable('user_has_chat_interactions');
112       migration.dropTable('chat_interactions');
113       migration.dropTable('team_has_chat_queues');
114       migration.dropTable('user_has_chat_queues');
115       migration.dropTable('chat_queues');
116       migration.dropTable('chat_visitors');
117       migration.dropTable('chat_websites_fields');
118       migration.dropTable('chat_websites');
119       // END CHAT
120
121       migration.changeColumn('voice_queues', 'dialQueueTimeout', {
122         type: Sequelize.INTEGER(3).UNSIGNED,
123         defaultValue: 3,
124         validate: {
125           min: 1,
126           max: 999
127         },
128         comment: 'Queue Timeout Seconds (min:1, max:999)'
129       });
130       // START FINAL
131       migration.final(resolve);
132       // END FINAL
133
134     });
135
136   },
137
138   down: function(queryInterface, Sequelize) {
139     var migration = new Migration(queryInterface);
140   }
141 };