Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / migrations / 2.5.7.js
1 'use strict';\r
2 \r
3 var BPromise = require('bluebird');\r
4 var util = require('util');\r
5 \r
6 var logger = require('../config/logger')('migration');\r
7 \r
8 var Sequence = function() {};\r
9 \r
10 Sequence.prototype.enqueue = function(fn) {\r
11   this.tail = this.tail ? this.tail.finally(fn) : fn();\r
12 };\r
13 \r
14 var Migration = function(queryInterface) {\r
15   this.queryInterface = queryInterface;\r
16   this.sequence = new Sequence();\r
17 };\r
18 \r
19 Migration.prototype.changeColumn = function(table, column, type) {\r
20   var _this = this;\r
21   this.sequence.enqueue(function() {\r
22     return _this.queryInterface\r
23       .changeColumn(table, column, type)\r
24       .then(function() {\r
25         logger.info('Changed column %s in table %s', column, table);\r
26       })\r
27       .catch(function(err) {\r
28         logger.info(JSON.stringify(err));\r
29       });\r
30   });\r
31 };\r
32 \r
33 Migration.prototype.addColumn = function(table, column, type) {\r
34   var _this = this;\r
35   this.sequence.enqueue(function() {\r
36     return _this.queryInterface\r
37       .addColumn(table, column, type)\r
38       .then(function() {\r
39         logger.info('Added column %s to %s', column, table);\r
40       })\r
41       .catch(function(err) {\r
42         logger.info(JSON.stringify(err));\r
43       });\r
44   });\r
45 };\r
46 \r
47 Migration.prototype.dropTable = function(table) {\r
48   var _this = this;\r
49   this.sequence.enqueue(function() {\r
50     return _this.queryInterface\r
51       .dropTable(table, {\r
52         force: true\r
53       })\r
54       .then(function() {\r
55         logger.info('table dropped %s', table);\r
56       })\r
57       .catch(function(err) {\r
58         logger.info(JSON.stringify(err));\r
59       });\r
60   });\r
61 };\r
62 \r
63 Migration.prototype.addIndex = function(table, column, options) {\r
64   var _this = this;\r
65   this.sequence.enqueue(function() {\r
66     return _this.queryInterface\r
67       .addIndex(table, column, {\r
68         indexName: options.indexName,\r
69         indicesType: options.indicesType\r
70       })\r
71       .then(function() {\r
72         logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);\r
73       })\r
74       .catch(function(err) {\r
75         logger.info(JSON.stringify(err));\r
76       });\r
77   });\r
78 };\r
79 \r
80 Migration.prototype.removeIndex = function(table, indexName) {\r
81   var _this = this;\r
82   this.sequence.enqueue(function() {\r
83     return _this.queryInterface\r
84       .removeIndex(table, indexName)\r
85       .then(function() {\r
86         logger.info('removeIndex %s %s', table, indexName);\r
87       })\r
88       .catch(function(err) {\r
89         logger.info(JSON.stringify(err));\r
90       });\r
91   });\r
92 };\r
93 \r
94 Migration.prototype.query = function(sql) {\r
95   var _this = this;\r
96   this.sequence.enqueue(function() {\r
97     return _this.queryInterface.sequelize\r
98       .query(sql)\r
99       .then(function() {\r
100         logger.info('query %s', sql);\r
101       })\r
102       .catch(function(err) {\r
103         logger.info(JSON.stringify(err));\r
104       });\r
105   });\r
106 };\r
107 \r
108 Migration.prototype.removeColumn = function(table, column) {\r
109   var _this = this;\r
110   this.sequence.enqueue(function() {\r
111     return _this.queryInterface\r
112       .removeColumn(table, column)\r
113       .then(function() {\r
114         logger.info('Removed column %s from %s', column, table);\r
115       })\r
116       .catch(function(err) {\r
117         logger.info(\r
118           util.inspect(err, {\r
119             showHidden: false,\r
120             depth: null\r
121           })\r
122         );\r
123       });\r
124   });\r
125 };\r
126 \r
127 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {\r
128   var _this = this;\r
129   this.sequence.enqueue(function() {\r
130     return _this.queryInterface\r
131       .renameColumn(table, oldColumn, newColumn)\r
132       .then(function() {\r
133         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);\r
134       })\r
135       .catch(function(err) {\r
136         logger.info(\r
137           util.inspect(err, {\r
138             showHidden: false,\r
139             depth: null\r
140           })\r
141         );\r
142       });\r
143   });\r
144 };\r
145 \r
146 Migration.prototype.final = function(resolve) {\r
147   this.sequence.enqueue(function() {\r
148     return resolve();\r
149   });\r
150 };\r
151 \r
152 module.exports = {\r
153   up: function(queryInterface, Sequelize) {\r
154     return new BPromise(function(resolve) {\r
155 \r
156       var migration = new Migration(queryInterface);\r
157 \r
158       //START dialogflow\r
159       migration.addColumn('chat_messages', 'providerName', {\r
160         type: Sequelize.STRING\r
161       });\r
162 \r
163       migration.addColumn('chat_messages', 'providerResponse', {\r
164         type: Sequelize.TEXT\r
165       });\r
166 \r
167       migration.addColumn('openchannel_messages', 'providerName', {\r
168         type: Sequelize.STRING\r
169       });\r
170 \r
171       migration.addColumn('openchannel_messages', 'providerResponse', {\r
172         type: Sequelize.TEXT\r
173       });\r
174 \r
175       migration.addColumn('sms_messages', 'providerName', {\r
176         type: Sequelize.STRING\r
177       });\r
178 \r
179       migration.addColumn('sms_messages', 'providerResponse', {\r
180         type: Sequelize.TEXT\r
181       });\r
182 \r
183       migration.addColumn('whatsapp_messages', 'providerName', {\r
184         type: Sequelize.STRING\r
185       });\r
186 \r
187       migration.addColumn('whatsapp_messages', 'providerResponse', {\r
188         type: Sequelize.TEXT\r
189       });\r
190 \r
191       migration.addColumn('square_messages', 'providerName', {\r
192         type: Sequelize.STRING\r
193       });\r
194 \r
195       migration.addColumn('square_messages', 'providerResponse', {\r
196         type: Sequelize.TEXT\r
197       });\r
198       //END dialogflow\r
199 \r
200       // START FINAL\r
201       migration.final(resolve);\r
202       // END FINAL\r
203     });\r
204   },\r
205 \r
206   down: function(queryInterface, Sequelize) {\r
207     // var migration = new Migration(queryInterface);\r
208   }\r
209 };