-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration-sql-parse.js
36 lines (30 loc) · 959 Bytes
/
migration-sql-parse.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
'use strict';
// Constants
const kDefaultDelimiter = '\n-- MIGRATION DOWN SQL\n';
/**
* @param {String} migrationSql
* @param {String} [optDelimiter='\n-- MIGRATION DOWN SQL\n']
* @returns {Object.<{up, down}}
*/
module.exports = function(migrationSql, optDelimiter = kDefaultDelimiter) {
if (!migrationSql)
throw new Error('Missing / empty migration SQL argument');
if (typeof migrationSql !== 'string')
throw new Error(`migration SQL must be string; got ${typeof migrationSql} instead`);
let delimiter = optDelimiter || exports.kDefaultDelimiter,
delimiterPosition = migrationSql.indexOf(delimiter),
upSql = null,
downSql = null;
if (delimiterPosition >= 0) {
upSql = migrationSql.substr(0, delimiterPosition);
downSql = migrationSql.substr(delimiterPosition);
}
else {
upSql = migrationSql;
}
return {
up: upSql,
down: downSql,
};
};
module.exports.kDefaultDelimiter = kDefaultDelimiter;