-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrator.js
387 lines (351 loc) · 12.2 KB
/
Migrator.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'use strict';
// Core
const assert = require('assert');
const fs = require('fs');
const path = require('path');
// Local
const migrationSqlParse = require('./migration-sql-parse');
// Constants
const kSavePointName = 'migrations';
const kDefaults = {
pattern: /^\d{14}_\d{4}_[\w-_]+\.sql$/,
modelName: 'MigrationMeta',
tableName: 'migrations_meta',
};
/**
* @param {Array} a
* @param {Array} b
* @returns {Array} elements in ${a} that are not in ${b}
*/
function difference(a, b) {
let bSet = new Set(b);
return a.filter((x) => !bSet.has(x));
}
/**
* Migrator facilitates running and reverting migrations safely. Migrations are stored locally on
* the filesystem. Migrations that have been executed are stored inside a PostgreSQL database table.
* To prevent race conditions between multiple instances attempting to run the same migrations
* simultaneously, an exclusive lock is obtained on the migrations meta table until all migrations
* have been processed.
*
* All migrations are executed within a single transaction that includes the migrations table lock.
* If a migration fails, all previous migrations are persisted (accomplished by using SAVEPOINTs).
*
* Each migration should be a sql file containing both the 'up' and 'down' SQL separated by a
* specific delimiter (by default, is -- DOWN MIGRATION SQL). For example:
*
* -- File: migration.sql
* create table users (
* id serial primary key,
* name text not null,
* password text not null,
* unique(name)
* );
*
* -- DOWN MIGRATION SQL
* drop table users;
*
* Note: Because all migrations are run in their own transaction, it is not necessary to input begin
* / commit clauses inside the migration SQL.
*/
module.exports =
class Migrator {
/**
* @constructor
* @param {Object} sequelize - configured instance of the sequelize library
* @param {Object} options
* @param {String} options.path - local path where migration files are stored
* @param {RegExp} [options.pattern=/^\d{14}_\d{4}_[\w-_]+\.sql$/] - only consider files in ${options.path} that match this pattern as migration files
* @param {String} [options.modelName='MigrationMeta'] - name of sequelize model
* @param {String} [options.tableName='migrations_meta'] - database table where migrations are stored
* @param {String} [options.schema='public']
* @param {Object} [options.logger=null] - bunyan logger instance
*/
constructor(sequelize, options = {}) {
assert(sequelize, 'sequelize argument is required');
assert(options.path, 'options.path argument is required');
this.sequelize_ = sequelize;
this.migrationFilePath_ = options.path;
this.migrationFilePattern_ = options.pattern || kDefaults.pattern;
this.modelName_ = options.modelName || kDefaults.modelName;
this.tableName_ = options.tableName || kDefaults.tableName;
this.schema_ = options.schema || 'public';
this.logger_ = options.logger;
this.migrationFiles_ = null;
this.model_ = this.createModel_();
this.searchPath_ = null;
}
/**
* Runs all pending migrations. Creates the migration table if it does not already exist, and
* exclusively locks the migrations table so that only a single instance may run migrations at a
* given time.
*
* @returns {Promise.<Array.<String>>} - array of migration file names that were executed
*/
async up() {
await this.ensureTableExists_();
return this.sequelize_.transaction(async (transaction) => {
await this.lockTable_(transaction);
const pendingMigrations = await this.pending(transaction);
let numPending = pendingMigrations.length;
if (numPending === 0) {
this.log_('info', 'No migrations are pending');
return null;
}
this.log_('info', `${numPending} pending migration(s)`);
try {
await this.saveSearchPath_(transaction);
await this.setSearchPath_(this.schema_, transaction);
const migrations = await this.migrationsUp_(pendingMigrations, transaction);
await this.setSearchPath_(this.searchPath_, transaction);
this.searchPath_ = null;
this.log_('info', 'Migrations complete');
return migrations.map((migration) => migration.get('name'));
} catch (error) {
this.log_('fatal', {error}, `Unable to perform the last migration: ${error.message}`);
throw error;
}
});
}
/**
* Undoes ${optAmount} migrations in reverse order that they were executed.
*
* @param {Number} [optAmount=1] - number of migrations to rollback
* @returns {Promise.<Array.<String>>}
*/
async down(optAmount = 1) {
await this.ensureTableExists_();
return this.sequelize_.transaction(async (transaction) => {
await this.lockTable_(transaction)
const migrationsToUndo = await this.recentlyExecuted(optAmount, transaction);
let numToUndo = migrationsToUndo.length;
if (numToUndo === 0) {
this.log_('info', 'No migrations found that may be undone');
return null;
}
this.log_('info', `Preparing to undo ${numToUndo} migration(s)`);
migrationsToUndo.reverse();
try {
await this.saveSearchPath_(transaction);
const migrations = await this.migrationsDown_(migrationsToUndo, transaction);
await this.setSearchPath_(this.searchPath_, transaction);
this.searchPath_ = null;
this.log_('info', 'Rollback complete');
return migrations.map((migration) => migration.get('name'));
} catch (error) {
this.log_('fatal', {error}, `Unable to undo the last migration: ${error.message}`);
throw error;
}
});
}
/**
* @returns {Promise.<Array.<String>>} - lexically sorted list of matching migration files
*/
migrationFiles() {
if (this.migrationFiles_) {
return Promise.resolve(this.migrationFiles_);
}
return new Promise((resolve, reject) => {
fs.readdir(this.migrationFilePath_, 'utf8', (error, files) => {
if (error) {
reject(error);
return;
}
resolve(files);
});
}).then((files) => files.filter((x) => this.migrationFilePattern_.test(x)).sort());
}
/**
* @returns {Model} - sequelize migration model
*/
model() {
return this.model_;
}
/**
* @param {Transaction} [optTransaction]
* @returns {Promise.<Array.<Model>>} - ordered array of migration instances that have been executed
*/
executed(optTransaction) {
return this.model_.findAll({
order: [
['id', 'ASC'],
],
attributes: ['id', 'name'],
transaction: optTransaction,
});
}
/**
* @param {Number} [optAmount=1] number of transactions to undo
* @param {Transaction} [optTransaction]
* @returns {Promise.<Array.<Model>>}
*/
async recentlyExecuted(optAmount = 1, optTransaction = null) {
const amount = Math.max(1, optAmount);
const executedMigrations = await this.executed(optTransaction);
return executedMigrations.slice(-amount);
}
/**
* @param {Transaction} [optTransaction]
* @returns {Promise.<Array.<Model>>} - ordered array of migration instances that have not yet been executed
*/
async pending(optTransaction) {
const [migrationFiles, executedMigrations] = await Promise.all([
this.migrationFiles(),
this.executed(optTransaction),
]);
const executedFileNames = executedMigrations.map((x) => x.name);
const pendingMigrationFileNames = difference(migrationFiles, executedFileNames);
pendingMigrationFileNames.sort();
const pendingMigrations = pendingMigrationFileNames.map((x) => this.model_.build({name: x}));
return pendingMigrations;
}
// ----------------------------------------------------
// Private methods
/**
* @returns {Model}
*/
createModel_() {
const fields = {
name: {
type: this.sequelize_.constructor.TEXT,
allowNull: false,
unique: true,
},
};
const params = {
tableName: this.tableName_,
schema: this.schema_,
charset: 'utf8',
timestamps: true,
updatedAt: false,
};
return this.sequelize_.define(this.modelName_, fields, params);
}
/**
* @returns {Promise}
*/
ensureTableExists_() {
return this.model_.sync();
}
/**
* @param {Transaction} transaction
* @returns {Promise}
*/
lockTable_(transaction) {
return this.sequelize_.query(`LOCK TABLE ${this.model_.getTableName()} IN ACCESS EXCLUSIVE MODE`,
{raw: true, transaction});
}
/**
* Convenience method for logging a message if a logger is defined.
*
* @param {string} method the bunyan logger method call (e.g. 'info')
* @param {...*} params arguments to pass to the logger
*/
log_(method, ...params) {
if (this.logger_) {
this.logger_[method](...params);
}
}
/**
* @param {Array.<String>} migrations - list of migration file names to perform
* @param {Transaction} transaction - sequelize transaction
* @returns {Promise.<Array.<Model>>} - list of executed transactions
*/
async migrationsUp_(migrations, transaction) {
for (const migration of migrations) {
this.log_('info', {fileName: migration.name}, ` >> ${migration.name}`);
const migrationSql = await this.parseMigration_(migration.name);
let sql = migrationSql.up;
if (!sql) {
throw new Error(`cannot run migration, ${migration.name}: missing 'up' SQL`);
}
await this.savePoint_(transaction);
try {
await this.sequelize_.query(sql, {raw: true, transaction});
} catch (error) {
await this.rollbackToSavePoint_(transaction);
await transaction.commit();
throw error;
}
await this.releaseSavePoint_(transaction);
await migration.save({transaction});
}
return migrations;
}
/**
* @param {Array.<String>} migrations - list of migration file names to perform
* @param {Transaction} transaction - sequelize transaction
* @returns {Promise.<Array.<Model>>} - list of executed transactions
*/
async migrationsDown_(migrations, transaction) {
for (const migration of migrations) {
this.log_('info', {fileName: migration.name}, ` << Reverting ${migration.name}`);
const migrationSql = await this.parseMigration_(migration.name);
let sql = migrationSql.down;
if (!sql) {
throw new Error(`cannot run migration, ${migration.name}: missing 'down' SQL`);
}
await this.savePoint_(transaction)
try {
await this.sequelize_.query(sql, {raw: true, transaction});
} catch (error) {
await this.rollbackToSavePoint_(transaction);
await transaction.commit();
throw error;
}
await this.releaseSavePoint_(transaction);
await migration.destroy({transaction});
}
return migrations;
}
/**
* @param {String} migrationFileName
* @returns {Promise.<Object>} - migration sql split into up / down SQL chunks
*/
parseMigration_(migrationFileName) {
const migrationFile = path.resolve(this.migrationFilePath_, migrationFileName);
return new Promise((resolve, reject) => {
fs.readFile(migrationFile, 'utf8', (error, sql) => {
if (error) {
reject(error);
return;
}
resolve(sql);
})
}).then(migrationSqlParse);
}
/**
* @param {Transaction} transaction
* @returns {Promise}
*/
savePoint_(transaction) {
return this.sequelize_.query(`SAVEPOINT ${kSavePointName}`, {raw: true, transaction});
}
/**
* @param {Transaction} transaction
* @returns {Promise}
*/
releaseSavePoint_(transaction) {
return this.sequelize_.query(`RELEASE SAVEPOINT ${kSavePointName}`, {raw: true, transaction});
}
/**
* @param {Transaction} transaction
* @returns {Promise}
*/
rollbackToSavePoint_(transaction) {
return this.sequelize_.query(`ROLLBACK TO SAVEPOINT ${kSavePointName}`, {raw: true, transaction});
}
saveSearchPath_(transaction) {
return this.sequelize_.query('SHOW search_path', {raw: true, plain: true, transaction})
.then((result) => {
this.searchPath_ = result.search_path;
});
}
setSearchPath_(searchPath, transaction) {
if (!searchPath) {
return Promise.resolve();
}
this.log_('info', {searchPath}, `Setting search_path to ${searchPath}`);
return this.sequelize_.query(`set search_path to ${searchPath}`, {raw: true, transaction});
}
};