Built motion from commit (unavailable).|2.5.23
[motion2.git] / server / migrations / 2.5.13.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 \r
159       // START FINAL\r
160       migration.final(resolve);\r
161       // END FINAL\r
162     });\r
163   },\r
164 \r
165   down: function (queryInterface, Sequelize) {\r
166     // var migration = new Migration(queryInterface);\r
167   }\r
168 };\r