@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,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A {@link SchemaDriver} for PostgreSQL over a caller-provided
|
|
3
|
+
* node-postgres style handle, ported from Go
|
|
4
|
+
* `core/upgrade/driver/postgres`. Kit has no runtime Postgres dependency:
|
|
5
|
+
* the caller opens the `pg` Pool (or any object with the same `query`
|
|
6
|
+
* shape) and passes it in, plus a function that returns a dedicated client
|
|
7
|
+
* for the lock.
|
|
8
|
+
*
|
|
9
|
+
* SQL, the table-name rule and the lock key text are the Go driver's,
|
|
10
|
+
* character for character, so a Go and a TypeScript Migrator on one table
|
|
11
|
+
* and schema name exclude each other.
|
|
12
|
+
*
|
|
13
|
+
* Import from `@hop-top/kit/upgrade/drivers/postgres`. The class is a
|
|
14
|
+
* structural `SchemaDriver` (checked in `postgres.test.ts`); it imports no
|
|
15
|
+
* type from `../driver` so the published declarations stay one chunk per
|
|
16
|
+
* entry.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { BackupUnsupportedError, abortError, errorText } from '../errors';
|
|
20
|
+
|
|
21
|
+
/** What a query resolves with: the rows, as objects keyed by column. */
|
|
22
|
+
export interface PgResult {
|
|
23
|
+
rows: Array<Record<string, any>>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The node-postgres `query(text, params)` shape (a `Pool` or `Client`). */
|
|
27
|
+
export interface PgQueryable {
|
|
28
|
+
query(text: string, params?: any[]): Promise<PgResult>;
|
|
29
|
+
/** A node-postgres `Pool` carries its options; `max: 1` is refused at lock time. */
|
|
30
|
+
readonly options?: { max?: number };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A dedicated connection for the lock: a client from `pool.connect()` (has
|
|
35
|
+
* `release`) or a standalone connected `Client` (has `end`). The driver
|
|
36
|
+
* ends the physical connection through whichever exists.
|
|
37
|
+
*/
|
|
38
|
+
export interface PgLockClient {
|
|
39
|
+
query(text: string, params?: any[]): Promise<PgResult>;
|
|
40
|
+
/** Pooled client: `release()` returns it, `release(true)` destroys it. */
|
|
41
|
+
release?(destroy?: boolean | Error): void;
|
|
42
|
+
/** Standalone client: closes the connection. */
|
|
43
|
+
end?(): void | Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface PostgresDriverOptions {
|
|
47
|
+
/** Runs the version queries. Never closed by the driver. */
|
|
48
|
+
db: PgQueryable;
|
|
49
|
+
/**
|
|
50
|
+
* Returns a dedicated connected client, e.g. `() => pool.connect()`.
|
|
51
|
+
* It carries the lock for the whole run, so it must not be `db` itself
|
|
52
|
+
* and a pool needs at least two connections.
|
|
53
|
+
*/
|
|
54
|
+
connect: () => Promise<PgLockClient>;
|
|
55
|
+
/** Version table, `^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`, interpolated unquoted. */
|
|
56
|
+
table: string;
|
|
57
|
+
/** Schema name, matched against `Migration.schema`. Default `postgres`. */
|
|
58
|
+
name?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const TABLE_RE = /^[a-zA-Z_][a-zA-Z0-9_]{0,63}$/;
|
|
62
|
+
|
|
63
|
+
/** SQL shared verbatim by every port (the server hashes the key text). */
|
|
64
|
+
export const SQL_LOCK = 'SELECT pg_advisory_lock(hashtextextended($1, 0))';
|
|
65
|
+
export const SQL_UNLOCK = 'SELECT pg_advisory_unlock(hashtextextended($1, 0))';
|
|
66
|
+
export const SQL_XACT_LOCK = 'SELECT pg_advisory_xact_lock(hashtextextended($1, 0))';
|
|
67
|
+
/**
|
|
68
|
+
* The non-blocking form of {@link SQL_LOCK}: same key, same session-level
|
|
69
|
+
* lock, so it excludes (and is excluded by) a blocking `pg_advisory_lock`
|
|
70
|
+
* on the key. Polled so an aborted wait holds nothing.
|
|
71
|
+
*/
|
|
72
|
+
export const SQL_TRY_LOCK = 'SELECT pg_try_advisory_lock(hashtextextended($1, 0))';
|
|
73
|
+
|
|
74
|
+
/** Text hashed into the advisory lock key for a run. */
|
|
75
|
+
export const lockKey = (table: string, name: string): string => `kit-upgrade:${table}:${name}`;
|
|
76
|
+
/** Text hashed into the key that serialises table creation. */
|
|
77
|
+
export const ddlKey = (table: string): string => `kit-upgrade:${table}:ddl`;
|
|
78
|
+
|
|
79
|
+
/** The DDL for the version table. */
|
|
80
|
+
export const createTableSql = (table: string): string =>
|
|
81
|
+
`CREATE TABLE IF NOT EXISTS ${table} (schema_name TEXT PRIMARY KEY, version TEXT NOT NULL DEFAULT '')`;
|
|
82
|
+
export const versionSql = (table: string): string => `SELECT version FROM ${table} WHERE schema_name = $1`;
|
|
83
|
+
export const upsertSql = (table: string): string =>
|
|
84
|
+
`INSERT INTO ${table} (schema_name, version) VALUES ($1, $2) ON CONFLICT (schema_name) DO UPDATE SET version = EXCLUDED.version`;
|
|
85
|
+
|
|
86
|
+
/** How often a blocked lock polls the server. */
|
|
87
|
+
const POLL_MS = 50;
|
|
88
|
+
|
|
89
|
+
/** Throws `invalid table name: "<name>"` unless `name` is a legal table name. */
|
|
90
|
+
export function validateTable(name: string): void {
|
|
91
|
+
if (typeof name !== 'string' || !TABLE_RE.test(name)) {
|
|
92
|
+
throw new Error(`invalid table name: ${JSON.stringify(name)}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const firstValue = (r: PgResult): unknown => {
|
|
97
|
+
const row = r.rows[0];
|
|
98
|
+
return row === undefined ? undefined : Object.values(row)[0];
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** Ends the physical connection: the session, and every lock it holds, goes. */
|
|
102
|
+
async function discard(c: PgLockClient): Promise<void> {
|
|
103
|
+
try {
|
|
104
|
+
if (typeof c.release === 'function') c.release(true);
|
|
105
|
+
else if (typeof c.end === 'function') await c.end();
|
|
106
|
+
} catch {
|
|
107
|
+
// Nothing more to do for a connection that cannot even close.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Hands a healthy client back (pool) or closes it (standalone). */
|
|
112
|
+
async function finish(c: PgLockClient): Promise<void> {
|
|
113
|
+
if (typeof c.release === 'function') c.release();
|
|
114
|
+
else if (typeof c.end === 'function') await c.end();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Resolves after `ms`, or early when `signal` aborts. */
|
|
118
|
+
function pause(ms: number, signal?: AbortSignal): Promise<void> {
|
|
119
|
+
return new Promise((resolve) => {
|
|
120
|
+
const done = (): void => {
|
|
121
|
+
clearTimeout(t);
|
|
122
|
+
signal?.removeEventListener('abort', done);
|
|
123
|
+
resolve();
|
|
124
|
+
};
|
|
125
|
+
const t = setTimeout(done, ms);
|
|
126
|
+
signal?.addEventListener('abort', done, { once: true });
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Tracks a schema version in a PostgreSQL table and serialises concurrent
|
|
132
|
+
* migrators with a session-level advisory lock. Create it with
|
|
133
|
+
* {@link openPostgresDriver}.
|
|
134
|
+
*/
|
|
135
|
+
export class PostgresDriver {
|
|
136
|
+
readonly storesVersion = true;
|
|
137
|
+
|
|
138
|
+
/** @internal use {@link openPostgresDriver}. */
|
|
139
|
+
constructor(private readonly o: Required<PostgresDriverOptions>) {}
|
|
140
|
+
|
|
141
|
+
name(): string {
|
|
142
|
+
return this.o.name;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The stored version; no row means `""`. */
|
|
146
|
+
async version(): Promise<string> {
|
|
147
|
+
try {
|
|
148
|
+
const r = await this.o.db.query(versionSql(this.o.table), [this.o.name]);
|
|
149
|
+
const v = r.rows[0]?.['version'];
|
|
150
|
+
return typeof v === 'string' ? v : '';
|
|
151
|
+
} catch (e) {
|
|
152
|
+
throw new Error(`postgres upgrade: version: ${errorText(e)}`, { cause: e });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Upserts the version row for this schema. */
|
|
157
|
+
async setVersion(version: string): Promise<void> {
|
|
158
|
+
try {
|
|
159
|
+
await this.o.db.query(upsertSql(this.o.table), [this.o.name, version]);
|
|
160
|
+
} catch (e) {
|
|
161
|
+
throw new Error(`postgres upgrade: set version: ${errorText(e)}`, { cause: e });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Kit does not back up a server database: the run goes on without one. */
|
|
166
|
+
async backup(_dest: string): Promise<void> {
|
|
167
|
+
throw new BackupUnsupportedError('postgres upgrade: backup unsupported');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Nothing is restored: a rollback that needs it ends "not restored". */
|
|
171
|
+
async restore(_src: string): Promise<void> {
|
|
172
|
+
throw new BackupUnsupportedError('postgres upgrade: restore unsupported');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Takes the session-level advisory lock on one dedicated connection and
|
|
177
|
+
* holds that connection until the returned unlock runs. Waits by polling
|
|
178
|
+
* `pg_try_advisory_lock` (about every 50 ms) until acquired or `signal`
|
|
179
|
+
* aborts; an aborted wait rejects with `abortError(signal)` and the
|
|
180
|
+
* connection is destroyed, so nothing is held. Unlock runs
|
|
181
|
+
* `pg_advisory_unlock` on that same connection regardless of `signal`;
|
|
182
|
+
* when it fails or answers `false` the connection is destroyed, which
|
|
183
|
+
* ends the session and the lock.
|
|
184
|
+
*
|
|
185
|
+
* A pool with `options.max === 1` is refused (the lock holds its only
|
|
186
|
+
* connection and the run's queries would wait for ever).
|
|
187
|
+
*/
|
|
188
|
+
async lock(signal?: AbortSignal): Promise<() => Promise<void>> {
|
|
189
|
+
if (this.o.db.options?.max === 1) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
'postgres upgrade: lock needs a pool of at least two connections: ' +
|
|
192
|
+
'the lock holds one for the whole run (a pool with max 1 would deadlock)',
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
if (signal?.aborted) throw abortError(signal);
|
|
196
|
+
let client: PgLockClient;
|
|
197
|
+
try {
|
|
198
|
+
client = await this.o.connect();
|
|
199
|
+
} catch (e) {
|
|
200
|
+
throw new Error(`postgres upgrade: lock: ${errorText(e)}`, { cause: e });
|
|
201
|
+
}
|
|
202
|
+
const key = lockKey(this.o.table, this.o.name);
|
|
203
|
+
try {
|
|
204
|
+
for (;;) {
|
|
205
|
+
if (signal?.aborted) throw abortError(signal);
|
|
206
|
+
const r = await client.query(SQL_TRY_LOCK, [key]);
|
|
207
|
+
if (firstValue(r) === true) break;
|
|
208
|
+
await pause(POLL_MS, signal);
|
|
209
|
+
}
|
|
210
|
+
// The acquiring statement may have been in flight when the abort hit.
|
|
211
|
+
if (signal?.aborted) throw abortError(signal);
|
|
212
|
+
} catch (e) {
|
|
213
|
+
await discard(client);
|
|
214
|
+
if (signal?.aborted) throw abortError(signal);
|
|
215
|
+
throw new Error(`postgres upgrade: lock: ${errorText(e)}`, { cause: e });
|
|
216
|
+
}
|
|
217
|
+
return this.unlocker(client, key);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private unlocker(client: PgLockClient, key: string): () => Promise<void> {
|
|
221
|
+
let result: Promise<void> | undefined;
|
|
222
|
+
return () => (result ??= this.release(client, key));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private async release(client: PgLockClient, key: string): Promise<void> {
|
|
226
|
+
let err: unknown;
|
|
227
|
+
try {
|
|
228
|
+
const r = await client.query(SQL_UNLOCK, [key]);
|
|
229
|
+
if (firstValue(r) !== true) err = new Error('lock was not held by this session');
|
|
230
|
+
} catch (e) {
|
|
231
|
+
err = e;
|
|
232
|
+
}
|
|
233
|
+
if (err !== undefined) {
|
|
234
|
+
await discard(client);
|
|
235
|
+
throw new Error(`postgres upgrade: unlock: ${errorText(err)}`, { cause: err });
|
|
236
|
+
}
|
|
237
|
+
// The statement released the session lock; now the connection can go back.
|
|
238
|
+
await finish(client);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Ensures the version table exists, then returns a driver for it. The
|
|
244
|
+
* table is created in a transaction on a dedicated client that first takes
|
|
245
|
+
* `pg_advisory_xact_lock`: two sessions running `CREATE TABLE IF NOT
|
|
246
|
+
* EXISTS` at once can still fail with a catalog unique violation. The
|
|
247
|
+
* driver never closes `db`.
|
|
248
|
+
*/
|
|
249
|
+
export async function openPostgresDriver(opts: PostgresDriverOptions): Promise<PostgresDriver> {
|
|
250
|
+
validateTable(opts.table);
|
|
251
|
+
if (!opts.db || typeof opts.db.query !== 'function') {
|
|
252
|
+
throw new Error('postgres upgrade: db must have query(text, params)');
|
|
253
|
+
}
|
|
254
|
+
if (typeof opts.connect !== 'function') {
|
|
255
|
+
throw new Error('postgres upgrade: connect must return a dedicated client');
|
|
256
|
+
}
|
|
257
|
+
const o: Required<PostgresDriverOptions> = { ...opts, name: opts.name ?? 'postgres' };
|
|
258
|
+
try {
|
|
259
|
+
await ensureTable(o);
|
|
260
|
+
} catch (e) {
|
|
261
|
+
throw new Error(`postgres upgrade: migrate: ${errorText(e)}`, { cause: e });
|
|
262
|
+
}
|
|
263
|
+
return new PostgresDriver(o);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function ensureTable(o: Required<PostgresDriverOptions>): Promise<void> {
|
|
267
|
+
const client = await o.connect();
|
|
268
|
+
try {
|
|
269
|
+
await client.query('BEGIN');
|
|
270
|
+
await client.query(SQL_XACT_LOCK, [ddlKey(o.table)]);
|
|
271
|
+
await client.query(createTableSql(o.table));
|
|
272
|
+
await client.query('COMMIT');
|
|
273
|
+
} catch (e) {
|
|
274
|
+
try {
|
|
275
|
+
await client.query('ROLLBACK');
|
|
276
|
+
} catch {
|
|
277
|
+
await discard(client);
|
|
278
|
+
throw e;
|
|
279
|
+
}
|
|
280
|
+
await finish(client);
|
|
281
|
+
throw e;
|
|
282
|
+
}
|
|
283
|
+
await finish(client);
|
|
284
|
+
}
|