Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.0.47.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4
5 var logger = require('../config/logger')('migration');
6 var util = require('util');
7
8 var Sequence = function() {};
9
10 Sequence.prototype.enqueue = function(fn) {
11   this.tail = this.tail ? this.tail.finally(fn) : fn();
12 };
13
14 var Migration = function(queryInterface) {
15   this.queryInterface = queryInterface;
16   this.sequence = new Sequence();
17 };
18
19 Migration.prototype.changeColumn = function(table, column, type) {
20   var _this = this;
21   this.sequence.enqueue(function() {
22     return _this.queryInterface
23       .changeColumn(table, column, type)
24       .then(function(res) {
25         logger.info('Changed column %s in table %s', column, table);
26       })
27       .catch(function(err) {
28         logger.info(JSON.stringify(err));
29       });
30   });
31 };
32
33 Migration.prototype.addColumn = function(table, column, type) {
34   var _this = this;
35   this.sequence.enqueue(function() {
36     return _this.queryInterface
37       .addColumn(table, column, type)
38       .then(function(res) {
39         logger.info('Added column %s to %s', column, table);
40       })
41       .catch(function(err) {
42         logger.info(JSON.stringify(err));
43       });
44   });
45 };
46
47 Migration.prototype.dropTable = function(table) {
48   var _this = this;
49   this.sequence.enqueue(function() {
50     return _this.queryInterface
51       .dropTable(table, {
52         force: true
53       })
54       .then(function(res) {
55         logger.info('table dropped %s', table);
56       })
57       .catch(function(err) {
58         logger.info(JSON.stringify(err));
59       });
60   });
61 };
62
63 Migration.prototype.addIndex = function(table, column, indexName) {
64   var _this = this;
65   this.sequence.enqueue(function() {
66     return _this.queryInterface.addIndex(table, column, {
67         indexName: indexName
68       })
69       .then(function(res) {
70         logger.info('addIndex %s %s %s', table, column.join(','), indexName);
71       })
72       .catch(function(err) {
73         logger.info(JSON.stringify(err));
74       });
75   });
76 };
77
78 Migration.prototype.query = function(sql) {
79   var _this = this;
80   this.sequence.enqueue(function() {
81     return _this.queryInterface.sequelize.query(sql)
82       .then(function(res) {
83         logger.info('query %s', sql);
84       })
85       .catch(function(err) {
86         logger.info(JSON.stringify(err));
87       });
88   });
89 };
90
91 Migration.prototype.removeColumn = function(table, column) {
92   var _this = this;
93   this.sequence.enqueue(function() {
94     return _this.queryInterface.removeColumn(table, column)
95       .then(function(res) {
96         logger.info('Removed column %s from %s', column, table);
97       })
98       .catch(function(err) {
99         logger.info(util.inspect(err, {
100           showHidden: false,
101           depth: null
102         }));
103       });
104   });
105 };
106
107 Migration.prototype.final = function(resolve) {
108   var _this = this;
109   this.sequence.enqueue(function() {
110     return resolve();
111   });
112 };
113
114 module.exports = {
115   up: function(queryInterface, Sequelize) {
116     return new BPromise(function(resolve, reject) {
117       var migration = new Migration(queryInterface);
118
119       // START DASHBOARD
120       migration.query("DELETE FROM dashboards");
121
122       migration.removeColumn('dashboards', 'interval');
123
124
125       migration.changeColumn('dashboard_items', 'type', {
126         type: Sequelize.STRING(20),
127         allowNull: false,
128         validate: {
129           len: [1, 20]
130         }
131       });
132       migration.removeColumn('dashboard_items', 'query');
133       migration.removeColumn('dashboard_items', 'data');
134
135       migration.addColumn('dashboard_items', 'title', {
136         type: Sequelize.STRING(20),
137         validate: {
138           len: [0, 20]
139         }
140       });
141
142       migration.addColumn('dashboard_items', 'attrs', {
143         type: Sequelize.TEXT('long')
144       });
145       // END DASHBOARD
146
147       // START SMS MESSAGES
148       migration.addColumn('sms_messages', 'secret', {
149         type: Sequelize.BOOLEAN,
150         defaultValue: false
151       });
152       // END SMS MESSAGES
153
154       // START CHATWEBSITE
155       migration.addColumn('chat_websites', 'formSubmitSuccessMessage', {
156         type: Sequelize.STRING,
157         defaultValue: 'Form properly submitted'
158       });
159
160       migration.addColumn('chat_websites', 'formSubmitFailureMessage', {
161         type: Sequelize.STRING,
162         defaultValue: 'Form submission error'
163       });
164       // END CHATWEBSITE
165
166
167       // START FINAL
168       migration.final(resolve);
169       // END FINAL
170     });
171   },
172
173   down: function(queryInterface, Sequelize) {
174     var migration = new Migration(queryInterface);
175   }
176 };