@hop-top/kit 0.5.0-alpha.11 → 0.5.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.md +5 -5
- package/dist/bus-D2bAOPYz.d.ts +55 -0
- package/dist/bus.d.ts +1 -0
- package/dist/bus.js +13 -0
- package/dist/chunk-74E6RNOI.js +82 -0
- package/dist/chunk-B7XTFDBB.js +17 -0
- package/dist/chunk-CAONBSPO.js +497 -0
- package/dist/chunk-F7RSL4N4.js +88 -0
- package/dist/chunk-I6XDPSXQ.js +75 -0
- package/dist/chunk-IDM6YZXN.js +204 -0
- package/dist/chunk-P3Y46PYF.js +138 -0
- package/dist/chunk-VKQCOVKI.js +880 -0
- package/dist/cli.js +3 -3
- package/dist/id/index.d.ts +1 -1
- package/dist/id/index.js +4 -2
- package/dist/index-BqZ7WGQf.d.ts +731 -0
- package/dist/{index-VLDcI5NF.d.ts → index-D9zpUrpE.d.ts} +18 -6
- package/dist/index.d.ts +5 -2
- package/dist/index.js +27 -13
- package/dist/upgrade/drivers/configfile.d.ts +45 -0
- package/dist/upgrade/drivers/configfile.js +78 -0
- package/dist/upgrade/drivers/fsdir.d.ts +61 -0
- package/dist/upgrade/drivers/fsdir.js +170 -0
- package/dist/upgrade/drivers/postgres.d.ts +121 -0
- package/dist/upgrade/drivers/postgres.js +195 -0
- package/dist/upgrade/drivers/sqlite.d.ts +101 -0
- package/dist/upgrade/drivers/sqlite.js +241 -0
- package/dist/upgrade/index.d.ts +2 -0
- package/dist/upgrade/index.js +73 -0
- package/dist/uri-IJ_AoY3d.d.ts +144 -0
- package/dist/uri.d.ts +1 -0
- package/dist/uri.js +37 -0
- package/dist/version-DEJ3pbiO.d.ts +10 -0
- package/dist/wizard-Bf_qHc31.d.ts +159 -0
- package/dist/wizard.d.ts +1 -0
- package/dist/wizard.js +39 -0
- package/package.json +78 -19
- package/skills/hop-top-kit-ts/.claude-plugin/plugin.json +19 -0
- package/skills/hop-top-kit-ts/SKILL.md +117 -0
- package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.expected +2 -0
- package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.ts +20 -0
- package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.expected +13 -0
- package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.ts +26 -0
- package/skills/hop-top-kit-ts/gym.json +5 -0
- package/skills/hop-top-kit-ts/references/ai-and-agents.md +45 -0
- package/skills/hop-top-kit-ts/references/cli-and-console.md +63 -0
- package/skills/hop-top-kit-ts/references/coverage.md +56 -0
- package/skills/hop-top-kit-ts/references/guardrails-and-trust.md +60 -0
- package/skills/hop-top-kit-ts/references/other-ports.md +31 -0
- package/skills/hop-top-kit-ts/references/serving-and-transports.md +49 -0
- package/skills/hop-top-kit-ts/scripts/detect-port.sh +168 -0
- package/src/id/README.md +2 -2
- package/src/id/contract.test.ts +11 -8
- package/src/id/differential.test.ts +54 -0
- package/src/id/index.ts +123 -17
- package/src/index.test.ts +48 -2
- package/src/index.ts +6 -1
- package/src/published-types.test.ts +8 -0
- package/src/serve.stopped-order.test.ts +156 -0
- package/src/upgrade/README.md +42 -0
- package/src/upgrade/backup.ts +174 -0
- package/src/upgrade/conformance.test.ts +310 -0
- package/src/upgrade/document.ts +229 -0
- package/src/upgrade/driver.ts +66 -0
- package/src/upgrade/drivers/README.md +47 -0
- package/src/upgrade/drivers/configfile.test.ts +112 -0
- package/src/upgrade/drivers/configfile.ts +107 -0
- package/src/upgrade/drivers/fsdir.test.ts +188 -0
- package/src/upgrade/drivers/fsdir.ts +216 -0
- package/src/upgrade/drivers/isolation.test.ts +18 -0
- package/src/upgrade/drivers/postgres.integration.test.ts +395 -0
- package/src/upgrade/drivers/postgres.test.ts +272 -0
- package/src/upgrade/drivers/postgres.ts +284 -0
- package/src/upgrade/drivers/sqlite.integration.test.ts +492 -0
- package/src/upgrade/drivers/sqlite.ts +300 -0
- package/src/upgrade/drivers/version.ts +21 -0
- package/src/upgrade/errors.ts +117 -0
- package/src/upgrade/events.ts +117 -0
- package/src/{upgrade.test.ts → upgrade/index.test.ts} +102 -1
- package/src/{upgrade.ts → upgrade/index.ts} +96 -27
- package/src/upgrade/migration.ts +89 -0
- package/src/upgrade/migrator.test.ts +391 -0
- package/src/upgrade/migrator.ts +527 -0
- package/src/upgrade/options.ts +59 -0
- package/src/upgrade/semver.ts +90 -0
- package/test/skill-snippets.test.ts +60 -0
- package/test/upgrade/conformance-migrator.ts +439 -0
- package/test/upgrade/conformance-support.ts +44 -0
- package/test/upgrade/driver-support.ts +44 -0
- package/test/upgrade/pg-container.ts +64 -0
- package/test/upgrade/pg-race-worker.ts +48 -0
- package/test/upgrade/race-helper.ts +36 -0
- package/test/upgrade/sqlite-support.ts +41 -0
- package/test/upgrade/testdata/go-fsdir-mydata.tar.gz +0 -0
- package/vitest.config.ts +26 -0
- package/dist/chunk-4GPCWBBA.js +0 -143
- package/dist/chunk-L23O2FFE.js +0 -51
- package/dist/upgrade-C3RWGwRi.d.ts +0 -125
- package/dist/upgrade.d.ts +0 -1
- package/dist/upgrade.js +0 -7
- package/dist/{chunk-MFVUENZR.js → chunk-KV7FYEAF.js} +2 -2
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
// Needs the native better-sqlite3 build: run by `make test-ts-upgrade-integration`.
|
|
2
|
+
|
|
3
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import Database from 'better-sqlite3';
|
|
9
|
+
import { buildSync } from 'esbuild';
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
11
|
+
|
|
12
|
+
import { tempDir } from '../../../test/upgrade/driver-support';
|
|
13
|
+
import { insert, integrity, openWAL, rows } from '../../../test/upgrade/sqlite-support';
|
|
14
|
+
import {
|
|
15
|
+
BackupUnsupportedError,
|
|
16
|
+
Migrator,
|
|
17
|
+
Registry,
|
|
18
|
+
type EventPublisher,
|
|
19
|
+
isBackupUnsupported,
|
|
20
|
+
isCanceled,
|
|
21
|
+
isNotRestored,
|
|
22
|
+
readVersionFile,
|
|
23
|
+
type SchemaDriver,
|
|
24
|
+
} from '../index';
|
|
25
|
+
import { SqliteDriver } from './sqlite';
|
|
26
|
+
|
|
27
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '../../..');
|
|
28
|
+
|
|
29
|
+
let tmp: ReturnType<typeof tempDir>;
|
|
30
|
+
let dbPath: string;
|
|
31
|
+
let data: string;
|
|
32
|
+
let db: Database.Database;
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
tmp = tempDir();
|
|
35
|
+
dbPath = join(tmp.dir, 'app.db');
|
|
36
|
+
data = join(tmp.dir, 'data');
|
|
37
|
+
db = openWAL(dbPath); // at 1.0.0, row 1 still in the WAL
|
|
38
|
+
insert(db, 1);
|
|
39
|
+
mkdirSync(join(data, 'appdb'), { recursive: true });
|
|
40
|
+
writeFileSync(join(data, 'appdb', 'version'), '1.0.0');
|
|
41
|
+
});
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
if (db.open) db.close();
|
|
44
|
+
tmp.cleanup();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const driver = (extra: ConstructorParameters<typeof SqliteDriver>[1] = {}): SqliteDriver =>
|
|
48
|
+
new SqliteDriver(dbPath, { name: 'appdb', dataDir: data, ...extra });
|
|
49
|
+
const sidecars = (p: string): string[] => ['-wal', '-shm', '-journal'].map((s) => p + s);
|
|
50
|
+
const failure = async (p: Promise<unknown>): Promise<Error> =>
|
|
51
|
+
p.then(
|
|
52
|
+
() => {
|
|
53
|
+
throw new Error('expected a rejection');
|
|
54
|
+
},
|
|
55
|
+
(e: unknown) => e as Error,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
describe('backup', () => {
|
|
59
|
+
it('holds the rows still in the WAL (handle mode, handle left open)', async () => {
|
|
60
|
+
expect(db.open).toBe(true);
|
|
61
|
+
const dest = join(tmp.dir, 'backup');
|
|
62
|
+
await driver({ db }).backup(dest);
|
|
63
|
+
expect(rows(join(dest, 'app.db'))).toEqual([1]);
|
|
64
|
+
expect(integrity(join(dest, 'app.db'))).toBe('ok');
|
|
65
|
+
expect(db.open).toBe(true);
|
|
66
|
+
insert(db, 2); // still usable
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('holds the rows still in the WAL (path only, own connection)', async () => {
|
|
70
|
+
const dest = join(tmp.dir, 'backup');
|
|
71
|
+
await driver().backup(dest);
|
|
72
|
+
expect(rows(join(dest, 'app.db'))).toEqual([1]);
|
|
73
|
+
expect(db.open).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('is nothing to back up when the file does not exist', async () => {
|
|
77
|
+
const dest = join(tmp.dir, 'backup');
|
|
78
|
+
await new SqliteDriver(join(tmp.dir, 'nope.db')).backup(dest);
|
|
79
|
+
expect(existsSync(join(dest, 'nope.db'))).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('replaces a stale target and tmp file', async () => {
|
|
83
|
+
const dest = join(tmp.dir, 'backup');
|
|
84
|
+
mkdirSync(dest);
|
|
85
|
+
writeFileSync(join(dest, 'app.db'), 'stale');
|
|
86
|
+
writeFileSync(join(dest, 'app.db.tmp'), 'stale');
|
|
87
|
+
await driver().backup(dest);
|
|
88
|
+
expect(rows(join(dest, 'app.db'))).toEqual([1]);
|
|
89
|
+
expect(existsSync(join(dest, 'app.db.tmp'))).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("binds the target as a parameter: quotes and spaces in a path are fine", async () => {
|
|
93
|
+
const dest = join(tmp.dir, "it's a back?up #1 %");
|
|
94
|
+
await driver().backup(dest);
|
|
95
|
+
expect(rows(join(dest, 'app.db'))).toEqual([1]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('fails clearly inside a caller transaction, handle left open and untouched', async () => {
|
|
99
|
+
db.exec('BEGIN');
|
|
100
|
+
insert(db, 9);
|
|
101
|
+
const err = await failure(driver({ db }).backup(join(tmp.dir, 'backup')));
|
|
102
|
+
expect(err.message).toMatch(/^backup db: .*open transaction.*VACUUM INTO/);
|
|
103
|
+
expect(db.open).toBe(true);
|
|
104
|
+
expect(db.inTransaction).toBe(true);
|
|
105
|
+
db.exec('ROLLBACK');
|
|
106
|
+
expect(existsSync(join(tmp.dir, 'backup', 'app.db'))).toBe(false);
|
|
107
|
+
expect(existsSync(join(tmp.dir, 'backup', 'app.db.tmp'))).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('restore', () => {
|
|
112
|
+
it('path only: swaps the file and drops stale sidecars', async () => {
|
|
113
|
+
const dest = join(tmp.dir, 'backup');
|
|
114
|
+
await driver().backup(dest);
|
|
115
|
+
insert(db, 2);
|
|
116
|
+
db.close();
|
|
117
|
+
for (const p of sidecars(dbPath)) writeFileSync(p, 'stale');
|
|
118
|
+
|
|
119
|
+
await driver().restore(dest);
|
|
120
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
121
|
+
expect(integrity(dbPath)).toBe('ok');
|
|
122
|
+
for (const p of sidecars(dbPath)) expect(existsSync(p), p).toBe(false);
|
|
123
|
+
expect(existsSync(dest + '/app.db')).toBe(true); // the backup stays
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('path only: keeps the database file mode and leaves no temp file', async () => {
|
|
127
|
+
const dest = join(tmp.dir, 'backup');
|
|
128
|
+
await driver().backup(dest);
|
|
129
|
+
db.close();
|
|
130
|
+
execFileSync('chmod', ['640', dbPath]);
|
|
131
|
+
await driver().restore(dest);
|
|
132
|
+
expect(execFileSync('stat', process.platform === 'darwin' ? ['-f', '%Lp', dbPath] : ['-c', '%a', dbPath]).toString().trim()).toBe('640');
|
|
133
|
+
expect(execFileSync('ls', [tmp.dir]).toString().split('\n').filter((n) => n.includes('.restore-'))).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('path only: a missing backup file fails before touching the database', async () => {
|
|
137
|
+
db.close();
|
|
138
|
+
const err = await failure(driver().restore(join(tmp.dir, 'empty')));
|
|
139
|
+
expect(err.message).toMatch(/^open backup: /);
|
|
140
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('caller handle: unsupported, nothing touched, handle left open', async () => {
|
|
144
|
+
const dest = join(tmp.dir, 'backup');
|
|
145
|
+
await driver({ db }).backup(dest);
|
|
146
|
+
insert(db, 2);
|
|
147
|
+
const err = await failure(driver({ db }).restore(dest));
|
|
148
|
+
expect(err).toBeInstanceOf(BackupUnsupportedError);
|
|
149
|
+
expect(isBackupUnsupported(err)).toBe(true);
|
|
150
|
+
expect(db.open).toBe(true);
|
|
151
|
+
expect(rows(dbPath)).toEqual([1, 2]);
|
|
152
|
+
expect(rows(join(dest, 'app.db'))).toEqual([1]);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe('lock', () => {
|
|
157
|
+
it('fits SchemaDriver, lock included', () => {
|
|
158
|
+
const d: SchemaDriver = driver();
|
|
159
|
+
expect(typeof d.lock).toBe('function');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('lives in .lock.db beside the version file and is free after unlock', async () => {
|
|
163
|
+
const d = driver();
|
|
164
|
+
const unlock = await d.lock();
|
|
165
|
+
expect(existsSync(join(data, 'appdb', '.lock.db'))).toBe(true);
|
|
166
|
+
await unlock();
|
|
167
|
+
await unlock(); // idempotent
|
|
168
|
+
await (await d.lock(AbortSignal.timeout(2000)))();
|
|
169
|
+
expect(existsSync(join(data, 'appdb', '.lock.db'))).toBe(true); // never deleted
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('creates the version directory', async () => {
|
|
173
|
+
const d = new SqliteDriver(dbPath, { name: 'fresh', dataDir: data });
|
|
174
|
+
await (await d.lock())();
|
|
175
|
+
expect(existsSync(join(data, 'fresh', '.lock.db'))).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('a second lock waits for the first without blocking the event loop', async () => {
|
|
179
|
+
const first = await driver().lock();
|
|
180
|
+
let ticks = 0;
|
|
181
|
+
const timer = setInterval(() => ticks++, 10);
|
|
182
|
+
let got = false;
|
|
183
|
+
const second = driver().lock().then((u) => {
|
|
184
|
+
got = true;
|
|
185
|
+
return u;
|
|
186
|
+
});
|
|
187
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
188
|
+
expect(got).toBe(false);
|
|
189
|
+
expect(ticks).toBeGreaterThanOrEqual(10); // the loop kept running while it waited
|
|
190
|
+
await first();
|
|
191
|
+
const unlock = await second;
|
|
192
|
+
clearInterval(timer);
|
|
193
|
+
expect(got).toBe(true);
|
|
194
|
+
await unlock();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('a cancelled wait rejects with the abort error and holds nothing', async () => {
|
|
198
|
+
const first = await driver().lock();
|
|
199
|
+
const ac = new AbortController();
|
|
200
|
+
setTimeout(() => ac.abort(), 100);
|
|
201
|
+
const err = await failure(driver().lock(ac.signal));
|
|
202
|
+
expect(isCanceled(err)).toBe(true);
|
|
203
|
+
expect(err.message).toBe('context canceled');
|
|
204
|
+
await first();
|
|
205
|
+
// The cancelled attempt left no lock behind.
|
|
206
|
+
await (await driver().lock(AbortSignal.timeout(2000)))();
|
|
207
|
+
|
|
208
|
+
const done = new AbortController();
|
|
209
|
+
done.abort();
|
|
210
|
+
expect(isCanceled(await failure(driver().lock(done.signal)))).toBe(true);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe('two processes', () => {
|
|
215
|
+
it('racing one file apply each migration once', async () => {
|
|
216
|
+
const dir = join(tmp.dir, 'race');
|
|
217
|
+
mkdirSync(dir);
|
|
218
|
+
const racePath = join(dir, 'app.db');
|
|
219
|
+
const setup = openWAL(racePath);
|
|
220
|
+
setup.exec('CREATE TABLE applied (version TEXT, pid INTEGER)');
|
|
221
|
+
setup.close();
|
|
222
|
+
|
|
223
|
+
const bundle = join(tmp.dir, 'race-helper.cjs');
|
|
224
|
+
buildSync({
|
|
225
|
+
entryPoints: [join(ROOT, 'test/upgrade/race-helper.ts')],
|
|
226
|
+
bundle: true,
|
|
227
|
+
platform: 'node',
|
|
228
|
+
format: 'cjs',
|
|
229
|
+
external: ['better-sqlite3'],
|
|
230
|
+
outfile: bundle,
|
|
231
|
+
logLevel: 'silent',
|
|
232
|
+
});
|
|
233
|
+
const env = { ...process.env, RACE_DIR: dir, NODE_PATH: join(ROOT, 'node_modules') };
|
|
234
|
+
const run = (): Promise<{ code: number | null; out: string }> =>
|
|
235
|
+
new Promise((resolve) => {
|
|
236
|
+
const p = spawn(process.execPath, [bundle], { env, cwd: ROOT });
|
|
237
|
+
let out = '';
|
|
238
|
+
p.stdout.on('data', (b: Buffer) => (out += b));
|
|
239
|
+
p.stderr.on('data', (b: Buffer) => (out += b));
|
|
240
|
+
p.on('close', (code) => resolve({ code, out }));
|
|
241
|
+
});
|
|
242
|
+
const results = await Promise.all([run(), run()]);
|
|
243
|
+
for (const r of results) expect(r, r.out).toMatchObject({ code: 0 });
|
|
244
|
+
|
|
245
|
+
const check = new Database(racePath, { readonly: true });
|
|
246
|
+
const got: Record<string, number> = {};
|
|
247
|
+
for (const r of check.prepare('SELECT version FROM applied').all() as { version: string }[]) {
|
|
248
|
+
got[r.version] = (got[r.version] ?? 0) + 1;
|
|
249
|
+
}
|
|
250
|
+
check.close();
|
|
251
|
+
expect(got).toEqual({ '1.1.0': 1, '1.2.0': 1 });
|
|
252
|
+
expect(await readVersionFile(join(dir, 'data'), 'appdb')).toBe('1.2.0');
|
|
253
|
+
}, 30_000);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// Collects the method of every reverted event.
|
|
257
|
+
function reverts(): EventPublisher & { methods: string[] } {
|
|
258
|
+
const methods: string[] = [];
|
|
259
|
+
return {
|
|
260
|
+
methods,
|
|
261
|
+
publish(e) {
|
|
262
|
+
if (e.topic.endsWith('.reverted')) methods.push((e.payload as { method: string }).method);
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function addColumn(path: string, col: string): void {
|
|
267
|
+
const c = new Database(path);
|
|
268
|
+
try {
|
|
269
|
+
c.exec(`ALTER TABLE t ADD COLUMN ${col} TEXT`);
|
|
270
|
+
} finally {
|
|
271
|
+
c.close();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function columns(path: string): string[] {
|
|
275
|
+
const c = new Database(path, { readonly: true, fileMustExist: true });
|
|
276
|
+
try {
|
|
277
|
+
return (c.prepare("SELECT name FROM pragma_table_info('t') ORDER BY cid").all() as { name: string }[]).map((r) => r.name);
|
|
278
|
+
} finally {
|
|
279
|
+
c.close();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Backup directories only: `pre-<v>.version` sidecars sit beside them.
|
|
283
|
+
const backupDirs = (): string[] =>
|
|
284
|
+
readdirSync(join(data, 'backups', 'appdb'), { withFileTypes: true })
|
|
285
|
+
.filter((e) => e.isDirectory())
|
|
286
|
+
.map((e) => e.name)
|
|
287
|
+
.sort();
|
|
288
|
+
|
|
289
|
+
// End to end: 1.1.0 inserts row 2; 1.2.0 fails (after `before12`).
|
|
290
|
+
// With `finalVerify`, 1.2.0 applies (inserting row 3) and the final verify
|
|
291
|
+
// fails instead. `downs` collects the versions whose down ran.
|
|
292
|
+
function migrator(
|
|
293
|
+
withDown: boolean,
|
|
294
|
+
d: SqliteDriver,
|
|
295
|
+
before12?: () => void,
|
|
296
|
+
manual = false,
|
|
297
|
+
finalVerify = false,
|
|
298
|
+
publisher?: EventPublisher,
|
|
299
|
+
downs: string[] = [],
|
|
300
|
+
): Migrator {
|
|
301
|
+
const reg = new Registry();
|
|
302
|
+
reg.register({
|
|
303
|
+
version: '1.1.0',
|
|
304
|
+
schema: 'appdb',
|
|
305
|
+
up: () => void db.prepare('INSERT INTO t (x) VALUES (2)').run(),
|
|
306
|
+
down: withDown
|
|
307
|
+
? () => {
|
|
308
|
+
downs.push('1.1.0');
|
|
309
|
+
db.prepare('DELETE FROM t WHERE x = 2').run();
|
|
310
|
+
}
|
|
311
|
+
: undefined,
|
|
312
|
+
});
|
|
313
|
+
reg.register({
|
|
314
|
+
version: '1.2.0',
|
|
315
|
+
schema: 'appdb',
|
|
316
|
+
up: () => {
|
|
317
|
+
before12?.();
|
|
318
|
+
if (finalVerify) return void db.prepare('INSERT INTO t (x) VALUES (3)').run();
|
|
319
|
+
throw new Error('simulated failure');
|
|
320
|
+
},
|
|
321
|
+
down: withDown
|
|
322
|
+
? () => {
|
|
323
|
+
downs.push('1.2.0');
|
|
324
|
+
db.prepare('DELETE FROM t WHERE x = 3').run();
|
|
325
|
+
}
|
|
326
|
+
: undefined,
|
|
327
|
+
});
|
|
328
|
+
const m = new Migrator('tool', '1.2.0', {
|
|
329
|
+
registry: reg,
|
|
330
|
+
dataDir: data,
|
|
331
|
+
manualRollback: manual,
|
|
332
|
+
warn: () => {},
|
|
333
|
+
publisher,
|
|
334
|
+
verify: finalVerify
|
|
335
|
+
? () => {
|
|
336
|
+
throw new Error('simulated failure');
|
|
337
|
+
}
|
|
338
|
+
: undefined,
|
|
339
|
+
});
|
|
340
|
+
m.addDriver(d);
|
|
341
|
+
return m;
|
|
342
|
+
}
|
|
343
|
+
const unlocked = async (d: SqliteDriver): Promise<void> => {
|
|
344
|
+
await (await d.lock(AbortSignal.timeout(2000)))();
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
describe('through the Migrator', () => {
|
|
348
|
+
it('path only: a failed run restores the file; the lock is free afterwards', async () => {
|
|
349
|
+
const d = driver();
|
|
350
|
+
const err = await failure(migrator(false, d, () => db.close()).run());
|
|
351
|
+
expect(err.message).toContain('simulated failure');
|
|
352
|
+
expect(isNotRestored(err)).toBe(false);
|
|
353
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
354
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.0.0');
|
|
355
|
+
expect(existsSync(join(data, 'appdb', '.lock.db'))).toBe(true);
|
|
356
|
+
await unlocked(d);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('caller handle, no down: ends not restored; backup stays; handle open', async () => {
|
|
360
|
+
const d = driver({ db });
|
|
361
|
+
const err = await failure(migrator(false, d).run());
|
|
362
|
+
expect(isNotRestored(err)).toBe(true);
|
|
363
|
+
expect(isBackupUnsupported(err)).toBe(true);
|
|
364
|
+
expect(err.message).toContain('simulated failure');
|
|
365
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.1.0');
|
|
366
|
+
expect(rows(dbPath)).toEqual([1, 2]);
|
|
367
|
+
expect(rows(join(data, 'backups', 'appdb', 'pre-1.1.0', 'app.db'))).toEqual([1]);
|
|
368
|
+
expect(db.open).toBe(true);
|
|
369
|
+
await unlocked(d);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it('caller handle, with down: a failed final verify rolls back by down', async () => {
|
|
373
|
+
const m = migrator(true, driver({ db }), undefined, false, true);
|
|
374
|
+
const err = await failure(m.run());
|
|
375
|
+
expect(err.message).toContain('final verify 1.2.0 failed: simulated failure');
|
|
376
|
+
expect(isNotRestored(err)).toBe(false);
|
|
377
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
378
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.0.0');
|
|
379
|
+
expect(db.open).toBe(true);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it('caller handle, a failed up is not downed: not restored even with every down', async () => {
|
|
383
|
+
const downs: string[] = [];
|
|
384
|
+
const pub = reverts();
|
|
385
|
+
const d = driver({ db });
|
|
386
|
+
const m = migrator(true, d, undefined, false, false, pub, downs);
|
|
387
|
+
const err = await failure(m.run());
|
|
388
|
+
expect(isNotRestored(err)).toBe(true);
|
|
389
|
+
expect(err.message).toContain('up 1.2.0 failed: simulated failure');
|
|
390
|
+
expect(downs).toEqual([]);
|
|
391
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.1.0');
|
|
392
|
+
expect(rows(dbPath)).toEqual([1, 2]);
|
|
393
|
+
expect(pub.methods).toEqual([]);
|
|
394
|
+
expect(db.open).toBe(true);
|
|
395
|
+
await unlocked(d);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('path only: a first up that fails after a partial change is restored', async () => {
|
|
399
|
+
const reg = new Registry();
|
|
400
|
+
reg.register({
|
|
401
|
+
version: '1.1.0',
|
|
402
|
+
schema: 'appdb',
|
|
403
|
+
up: () => {
|
|
404
|
+
db.close(); // precondition of a path-only restore: no handle on the file
|
|
405
|
+
addColumn(dbPath, 'note');
|
|
406
|
+
throw new Error('boom');
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
const pub = reverts();
|
|
410
|
+
const d = driver();
|
|
411
|
+
const m = new Migrator('tool', '1.1.0', { registry: reg, dataDir: data, publisher: pub, warn: () => {} });
|
|
412
|
+
m.addDriver(d);
|
|
413
|
+
const err = await failure(m.run());
|
|
414
|
+
expect(err.message).toContain('up 1.1.0 failed: boom');
|
|
415
|
+
expect(isNotRestored(err)).toBe(false);
|
|
416
|
+
expect(columns(dbPath)).not.toContain('note');
|
|
417
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
418
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.0.0');
|
|
419
|
+
expect(backupDirs()).toEqual(['pre-1.1.0']);
|
|
420
|
+
expect(pub.methods).toEqual(['restore']);
|
|
421
|
+
await unlocked(d);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it('path only: a later up that fails restores without calling any down', async () => {
|
|
425
|
+
const downs: string[] = [];
|
|
426
|
+
const reg = new Registry();
|
|
427
|
+
reg.register({
|
|
428
|
+
version: '1.1.0',
|
|
429
|
+
schema: 'appdb',
|
|
430
|
+
up: () => void db.prepare('INSERT INTO t (x) VALUES (2)').run(),
|
|
431
|
+
down: () => void downs.push('1.1.0'),
|
|
432
|
+
});
|
|
433
|
+
reg.register({
|
|
434
|
+
version: '1.2.0',
|
|
435
|
+
schema: 'appdb',
|
|
436
|
+
up: () => {
|
|
437
|
+
db.close();
|
|
438
|
+
addColumn(dbPath, 'note');
|
|
439
|
+
throw new Error('boom');
|
|
440
|
+
},
|
|
441
|
+
down: () => void downs.push('1.2.0'),
|
|
442
|
+
});
|
|
443
|
+
const pub = reverts();
|
|
444
|
+
const m = new Migrator('tool', '1.2.0', { registry: reg, dataDir: data, publisher: pub, warn: () => {} });
|
|
445
|
+
m.addDriver(driver());
|
|
446
|
+
const err = await failure(m.run());
|
|
447
|
+
expect(err.message).toContain('up 1.2.0 failed: boom');
|
|
448
|
+
expect(isNotRestored(err)).toBe(false);
|
|
449
|
+
expect(downs).toEqual([]);
|
|
450
|
+
expect(columns(dbPath)).not.toContain('note');
|
|
451
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
452
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.0.0');
|
|
453
|
+
expect(pub.methods).toEqual(['restore']);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it('manual rollback, path only: restores and drops the sidecars', async () => {
|
|
457
|
+
const d = driver();
|
|
458
|
+
const m = migrator(false, d, undefined, true);
|
|
459
|
+
expect((await failure(m.run())).message).toContain('simulated failure');
|
|
460
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.1.0');
|
|
461
|
+
db.close(); // precondition: no handle on the file
|
|
462
|
+
await m.rollbackLatest();
|
|
463
|
+
expect(rows(dbPath)).toEqual([1]);
|
|
464
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.0.0');
|
|
465
|
+
for (const p of sidecars(dbPath)) expect(existsSync(p), p).toBe(false);
|
|
466
|
+
await unlocked(d);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('manual rollback, caller handle: not restored, version and handle stay', async () => {
|
|
470
|
+
const d = driver({ db });
|
|
471
|
+
const m = migrator(false, d, undefined, true);
|
|
472
|
+
await failure(m.run());
|
|
473
|
+
const err = await failure(m.rollbackLatest());
|
|
474
|
+
expect(isNotRestored(err)).toBe(true);
|
|
475
|
+
expect(isBackupUnsupported(err)).toBe(true);
|
|
476
|
+
expect(await readVersionFile(data, 'appdb')).toBe('1.1.0');
|
|
477
|
+
expect(rows(dbPath)).toEqual([1, 2]);
|
|
478
|
+
expect(db.open).toBe(true);
|
|
479
|
+
await unlocked(d);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it('a successful run in handle mode leaves a backup with the WAL rows', async () => {
|
|
483
|
+
const reg = new Registry();
|
|
484
|
+
reg.register({ version: '1.1.0', schema: 'appdb', up: () => void db.prepare('INSERT INTO t (x) VALUES (2)').run() });
|
|
485
|
+
const m = new Migrator('tool', '1.1.0', { registry: reg, dataDir: data });
|
|
486
|
+
m.addDriver(driver({ db }));
|
|
487
|
+
await m.run();
|
|
488
|
+
expect(rows(join(data, 'backups', 'appdb', 'pre-1.1.0', 'app.db'))).toEqual([1]);
|
|
489
|
+
expect(rows(dbPath)).toEqual([1, 2]);
|
|
490
|
+
expect(readFileSync(join(data, 'appdb', 'version'), 'utf8')).toBe('1.1.0');
|
|
491
|
+
});
|
|
492
|
+
});
|