forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenumber-db-version.js
75 lines (63 loc) · 2.6 KB
/
renumber-db-version.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
const fs = require('fs');
const child_process = require('child_process');
const yaml = require('js-yaml');
/**
* A very temporary script to renumber DB versions, which is helpful when lots
* of people are writing DB versions.
*
* Run as, for example to renumber version 21 to 22,
*
* node infrastructure/renumber-db-version.js 21 22
*/
const run = async command => {
const proc = child_process.spawn(command[0], command.slice(1), {
stdio: 'inherit',
});
return new Promise((resolve, reject) => {
proc.once('close', code => {
if (code === 0) {
resolve();
} else {
reject(new Error('command failed'));
}
});
proc.once('error', reject);
});
};
const main = async () => {
const fromVersion = process.argv[2].padStart(4, '0');
const toVersion = process.argv[3].padStart(4, '0');
const renames = [];
const fromVersionFile = `db/versions/${fromVersion}.yml`;
const toVersionFile = `db/versions/${toVersion}.yml`;
if (fs.existsSync(toVersionFile)) {
throw new Error(`${toVersionFile} already exists`);
}
renames.push([fromVersionFile, toVersionFile]);
let versionContent = fs.readFileSync(fromVersionFile, 'utf8');
const version = yaml.load(versionContent);
versionContent = versionContent.replace(/^version: \d+/, `version: ${parseInt(toVersion)}`);
if (version.migrationScript && !version.migrationScript.includes('\n')) {
const newMigrationScript = version.migrationScript.replace(fromVersion, toVersion);
console.log( newMigrationScript);
renames.push([`db/versions/${version.migrationScript}`, `db/versions/${newMigrationScript}`]);
versionContent = versionContent.replace(`migrationScript: ${version.migrationScript}`, `migrationScript: ${newMigrationScript}`);
}
if (version.downgradeScript && !version.downgradeScript.includes('\n')) {
const newDowngradeScript = version.downgradeScript.replace(fromVersion, toVersion);
renames.push([`db/versions/${version.downgradeScript}`, `db/versions/${newDowngradeScript}`]);
versionContent = versionContent.replace(`downgradeScript: ${version.downgradeScript}`, `downgradeScript: ${newDowngradeScript}`);
}
const fromTestFile = `db/test/versions/${fromVersion}_test.js`;
if (fs.existsSync(fromTestFile)) {
renames.push([fromTestFile, fromTestFile.replace(fromVersion, toVersion)]);
}
for (let [s, d] of renames) {
console.log(`${s} -> ${d}`);
await run(['git', 'mv', s, d]);
}
console.log(`update ${toVersionFile}`);
fs.writeFileSync(toVersionFile, versionContent, 'utf8');
await run(['git', 'add', toVersionFile]);
};
main().catch(err => console.log(err));