-
-
Notifications
You must be signed in to change notification settings - Fork 528
/
db.js
249 lines (225 loc) · 7.48 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"use strict";
var Bluebird = require("bluebird");
var fs = require("fs");
var path = require("path");
var helpers = require(path.resolve(__dirname, "..", "helpers"));
var args = require("yargs").argv;
var _ = require("lodash");
var Sequelize = helpers.generic.getSequelize();
var Umzug = require("umzug");
module.exports = {
"db:migrate": {
descriptions: {
"short": "Run pending migrations.",
"long": [
"The command runs every not yet executed migration."
]
},
task: function() {
getMigrator(function(migrator) {
ensureCurrentMetaSchema(migrator).then(function () {
return migrator.pending();
}).then(function(migrations) {
if (migrations.length === 0) {
console.log("No executed migrations found.");
process.exit(0);
}
}).then(function () {
return migrator.up();
}).then(function () {
process.exit(0);
});
});
}
},
"db:migrate:undo": {
descriptions: {
"short": "Revert the last migration run."
},
task: function() {
getMigrator(function(migrator) {
ensureCurrentMetaSchema(migrator).then(function () {
return migrator.executed();
}).then(function (migrations) {
if (migrations.length === 0) {
console.log("No executed migrations found.");
process.exit(0);
}
}).then(function () {
return migrator.down();
}).then(function () {
process.exit(0);
});
});
}
},
"db:migrate:old_schema": {
descriptions: {
"short": "Update legacy migration table",
"long": [
"This command updates the legacy structure of the SequelizeMeta table to",
"the newer version which is shipped with the migrator engine 'umzug' and",
"which is the required structure of the CLI since version 1.0.0.",
"",
"In details this means, that the previous structure (id:int, from:string,",
"to:string) gets migrated to the new structure (name:string). As the old",
"structure does not actually contain all single migration names but only",
"certain ranges, the script will take a look at the migrations folder and",
"generate the single migration names accordingly.",
"",
"Please note that the script will create a backup of your old table schema",
"table by renaming the original table to SequelizeMetaBackup."
]
},
task: function () {
var sequelize = getSequelizeInstance();
var queryInterface = sequelize.getQueryInterface();
queryInterface.showAllTables().then(function(tableNames) {
if (tableNames.indexOf("SequelizeMeta") === -1) {
console.log("No SequelizeMeta table found.");
process.exit(1);
}
}).then(function () {
return queryInterface.describeTable("SequelizeMeta");
}).then(function (table) {
if (JSON.stringify(Object.keys(table).sort()) !== JSON.stringify(["id", "from", "to"])) {
return queryInterface
.renameTable("SequelizeMeta", "SequelizeMetaBackup")
.then(function () {
var sql = queryInterface.QueryGenerator.selectQuery("SequelizeMetaBackup");
return sequelize.query(sql, null, { raw: true });
})
.then(function (result) {
var timestamps = result.map(function (item) { return item.to; });
var files = fs.readdirSync(helpers.path.getMigrationsPath());
return files.filter(function (file) {
var timestamp = file.match(/(\d+)-/)[1];
return timestamps.indexOf(timestamp) > -1;
});
})
.then(function (files) {
var SequelizeMeta = sequelize.define("SequelizeMeta", {
name: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
primaryKey: true,
autoIncrement: false
}
}, {
tableName: "SequelizeMeta",
timestamps: false
});
return SequelizeMeta.sync().then(function () {
return SequelizeMeta.bulkCreate(
files.map(function (file) {
return { name: file };
})
);
});
}).then(function (items) {
console.log("Successfully migrated " + items.length + " migrations.");
process.exit(0);
});
}
});
}
}
};
function ensureCurrentMetaSchema (migrator) {
var sequelize = migrator.options.storageOptions.sequelize;
var columnName = migrator.options.storageOptions.columnName;
return sequelize.getQueryInterface()
.showAllTables()
.then(function (tables) {
if (tables.indexOf("SequelizeMeta") === -1) {
return;
}
return sequelize.queryInterface
.describeTable("SequelizeMeta")
.then(function (table) {
var columns = Object.keys(table);
if ((columns.length === 1) && (columns[0] === columnName)) {
return;
} else {
console.error(
"Database schema was not migrated. Please run " +
"'sequelize db:migrate:old_schema' first."
);
process.exit(1);
}
});
});
}
function logMigrator (s) {
if (s.indexOf("Executing") !== 0) {
helpers.view.log(s);
}
}
function getSequelizeInstance () {
var config = null;
var options = {};
try {
config = helpers.config.readConfig();
} catch(e) {
console.log(e.message);
process.exit(1);
}
_.each(config, function(value, key) {
if (["database", "username", "password"].indexOf(key) === -1) {
options[key] = value;
}
if (key === "use_env_variable") {
if (process.env[value]) {
var db_info = process.env[value].match(/([^:]+):\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/);
config.database = db_info[6];
config.username = db_info[2];
config.password = db_info[3];
options = _.extend(options, {
host: db_info[4],
port: db_info[5],
dialect: db_info[1],
protocol: db_info[1]
});
}
}
});
options = _.extend({ logging: logMigrator }, options);
return new Sequelize(config.database, config.username, config.password, options);
}
function getMigrator (callback) {
if (helpers.config.configFileExists() || args.url) {
var sequelize = getSequelizeInstance();
var migrator = new Umzug({
storage: "sequelize",
storageOptions: { sequelize: sequelize },
migrations: {
params: [ sequelize.getQueryInterface(), Sequelize ],
path: helpers.path.getMigrationsPath(),
pattern: helpers.config.supportsCoffee() ? /\.js$|\.coffee$/ : /\.js$/,
wrap: function (fun) {
if (fun.length === 3) {
return Bluebird.promisify(fun);
} else {
return fun;
}
}
}
});
sequelize
.authenticate()
.success(function() {
callback(migrator);
})
.error(function (err) {
console.error("Unable to connect to database: " + err);
process.exit(1);
});
} else {
console.log(
"Cannot find '" + helpers.config.getConfigFile() +
"'. Have you run 'sequelize init'?"
);
process.exit(1);
}
}