Skip to content

Commit 375aaa9

Browse files
authored
Add getDbMajorVersion utility function (sqlpad#855)
* Add getDbMajorVersion utility function * Fix tests for backend dbs * remove change to nedb-to-sqlite test
1 parent 672cbed commit 375aaa9

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

server/lib/make-migrator.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,64 @@ function makeMigrator(config, appLog, nedb, sequelizeInstance) {
2929
migrate() {
3030
return umzug.up();
3131
},
32+
3233
async schemaUpToDate() {
3334
const pending = await umzug.pending();
3435
const upToDate = pending.length === 0;
3536
return upToDate;
3637
},
38+
39+
async getDbMajorVersion() {
40+
const executed = await umzug.executed();
41+
if (executed.length === 0) {
42+
return 0;
43+
}
44+
45+
const migrationFileRegex = /\d\d-\d\d\d\d\d-(\w|-)+\.js/;
46+
47+
const migrationFiles = executed
48+
.map((migration) => migration.file)
49+
.sort()
50+
.filter((name) => {
51+
// regex.test() is stateful if /g is set
52+
// It isn't in use now, but it was during initial dev and caused confusion
53+
// Using string.search() to ensure this stops functioning in case of /g being set in future
54+
return name.search(migrationFileRegex) > -1;
55+
});
56+
57+
const lastMigrationFile = migrationFiles.pop();
58+
59+
// Migration files have format of 'nn-nnnnn-some-text.js'
60+
// Those initial 2 numbers were intended on being the major version...
61+
// but I messed that up pretty eary on 😬
62+
//
63+
// v5 migrations = anything starting with 05, or >= 04-00200
64+
// 04-00200-nedb-sqlite-tables.js
65+
// ... all the way to
66+
// 05-00100-sessions.js
67+
//
68+
// v4 migrations = 04-00000 - 04-00199
69+
// 04-00100-query-acl-schema.js
70+
// ... all the way to
71+
// 04-00129-service-tokens-schema.js
72+
//
73+
const [majorString, minorString] = lastMigrationFile.split('-');
74+
const major = parseInt(majorString, 10);
75+
const minor = parseInt(minorString, 10);
76+
77+
if (major === 5) {
78+
return 5;
79+
}
80+
81+
if (major === 4) {
82+
if (minor >= 200) {
83+
return 5;
84+
}
85+
return 4;
86+
}
87+
88+
return -1;
89+
},
3790
};
3891
}
3992

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable no-await-in-loop */
2+
const assert = require('assert');
3+
const path = require('path');
4+
const ncp = require('ncp').ncp;
5+
const TestUtils = require('../utils');
6+
7+
ncp.limit = 16;
8+
9+
const sourceDir = path.join(__dirname, '../fixtures/v4.2.0-test-db/testdb');
10+
11+
function copyDbFiles(source, destination) {
12+
return new Promise((resolve, reject) => {
13+
ncp(source, destination, function (err) {
14+
if (err) {
15+
return reject(err);
16+
}
17+
return resolve();
18+
});
19+
});
20+
}
21+
22+
describe('migrations/get-db-major-version', function () {
23+
/**
24+
* @type {TestUtils}
25+
*/
26+
let utils;
27+
28+
before('preps the env', async function () {
29+
utils = new TestUtils({
30+
dbPath: path.join(__dirname, '../artifacts/v4-to-v5'),
31+
dbInMemory: false,
32+
// Force this test to only run with SQLite
33+
backendDatabaseUri: '',
34+
});
35+
36+
const destination = utils.config.get('dbPath');
37+
38+
await utils.prepDbDir();
39+
await copyDbFiles(sourceDir, destination);
40+
41+
await utils.initDbs();
42+
});
43+
44+
after(function () {
45+
return utils.sequelizeDb.sequelize.close();
46+
});
47+
48+
it('Before migration - major version is 4', async function () {
49+
const major = await utils.migrator.getDbMajorVersion();
50+
assert.strictEqual(major, 4);
51+
});
52+
53+
it('Migrates', async function () {
54+
await utils.migrate();
55+
});
56+
57+
it('After migration - major is 5', async function () {
58+
const major = await utils.migrator.getDbMajorVersion();
59+
assert.strictEqual(major, 5);
60+
});
61+
});

server/test/migrations/make-migrator.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ describe('lib/make-migrator', function () {
1111
it('Not up-to-date without running mirations', async function () {
1212
const upToDate = await utils.migrator.schemaUpToDate();
1313
assert(!upToDate);
14+
15+
const majorVersion = await utils.migrator.getDbMajorVersion();
16+
assert.strictEqual(majorVersion, 0);
1417
});
1518

1619
it('Up to date after running migrations', async function () {
1720
await utils.migrator.migrate();
1821
const upToDate = await utils.migrator.schemaUpToDate();
1922
assert(upToDate);
23+
24+
const majorVersion = await utils.migrator.getDbMajorVersion();
25+
assert.strictEqual(majorVersion, 5);
2026
});
2127
});

0 commit comments

Comments
 (0)