forked from boujeepossum/node-sql-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration-provider.js
More file actions
27 lines (24 loc) · 901 Bytes
/
migration-provider.js
File metadata and controls
27 lines (24 loc) · 901 Bytes
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
var fs = require('fs');
var path = require('path');
module.exports = function (config) {
return {
getMigrationsList: function () {
return fs.readdirSync(config.migrationsDir);
},
/**
* @param {Array} migrationsList list of filenames
* @param {Array<Number>} ids of applied migration from db table
* @return {Array} of migration filenames that have not been applied
*/
getSql: function (migration) {
var sql = fs.readFileSync(path.join(config.migrationsDir, migration)).toString();
Object.keys(config.parameters || {}).forEach(function (key) {
sql = sql.replace(new RegExp(escapeRegExp(key), "g"), config.parameters[key]);
});
return sql;
}
};
};
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}