@vrplatform/kysely 1.3.45-stage.245 → 1.3.45-stage.252
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/build/main/local/index.js +98 -8
- package/build/main/local/index.js.map +1 -1
- package/build/main/test/index.d.ts +25 -2
- package/build/main/test/index.js +36 -5
- package/build/main/test/index.js.map +1 -1
- package/build/module/local/index.js +98 -8
- package/build/module/local/index.js.map +1 -1
- package/build/module/test/index.d.ts +25 -2
- package/build/module/test/index.js +35 -5
- package/build/module/test/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -64,17 +64,68 @@ async function migrateToLatest(kysely) {
|
|
|
64
64
|
throw error;
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
// Every spec file used to replay this identical bootstrap - migrations, the
|
|
68
|
+
// schema surgery, and the control-plane seed copies - against a freshly loaded
|
|
69
|
+
// snapshot, which measured ~345ms per file against ~185ms for the snapshot load
|
|
70
|
+
// alone. None of it depends on the caller, so it runs once and is cached as a
|
|
71
|
+
// prepared snapshot keyed by everything that determines its content.
|
|
72
|
+
const preparedDumpPrefix = '.data.prepared.';
|
|
73
|
+
let preparedDumpPromise;
|
|
74
|
+
async function preparedDumpKey() {
|
|
75
|
+
const [snapshot, appMigrations, controlPlaneMigrations] = await Promise.all([
|
|
76
|
+
node_fs_1.promises.stat(exports.dumpPath),
|
|
77
|
+
node_fs_1.promises.readdir(path.join(exports.root, migration_1.appMigrationEpoch.folder)),
|
|
78
|
+
node_fs_1.promises.readdir(path.join(exports.root, 'control-plane-migrations')),
|
|
79
|
+
]);
|
|
80
|
+
const hasher = new Bun.CryptoHasher('sha256');
|
|
81
|
+
hasher.update(`${snapshot.size}:${snapshot.mtimeMs}`);
|
|
82
|
+
hasher.update(appMigrations.sort().join(','));
|
|
83
|
+
hasher.update(controlPlaneMigrations.sort().join(','));
|
|
84
|
+
// Source of the bootstrap itself, so editing the surgery rebuilds the
|
|
85
|
+
// snapshot. Read through toString() rather than the file: this module is also
|
|
86
|
+
// published as CommonJS, where import.meta is a compile error.
|
|
87
|
+
for (const step of [
|
|
88
|
+
buildPreparedDump,
|
|
89
|
+
migrateToLatest,
|
|
90
|
+
digest_1.ensureDigestFunction,
|
|
91
|
+
digest_1.ensureGenRandomBytesFunction,
|
|
92
|
+
]) {
|
|
93
|
+
hasher.update(step.toString());
|
|
94
|
+
}
|
|
95
|
+
return hasher.digest('hex').slice(0, 16);
|
|
96
|
+
}
|
|
97
|
+
async function resolvePreparedDump() {
|
|
98
|
+
const key = await preparedDumpKey();
|
|
99
|
+
const target = path.join(exports.root, `${preparedDumpPrefix}${key}.tar`);
|
|
100
|
+
const cached = Bun.file(target);
|
|
101
|
+
if (await cached.exists()) {
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
104
|
+
const content = await buildPreparedDump();
|
|
105
|
+
// Sibling workers build this concurrently on a cold cache; rename is atomic,
|
|
106
|
+
// so a reader never observes a half-written snapshot.
|
|
107
|
+
const staging = `${target}.${process.pid}.tmp`;
|
|
108
|
+
await Bun.write(staging, content);
|
|
109
|
+
await node_fs_1.promises.rename(staging, target);
|
|
110
|
+
// Only finished snapshots: a sibling worker's staging file also carries the
|
|
111
|
+
// prefix, and removing it would break its rename.
|
|
112
|
+
const current = path.basename(target);
|
|
113
|
+
for (const entry of await node_fs_1.promises.readdir(exports.root)) {
|
|
114
|
+
if (entry.startsWith(preparedDumpPrefix) &&
|
|
115
|
+
entry.endsWith('.tar') &&
|
|
116
|
+
entry !== current) {
|
|
117
|
+
await node_fs_1.promises.rm(path.join(exports.root, entry), { force: true });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return content;
|
|
121
|
+
}
|
|
122
|
+
async function buildPreparedDump() {
|
|
71
123
|
const pglite = new pglite_1.PGlite({
|
|
72
|
-
loadDataDir:
|
|
124
|
+
loadDataDir: Bun.file(exports.dumpPath),
|
|
73
125
|
});
|
|
74
126
|
const dialect = new kysely_pglite_dialect_1.PGliteDialect(pglite);
|
|
75
127
|
const kysely = (0, plugins_1.getKysely)(dialect, {
|
|
76
|
-
repositoryName:
|
|
77
|
-
log: args?.log,
|
|
128
|
+
repositoryName: 'localTestingPrepare',
|
|
78
129
|
});
|
|
79
130
|
await (0, digest_1.ensureDigestFunction)(kysely);
|
|
80
131
|
await (0, digest_1.ensureGenRandomBytesFunction)(kysely);
|
|
@@ -84,7 +135,7 @@ async function useLocalKysely(args = {}) {
|
|
|
84
135
|
await (0, kysely_1.sql) `create schema public`.execute(kysely);
|
|
85
136
|
await (0, kysely_1.sql) `create schema core`.execute(kysely);
|
|
86
137
|
const controlPlaneBootstrapKysely = (0, plugins_1.getKysely)(dialect, {
|
|
87
|
-
repositoryName:
|
|
138
|
+
repositoryName: 'localTestingPrepare/control-plane-bootstrap',
|
|
88
139
|
});
|
|
89
140
|
const controlPlaneMigrator = new kysely_1.Migrator({
|
|
90
141
|
db: controlPlaneBootstrapKysely,
|
|
@@ -134,6 +185,26 @@ async function useLocalKysely(args = {}) {
|
|
|
134
185
|
end;
|
|
135
186
|
$$;
|
|
136
187
|
`.execute(controlPlaneBootstrapKysely);
|
|
188
|
+
// The canonical VRPlatform tenant. Production code falls back to this exact id
|
|
189
|
+
// in getCalculatedBillingPartnerId, so specs need it to exist without each of
|
|
190
|
+
// them creating - and mutating - their own copy of a shared singleton.
|
|
191
|
+
await kysely
|
|
192
|
+
.insertInto('public.tenant')
|
|
193
|
+
.values({
|
|
194
|
+
id: '7bfec41f-aa3f-4602-95d2-f7996e4f0e59',
|
|
195
|
+
name: 'VRPlatform',
|
|
196
|
+
type: 'partner',
|
|
197
|
+
status: 'active',
|
|
198
|
+
isActive: true,
|
|
199
|
+
isGeneralLedger: false,
|
|
200
|
+
isOnboarding: false,
|
|
201
|
+
partnerId: null,
|
|
202
|
+
billingPartnerId: null,
|
|
203
|
+
defaultCurrency: 'usd',
|
|
204
|
+
statementStartAt: new Date('2020-01-01T00:00:00Z'),
|
|
205
|
+
})
|
|
206
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
207
|
+
.execute();
|
|
137
208
|
await (0, kysely_1.sql) `
|
|
138
209
|
insert into control_plane.app (
|
|
139
210
|
authentication,
|
|
@@ -289,6 +360,25 @@ async function useLocalKysely(args = {}) {
|
|
|
289
360
|
from public.token
|
|
290
361
|
on conflict (id) do nothing;
|
|
291
362
|
`.execute(controlPlaneBootstrapKysely);
|
|
363
|
+
const content = await pglite.dumpDataDir('none');
|
|
364
|
+
await kysely.destroy();
|
|
365
|
+
return content;
|
|
366
|
+
}
|
|
367
|
+
async function useLocalKysely(args = {}) {
|
|
368
|
+
let isDestroyed = false;
|
|
369
|
+
preparedDumpPromise ?? (preparedDumpPromise = resolvePreparedDump());
|
|
370
|
+
const pglite = new pglite_1.PGlite({
|
|
371
|
+
loadDataDir: await preparedDumpPromise,
|
|
372
|
+
});
|
|
373
|
+
// The constructor is lazy. The bootstrap this replaced forced the snapshot to
|
|
374
|
+
// load before returning, so keep that contract: a returned handle is usable,
|
|
375
|
+
// and a bad snapshot fails here instead of inside an unrelated test.
|
|
376
|
+
await pglite.waitReady;
|
|
377
|
+
const dialect = new kysely_pglite_dialect_1.PGliteDialect(pglite);
|
|
378
|
+
const kysely = (0, plugins_1.getKysely)(dialect, {
|
|
379
|
+
repositoryName: args?.repositoryName ?? 'localTesting',
|
|
380
|
+
log: args?.log,
|
|
381
|
+
});
|
|
292
382
|
const controlPlaneKysely = (0, control_plane_1.withControlPlaneSchema)((0, plugins_1.getKysely)(dialect, {
|
|
293
383
|
repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane`,
|
|
294
384
|
log: args.log,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["local/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,wCA+QC;AA7TD,qCAAyC;AAC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AAE;QACjC,cAAc,EAAE,GAAG,IAAI,CAAC,cAAc,IAAI,cAAc,gBAAgB;QACxE,GAAG,EAAE,IAAI,CAAC,GAAG;KACd,CAAC,EACF,eAAe,EACf,eAAe,CAChB,CAAC;IACF,kBAAkB,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC;IAC5C,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,oBAAoB,EAAE;QAClD,KAAK,EAAE,kBAAkB;KAC1B,CAAC,CAAC;IAEH,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpD,MAAM,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE;QAC1B,IAAI,WAAW;YAAE,OAAO;QACxB,WAAW,GAAG,IAAI,CAAC;QACnB,wCAAwC;QACxC,MAAM,eAAe,EAAE,CAAC;QACxB,cAAc;QACd,0EAA0E;QAC1E,IAAI;IACN,CAAC,CAAC;IAEF,iFAAiF;IAEjF,OAAO,MAAqB,CAAC;AAC/B,CAAC","sourcesContent":["import { promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport { PGlite } from '@electric-sql/pglite';\nimport { FileMigrationProvider, type LogConfig, Migrator, sql } from 'kysely';\nimport { PGliteDialect } from 'kysely-pglite-dialect';\nimport { type ControlPlaneDB, withControlPlaneSchema } from '../control-plane';\nimport type { ControlPlaneKysely } from '../index';\nimport { appMigrationEpoch } from '../migration';\nimport { getKysely } from '../plugins';\nimport type { DB } from '../v1.generated';\nimport { ensureDigestFunction, ensureGenRandomBytesFunction } from './digest';\n\nexport const root = path.join(__dirname, '../..');\nexport const dumpPath = path.join(root, '.data.tar');\n\nexport type UseLocalKyselyOptions = {\n repositoryName?: string;\n log?: true | LogConfig;\n};\n\nexport type LocalKysely = import('kysely').Kysely<DB> & {\n controlPlaneKysely: ControlPlaneKysely;\n};\n\nasync function migrateToLatest<Database>(\n kysely: import('kysely').Kysely<Database>\n) {\n const migrator = new Migrator({\n allowUnorderedMigrations: true,\n db: kysely,\n migrationTableSchema: appMigrationEpoch.tableSchema,\n migrationTableName: appMigrationEpoch.tableName,\n migrationLockTableName: appMigrationEpoch.lockTableName,\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, appMigrationEpoch.folder),\n }),\n });\n\n const { error } = await migrator.migrateToLatest();\n if (error) {\n throw error;\n }\n}\n\nexport async function useLocalKysely(\n args: UseLocalKyselyOptions = {}\n): Promise<LocalKysely> {\n let isDestroyed = false;\n // const now = performance.now();\n\n const dump = Bun.file(dumpPath);\n\n const pglite = new PGlite({\n loadDataDir: dump,\n });\n const dialect = new PGliteDialect(pglite);\n const kysely = getKysely<DB>(dialect, {\n repositoryName: args?.repositoryName ?? 'localTesting',\n log: args?.log,\n });\n await ensureDigestFunction(kysely);\n await ensureGenRandomBytesFunction(kysely);\n await migrateToLatest(kysely);\n\n await sql`alter schema public rename to regional_public`.execute(kysely);\n await sql`alter schema core rename to regional_core`.execute(kysely);\n await sql`create schema public`.execute(kysely);\n await sql`create schema core`.execute(kysely);\n\n const controlPlaneBootstrapKysely = getKysely<ControlPlaneDB>(dialect, {\n repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane-bootstrap`,\n });\n const controlPlaneMigrator = new Migrator({\n db: controlPlaneBootstrapKysely,\n migrationTableSchema: 'public',\n migrationTableName: 'control_plane_kysely_migration',\n migrationLockTableName: 'control_plane_kysely_migration_lock',\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, 'control-plane-migrations'),\n }),\n });\n const { error: controlPlaneMigrationError } =\n await controlPlaneMigrator.migrateToLatest();\n if (controlPlaneMigrationError) {\n throw controlPlaneMigrationError;\n }\n\n await sql`alter table core.flow set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter table core.issue_message_overwrite set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`drop schema core`.execute(controlPlaneBootstrapKysely);\n await sql`alter schema public rename to control_plane`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_public rename to public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_core rename to core`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`\n do $$\n declare\n definition text;\n begin\n select pg_get_functiondef(\n 'control_plane.normalize_tenant_slug(text)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.build_unique_tenant_slug(text,uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_operation_state(uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_destructive_job_operation()'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n end;\n $$;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.app (\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n )\n select\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n from public.app\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.currency (name)\n select name\n from public.currency\n on conflict (name) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel (\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n )\n select\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n from public.booking_channel\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel_icon_candidate (\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n )\n select\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n from public.booking_channel_icon_candidate\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.tenant (\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n )\n select\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n from public.tenant\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.token (\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n )\n select\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n from public.token\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n\n const controlPlaneKysely = withControlPlaneSchema(\n getKysely<ControlPlaneDB>(dialect, {\n repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane`,\n log: args.log,\n }),\n 'control_plane',\n 'control_plane'\n );\n controlPlaneKysely.destroy = async () => {};\n Object.defineProperty(kysely, 'controlPlaneKysely', {\n value: controlPlaneKysely,\n });\n\n const originalDestroy = kysely.destroy.bind(kysely);\n kysely.destroy = async () => {\n if (isDestroyed) return;\n isDestroyed = true;\n //const destroyTime = performance.now();\n await originalDestroy();\n //console.log(\n // `Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`\n //);\n };\n\n // console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);\n\n return kysely as LocalKysely;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["local/index.ts"],"names":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M,CAAC,cAAc,CAAC,MAAM,EAAE,oBAAoB,EAAE;QAClD,KAAK,EAAE,kBAAkB;KAC1B,CAAC,CAAC;IAEH,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpD,MAAM,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE;QAC1B,IAAI,WAAW;YAAE,OAAO;QACxB,WAAW,GAAG,IAAI,CAAC;QACnB,wCAAwC;QACxC,MAAM,eAAe,EAAE,CAAC;QACxB,cAAc;QACd,0EAA0E;QAC1E,IAAI;IACN,CAAC,CAAC;IAEF,iFAAiF;IAEjF,OAAO,MAAqB,CAAC;AAC/B,CAAC","sourcesContent":["import { promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport { PGlite } from '@electric-sql/pglite';\nimport { FileMigrationProvider, type LogConfig, Migrator, sql } from 'kysely';\nimport { PGliteDialect } from 'kysely-pglite-dialect';\nimport { type ControlPlaneDB, withControlPlaneSchema } from '../control-plane';\nimport type { ControlPlaneKysely } from '../index';\nimport { appMigrationEpoch } from '../migration';\nimport { getKysely } from '../plugins';\nimport type { DB } from '../v1.generated';\nimport { ensureDigestFunction, ensureGenRandomBytesFunction } from './digest';\n\nexport const root = path.join(__dirname, '../..');\nexport const dumpPath = path.join(root, '.data.tar');\n\nexport type UseLocalKyselyOptions = {\n repositoryName?: string;\n log?: true | LogConfig;\n};\n\nexport type LocalKysely = import('kysely').Kysely<DB> & {\n controlPlaneKysely: ControlPlaneKysely;\n};\n\nasync function migrateToLatest<Database>(\n kysely: import('kysely').Kysely<Database>\n) {\n const migrator = new Migrator({\n allowUnorderedMigrations: true,\n db: kysely,\n migrationTableSchema: appMigrationEpoch.tableSchema,\n migrationTableName: appMigrationEpoch.tableName,\n migrationLockTableName: appMigrationEpoch.lockTableName,\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, appMigrationEpoch.folder),\n }),\n });\n\n const { error } = await migrator.migrateToLatest();\n if (error) {\n throw error;\n }\n}\n\n// Every spec file used to replay this identical bootstrap - migrations, the\n// schema surgery, and the control-plane seed copies - against a freshly loaded\n// snapshot, which measured ~345ms per file against ~185ms for the snapshot load\n// alone. None of it depends on the caller, so it runs once and is cached as a\n// prepared snapshot keyed by everything that determines its content.\nconst preparedDumpPrefix = '.data.prepared.';\n\nlet preparedDumpPromise: Promise<Blob> | undefined;\n\nasync function preparedDumpKey(): Promise<string> {\n const [snapshot, appMigrations, controlPlaneMigrations] = await Promise.all([\n fs.stat(dumpPath),\n fs.readdir(path.join(root, appMigrationEpoch.folder)),\n fs.readdir(path.join(root, 'control-plane-migrations')),\n ]);\n\n const hasher = new Bun.CryptoHasher('sha256');\n hasher.update(`${snapshot.size}:${snapshot.mtimeMs}`);\n hasher.update(appMigrations.sort().join(','));\n hasher.update(controlPlaneMigrations.sort().join(','));\n // Source of the bootstrap itself, so editing the surgery rebuilds the\n // snapshot. Read through toString() rather than the file: this module is also\n // published as CommonJS, where import.meta is a compile error.\n for (const step of [\n buildPreparedDump,\n migrateToLatest,\n ensureDigestFunction,\n ensureGenRandomBytesFunction,\n ]) {\n hasher.update(step.toString());\n }\n return hasher.digest('hex').slice(0, 16);\n}\n\nasync function resolvePreparedDump(): Promise<Blob> {\n const key = await preparedDumpKey();\n const target = path.join(root, `${preparedDumpPrefix}${key}.tar`);\n const cached = Bun.file(target);\n if (await cached.exists()) {\n return cached;\n }\n\n const content = await buildPreparedDump();\n // Sibling workers build this concurrently on a cold cache; rename is atomic,\n // so a reader never observes a half-written snapshot.\n const staging = `${target}.${process.pid}.tmp`;\n await Bun.write(staging, content);\n await fs.rename(staging, target);\n\n // Only finished snapshots: a sibling worker's staging file also carries the\n // prefix, and removing it would break its rename.\n const current = path.basename(target);\n for (const entry of await fs.readdir(root)) {\n if (\n entry.startsWith(preparedDumpPrefix) &&\n entry.endsWith('.tar') &&\n entry !== current\n ) {\n await fs.rm(path.join(root, entry), { force: true });\n }\n }\n\n return content;\n}\n\nasync function buildPreparedDump(): Promise<Blob> {\n const pglite = new PGlite({\n loadDataDir: Bun.file(dumpPath),\n });\n const dialect = new PGliteDialect(pglite);\n const kysely = getKysely<DB>(dialect, {\n repositoryName: 'localTestingPrepare',\n });\n await ensureDigestFunction(kysely);\n await ensureGenRandomBytesFunction(kysely);\n await migrateToLatest(kysely);\n\n await sql`alter schema public rename to regional_public`.execute(kysely);\n await sql`alter schema core rename to regional_core`.execute(kysely);\n await sql`create schema public`.execute(kysely);\n await sql`create schema core`.execute(kysely);\n\n const controlPlaneBootstrapKysely = getKysely<ControlPlaneDB>(dialect, {\n repositoryName: 'localTestingPrepare/control-plane-bootstrap',\n });\n const controlPlaneMigrator = new Migrator({\n db: controlPlaneBootstrapKysely,\n migrationTableSchema: 'public',\n migrationTableName: 'control_plane_kysely_migration',\n migrationLockTableName: 'control_plane_kysely_migration_lock',\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, 'control-plane-migrations'),\n }),\n });\n const { error: controlPlaneMigrationError } =\n await controlPlaneMigrator.migrateToLatest();\n if (controlPlaneMigrationError) {\n throw controlPlaneMigrationError;\n }\n\n await sql`alter table core.flow set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter table core.issue_message_overwrite set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`drop schema core`.execute(controlPlaneBootstrapKysely);\n await sql`alter schema public rename to control_plane`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_public rename to public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_core rename to core`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`\n do $$\n declare\n definition text;\n begin\n select pg_get_functiondef(\n 'control_plane.normalize_tenant_slug(text)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.build_unique_tenant_slug(text,uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_operation_state(uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_destructive_job_operation()'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n end;\n $$;\n `.execute(controlPlaneBootstrapKysely);\n // The canonical VRPlatform tenant. Production code falls back to this exact id\n // in getCalculatedBillingPartnerId, so specs need it to exist without each of\n // them creating - and mutating - their own copy of a shared singleton.\n await kysely\n .insertInto('public.tenant')\n .values({\n id: '7bfec41f-aa3f-4602-95d2-f7996e4f0e59',\n name: 'VRPlatform',\n type: 'partner',\n status: 'active',\n isActive: true,\n isGeneralLedger: false,\n isOnboarding: false,\n partnerId: null,\n billingPartnerId: null,\n defaultCurrency: 'usd',\n statementStartAt: new Date('2020-01-01T00:00:00Z'),\n })\n .onConflict((oc) => oc.column('id').doNothing())\n .execute();\n\n await sql`\n insert into control_plane.app (\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n )\n select\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n from public.app\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.currency (name)\n select name\n from public.currency\n on conflict (name) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel (\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n )\n select\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n from public.booking_channel\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel_icon_candidate (\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n )\n select\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n from public.booking_channel_icon_candidate\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.tenant (\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n )\n select\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n from public.tenant\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.token (\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n )\n select\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n from public.token\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n\n const content = await pglite.dumpDataDir('none');\n await kysely.destroy();\n return content;\n}\n\nexport async function useLocalKysely(\n args: UseLocalKyselyOptions = {}\n): Promise<LocalKysely> {\n let isDestroyed = false;\n\n preparedDumpPromise ??= resolvePreparedDump();\n const pglite = new PGlite({\n loadDataDir: await preparedDumpPromise,\n });\n // The constructor is lazy. The bootstrap this replaced forced the snapshot to\n // load before returning, so keep that contract: a returned handle is usable,\n // and a bad snapshot fails here instead of inside an unrelated test.\n await pglite.waitReady;\n const dialect = new PGliteDialect(pglite);\n const kysely = getKysely<DB>(dialect, {\n repositoryName: args?.repositoryName ?? 'localTesting',\n log: args?.log,\n });\n\n const controlPlaneKysely = withControlPlaneSchema(\n getKysely<ControlPlaneDB>(dialect, {\n repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane`,\n log: args.log,\n }),\n 'control_plane',\n 'control_plane'\n );\n controlPlaneKysely.destroy = async () => {};\n Object.defineProperty(kysely, 'controlPlaneKysely', {\n value: controlPlaneKysely,\n });\n\n const originalDestroy = kysely.destroy.bind(kysely);\n kysely.destroy = async () => {\n if (isDestroyed) return;\n isDestroyed = true;\n //const destroyTime = performance.now();\n await originalDestroy();\n //console.log(\n // `Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`\n //);\n };\n\n // console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);\n\n return kysely as LocalKysely;\n}\n"]}
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import { type LocalKysely, type UseLocalKyselyOptions } from '../local';
|
|
2
|
-
|
|
2
|
+
type UseTestKyselyOptions = UseLocalKyselyOptions & {
|
|
3
|
+
/**
|
|
4
|
+
* Force a private database even when the worker shares one. Required by specs
|
|
5
|
+
* that model several regions at once, where two handles must be two distinct
|
|
6
|
+
* physical databases.
|
|
7
|
+
*/
|
|
8
|
+
isolated?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare global {
|
|
11
|
+
var __vrplatformSharedKysely: Promise<LocalKysely> | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Locally each worker process shares one database, because specs isolate by
|
|
15
|
+
* tenant and the faster suite is what makes running it before a push realistic.
|
|
16
|
+
*
|
|
17
|
+
* CI keeps a database per spec file. It retries nothing, so a badly scoped spec
|
|
18
|
+
* would surface there as a red build in an unrelated file rather than as a
|
|
19
|
+
* local failure the author can reproduce.
|
|
20
|
+
*
|
|
21
|
+
* VR_TEST_SHARED_KYSELY overrides either way; `isolated: true` opts a single
|
|
22
|
+
* call out when a spec genuinely needs two physical databases at once.
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveSharedKysely(env: Record<string, string | undefined>): boolean;
|
|
25
|
+
export declare function useTestAutoCleanupKysely(args?: UseTestKyselyOptions): Promise<LocalKysely>;
|
|
3
26
|
type ReturnType = LocalKysely & {
|
|
4
27
|
[Symbol.dispose]: () => Promise<void>;
|
|
5
28
|
};
|
|
6
|
-
export declare function useLocalKyselyResource(args?:
|
|
29
|
+
export declare function useLocalKyselyResource(args?: UseTestKyselyOptions): Promise<ReturnType>;
|
|
7
30
|
export {};
|
package/build/main/test/index.js
CHANGED
|
@@ -1,23 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveSharedKysely = resolveSharedKysely;
|
|
3
4
|
exports.useTestAutoCleanupKysely = useTestAutoCleanupKysely;
|
|
4
5
|
exports.useLocalKyselyResource = useLocalKyselyResource;
|
|
5
6
|
const bun_test_1 = require("bun:test");
|
|
6
7
|
const local_1 = require("../local");
|
|
8
|
+
/**
|
|
9
|
+
* Locally each worker process shares one database, because specs isolate by
|
|
10
|
+
* tenant and the faster suite is what makes running it before a push realistic.
|
|
11
|
+
*
|
|
12
|
+
* CI keeps a database per spec file. It retries nothing, so a badly scoped spec
|
|
13
|
+
* would surface there as a red build in an unrelated file rather than as a
|
|
14
|
+
* local failure the author can reproduce.
|
|
15
|
+
*
|
|
16
|
+
* VR_TEST_SHARED_KYSELY overrides either way; `isolated: true` opts a single
|
|
17
|
+
* call out when a spec genuinely needs two physical databases at once.
|
|
18
|
+
*/
|
|
19
|
+
function resolveSharedKysely(env) {
|
|
20
|
+
if (env.VR_TEST_SHARED_KYSELY)
|
|
21
|
+
return env.VR_TEST_SHARED_KYSELY !== '0';
|
|
22
|
+
return !(env.CI || env.GITHUB_ACTIONS);
|
|
23
|
+
}
|
|
24
|
+
const sharedKysely = resolveSharedKysely(process.env);
|
|
25
|
+
function acquireKysely(args) {
|
|
26
|
+
if (!sharedKysely || args?.isolated)
|
|
27
|
+
return (0, local_1.useLocalKysely)(args);
|
|
28
|
+
globalThis.__vrplatformSharedKysely ?? (globalThis.__vrplatformSharedKysely = (0, local_1.useLocalKysely)(args));
|
|
29
|
+
return globalThis.__vrplatformSharedKysely;
|
|
30
|
+
}
|
|
31
|
+
function ownsKysely(args) {
|
|
32
|
+
return !sharedKysely || Boolean(args?.isolated);
|
|
33
|
+
}
|
|
7
34
|
async function useTestAutoCleanupKysely(args) {
|
|
8
|
-
const kysely = await (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
35
|
+
const kysely = await acquireKysely(args);
|
|
36
|
+
if (ownsKysely(args)) {
|
|
37
|
+
(0, bun_test_1.afterAll)(async () => {
|
|
38
|
+
await kysely.destroy();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
12
41
|
return kysely;
|
|
13
42
|
}
|
|
14
43
|
async function useLocalKyselyResource(args) {
|
|
15
|
-
const kysely = await (
|
|
44
|
+
const kysely = await acquireKysely(args);
|
|
16
45
|
return new Proxy({}, {
|
|
17
46
|
get(_target, prop) {
|
|
18
47
|
// somehow not working if using Symbol.dispose
|
|
19
48
|
if (prop === Symbol.asyncDispose) {
|
|
20
49
|
return async () => {
|
|
50
|
+
if (!ownsKysely(args))
|
|
51
|
+
return;
|
|
21
52
|
await kysely.destroy();
|
|
22
53
|
};
|
|
23
54
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["test/index.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["test/index.ts"],"names":[],"mappings":";;AA+BA,kDAKC;AAcD,4DAUC;AAMD,wDA0BC;AA5FD,uCAAoC;AACpC,oCAIkB;AAelB;;;;;;;;;;GAUG;AACH,SAAgB,mBAAmB,CACjC,GAAuC;IAEvC,IAAI,GAAG,CAAC,qBAAqB;QAAE,OAAO,GAAG,CAAC,qBAAqB,KAAK,GAAG,CAAC;IACxE,OAAO,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,cAAc,CAAC,CAAC;AACzC,CAAC;AAED,MAAM,YAAY,GAAG,mBAAmB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAEtD,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,UAAU,EAAE,CAAC;gBACvC,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACnC,CAAC;YACD,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;KACF,CACY,CAAC;AAClB,CAAC","sourcesContent":["import { afterAll } from 'bun:test';\nimport {\n type LocalKysely,\n type UseLocalKyselyOptions,\n useLocalKysely,\n} from '../local';\n\ntype UseTestKyselyOptions = UseLocalKyselyOptions & {\n /**\n * Force a private database even when the worker shares one. Required by specs\n * that model several regions at once, where two handles must be two distinct\n * physical databases.\n */\n isolated?: boolean;\n};\n\ndeclare global {\n var __vrplatformSharedKysely: Promise<LocalKysely> | undefined;\n}\n\n/**\n * Locally each worker process shares one database, because specs isolate by\n * tenant and the faster suite is what makes running it before a push realistic.\n *\n * CI keeps a database per spec file. It retries nothing, so a badly scoped spec\n * would surface there as a red build in an unrelated file rather than as a\n * local failure the author can reproduce.\n *\n * VR_TEST_SHARED_KYSELY overrides either way; `isolated: true` opts a single\n * call out when a spec genuinely needs two physical databases at once.\n */\nexport function resolveSharedKysely(\n env: Record<string, string | undefined>\n): boolean {\n if (env.VR_TEST_SHARED_KYSELY) return env.VR_TEST_SHARED_KYSELY !== '0';\n return !(env.CI || env.GITHUB_ACTIONS);\n}\n\nconst sharedKysely = resolveSharedKysely(process.env);\n\nfunction acquireKysely(args?: UseTestKyselyOptions): Promise<LocalKysely> {\n if (!sharedKysely || args?.isolated) return useLocalKysely(args);\n globalThis.__vrplatformSharedKysely ??= useLocalKysely(args);\n return globalThis.__vrplatformSharedKysely;\n}\n\nfunction ownsKysely(args?: UseTestKyselyOptions): boolean {\n return !sharedKysely || Boolean(args?.isolated);\n}\n\nexport async function useTestAutoCleanupKysely(args?: UseTestKyselyOptions) {\n const kysely = await acquireKysely(args);\n\n if (ownsKysely(args)) {\n afterAll(async () => {\n await kysely.destroy();\n });\n }\n\n return kysely;\n}\n\ntype ReturnType = LocalKysely & {\n [Symbol.dispose]: () => Promise<void>;\n};\n\nexport async function useLocalKyselyResource(\n args?: UseTestKyselyOptions\n): Promise<ReturnType> {\n const kysely = await acquireKysely(args);\n\n return new Proxy(\n {},\n {\n get(_target, prop) {\n // somehow not working if using Symbol.dispose\n if (prop === Symbol.asyncDispose) {\n return async () => {\n if (!ownsKysely(args)) return;\n await kysely.destroy();\n };\n }\n if (prop === 'kysely') {\n return kysely;\n }\n if (typeof kysely[prop] === 'function') {\n return kysely[prop].bind(kysely);\n }\n return kysely[prop];\n },\n }\n ) as ReturnType;\n}\n"]}
|
|
@@ -27,17 +27,68 @@ async function migrateToLatest(kysely) {
|
|
|
27
27
|
throw error;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
// Every spec file used to replay this identical bootstrap - migrations, the
|
|
31
|
+
// schema surgery, and the control-plane seed copies - against a freshly loaded
|
|
32
|
+
// snapshot, which measured ~345ms per file against ~185ms for the snapshot load
|
|
33
|
+
// alone. None of it depends on the caller, so it runs once and is cached as a
|
|
34
|
+
// prepared snapshot keyed by everything that determines its content.
|
|
35
|
+
const preparedDumpPrefix = '.data.prepared.';
|
|
36
|
+
let preparedDumpPromise;
|
|
37
|
+
async function preparedDumpKey() {
|
|
38
|
+
const [snapshot, appMigrations, controlPlaneMigrations] = await Promise.all([
|
|
39
|
+
fs.stat(dumpPath),
|
|
40
|
+
fs.readdir(path.join(root, appMigrationEpoch.folder)),
|
|
41
|
+
fs.readdir(path.join(root, 'control-plane-migrations')),
|
|
42
|
+
]);
|
|
43
|
+
const hasher = new Bun.CryptoHasher('sha256');
|
|
44
|
+
hasher.update(`${snapshot.size}:${snapshot.mtimeMs}`);
|
|
45
|
+
hasher.update(appMigrations.sort().join(','));
|
|
46
|
+
hasher.update(controlPlaneMigrations.sort().join(','));
|
|
47
|
+
// Source of the bootstrap itself, so editing the surgery rebuilds the
|
|
48
|
+
// snapshot. Read through toString() rather than the file: this module is also
|
|
49
|
+
// published as CommonJS, where import.meta is a compile error.
|
|
50
|
+
for (const step of [
|
|
51
|
+
buildPreparedDump,
|
|
52
|
+
migrateToLatest,
|
|
53
|
+
ensureDigestFunction,
|
|
54
|
+
ensureGenRandomBytesFunction,
|
|
55
|
+
]) {
|
|
56
|
+
hasher.update(step.toString());
|
|
57
|
+
}
|
|
58
|
+
return hasher.digest('hex').slice(0, 16);
|
|
59
|
+
}
|
|
60
|
+
async function resolvePreparedDump() {
|
|
61
|
+
const key = await preparedDumpKey();
|
|
62
|
+
const target = path.join(root, `${preparedDumpPrefix}${key}.tar`);
|
|
63
|
+
const cached = Bun.file(target);
|
|
64
|
+
if (await cached.exists()) {
|
|
65
|
+
return cached;
|
|
66
|
+
}
|
|
67
|
+
const content = await buildPreparedDump();
|
|
68
|
+
// Sibling workers build this concurrently on a cold cache; rename is atomic,
|
|
69
|
+
// so a reader never observes a half-written snapshot.
|
|
70
|
+
const staging = `${target}.${process.pid}.tmp`;
|
|
71
|
+
await Bun.write(staging, content);
|
|
72
|
+
await fs.rename(staging, target);
|
|
73
|
+
// Only finished snapshots: a sibling worker's staging file also carries the
|
|
74
|
+
// prefix, and removing it would break its rename.
|
|
75
|
+
const current = path.basename(target);
|
|
76
|
+
for (const entry of await fs.readdir(root)) {
|
|
77
|
+
if (entry.startsWith(preparedDumpPrefix) &&
|
|
78
|
+
entry.endsWith('.tar') &&
|
|
79
|
+
entry !== current) {
|
|
80
|
+
await fs.rm(path.join(root, entry), { force: true });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return content;
|
|
84
|
+
}
|
|
85
|
+
async function buildPreparedDump() {
|
|
34
86
|
const pglite = new PGlite({
|
|
35
|
-
loadDataDir:
|
|
87
|
+
loadDataDir: Bun.file(dumpPath),
|
|
36
88
|
});
|
|
37
89
|
const dialect = new PGliteDialect(pglite);
|
|
38
90
|
const kysely = getKysely(dialect, {
|
|
39
|
-
repositoryName:
|
|
40
|
-
log: args?.log,
|
|
91
|
+
repositoryName: 'localTestingPrepare',
|
|
41
92
|
});
|
|
42
93
|
await ensureDigestFunction(kysely);
|
|
43
94
|
await ensureGenRandomBytesFunction(kysely);
|
|
@@ -47,7 +98,7 @@ export async function useLocalKysely(args = {}) {
|
|
|
47
98
|
await sql `create schema public`.execute(kysely);
|
|
48
99
|
await sql `create schema core`.execute(kysely);
|
|
49
100
|
const controlPlaneBootstrapKysely = getKysely(dialect, {
|
|
50
|
-
repositoryName:
|
|
101
|
+
repositoryName: 'localTestingPrepare/control-plane-bootstrap',
|
|
51
102
|
});
|
|
52
103
|
const controlPlaneMigrator = new Migrator({
|
|
53
104
|
db: controlPlaneBootstrapKysely,
|
|
@@ -97,6 +148,26 @@ export async function useLocalKysely(args = {}) {
|
|
|
97
148
|
end;
|
|
98
149
|
$$;
|
|
99
150
|
`.execute(controlPlaneBootstrapKysely);
|
|
151
|
+
// The canonical VRPlatform tenant. Production code falls back to this exact id
|
|
152
|
+
// in getCalculatedBillingPartnerId, so specs need it to exist without each of
|
|
153
|
+
// them creating - and mutating - their own copy of a shared singleton.
|
|
154
|
+
await kysely
|
|
155
|
+
.insertInto('public.tenant')
|
|
156
|
+
.values({
|
|
157
|
+
id: '7bfec41f-aa3f-4602-95d2-f7996e4f0e59',
|
|
158
|
+
name: 'VRPlatform',
|
|
159
|
+
type: 'partner',
|
|
160
|
+
status: 'active',
|
|
161
|
+
isActive: true,
|
|
162
|
+
isGeneralLedger: false,
|
|
163
|
+
isOnboarding: false,
|
|
164
|
+
partnerId: null,
|
|
165
|
+
billingPartnerId: null,
|
|
166
|
+
defaultCurrency: 'usd',
|
|
167
|
+
statementStartAt: new Date('2020-01-01T00:00:00Z'),
|
|
168
|
+
})
|
|
169
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
170
|
+
.execute();
|
|
100
171
|
await sql `
|
|
101
172
|
insert into control_plane.app (
|
|
102
173
|
authentication,
|
|
@@ -252,6 +323,25 @@ export async function useLocalKysely(args = {}) {
|
|
|
252
323
|
from public.token
|
|
253
324
|
on conflict (id) do nothing;
|
|
254
325
|
`.execute(controlPlaneBootstrapKysely);
|
|
326
|
+
const content = await pglite.dumpDataDir('none');
|
|
327
|
+
await kysely.destroy();
|
|
328
|
+
return content;
|
|
329
|
+
}
|
|
330
|
+
export async function useLocalKysely(args = {}) {
|
|
331
|
+
let isDestroyed = false;
|
|
332
|
+
preparedDumpPromise ??= resolvePreparedDump();
|
|
333
|
+
const pglite = new PGlite({
|
|
334
|
+
loadDataDir: await preparedDumpPromise,
|
|
335
|
+
});
|
|
336
|
+
// The constructor is lazy. The bootstrap this replaced forced the snapshot to
|
|
337
|
+
// load before returning, so keep that contract: a returned handle is usable,
|
|
338
|
+
// and a bad snapshot fails here instead of inside an unrelated test.
|
|
339
|
+
await pglite.waitReady;
|
|
340
|
+
const dialect = new PGliteDialect(pglite);
|
|
341
|
+
const kysely = getKysely(dialect, {
|
|
342
|
+
repositoryName: args?.repositoryName ?? 'localTesting',
|
|
343
|
+
log: args?.log,
|
|
344
|
+
});
|
|
255
345
|
const controlPlaneKysely = withControlPlaneSchema(getKysely(dialect, {
|
|
256
346
|
repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane`,
|
|
257
347
|
log: args.log,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["local/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAkB,QAAQ,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAuB,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAE/E,OAAO,EAAE,iBA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,EACF,eAAe,EACf,eAAe,CAChB,CAAC;IACF,kBAAkB,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC;IAC5C,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,oBAAoB,EAAE;QAClD,KAAK,EAAE,kBAAkB;KAC1B,CAAC,CAAC;IAEH,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpD,MAAM,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE;QAC1B,IAAI,WAAW;YAAE,OAAO;QACxB,WAAW,GAAG,IAAI,CAAC;QACnB,wCAAwC;QACxC,MAAM,eAAe,EAAE,CAAC;QACxB,cAAc;QACd,0EAA0E;QAC1E,IAAI;IACN,CAAC,CAAC;IAEF,iFAAiF;IAEjF,OAAO,MAAqB,CAAC;AAC/B,CAAC","sourcesContent":["import { promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport { PGlite } from '@electric-sql/pglite';\nimport { FileMigrationProvider, type LogConfig, Migrator, sql } from 'kysely';\nimport { PGliteDialect } from 'kysely-pglite-dialect';\nimport { type ControlPlaneDB, withControlPlaneSchema } from '../control-plane';\nimport type { ControlPlaneKysely } from '../index';\nimport { appMigrationEpoch } from '../migration';\nimport { getKysely } from '../plugins';\nimport type { DB } from '../v1.generated';\nimport { ensureDigestFunction, ensureGenRandomBytesFunction } from './digest';\n\nexport const root = path.join(__dirname, '../..');\nexport const dumpPath = path.join(root, '.data.tar');\n\nexport type UseLocalKyselyOptions = {\n repositoryName?: string;\n log?: true | LogConfig;\n};\n\nexport type LocalKysely = import('kysely').Kysely<DB> & {\n controlPlaneKysely: ControlPlaneKysely;\n};\n\nasync function migrateToLatest<Database>(\n kysely: import('kysely').Kysely<Database>\n) {\n const migrator = new Migrator({\n allowUnorderedMigrations: true,\n db: kysely,\n migrationTableSchema: appMigrationEpoch.tableSchema,\n migrationTableName: appMigrationEpoch.tableName,\n migrationLockTableName: appMigrationEpoch.lockTableName,\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, appMigrationEpoch.folder),\n }),\n });\n\n const { error } = await migrator.migrateToLatest();\n if (error) {\n throw error;\n }\n}\n\nexport async function useLocalKysely(\n args: UseLocalKyselyOptions = {}\n): Promise<LocalKysely> {\n let isDestroyed = false;\n // const now = performance.now();\n\n const dump = Bun.file(dumpPath);\n\n const pglite = new PGlite({\n loadDataDir: dump,\n });\n const dialect = new PGliteDialect(pglite);\n const kysely = getKysely<DB>(dialect, {\n repositoryName: args?.repositoryName ?? 'localTesting',\n log: args?.log,\n });\n await ensureDigestFunction(kysely);\n await ensureGenRandomBytesFunction(kysely);\n await migrateToLatest(kysely);\n\n await sql`alter schema public rename to regional_public`.execute(kysely);\n await sql`alter schema core rename to regional_core`.execute(kysely);\n await sql`create schema public`.execute(kysely);\n await sql`create schema core`.execute(kysely);\n\n const controlPlaneBootstrapKysely = getKysely<ControlPlaneDB>(dialect, {\n repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane-bootstrap`,\n });\n const controlPlaneMigrator = new Migrator({\n db: controlPlaneBootstrapKysely,\n migrationTableSchema: 'public',\n migrationTableName: 'control_plane_kysely_migration',\n migrationLockTableName: 'control_plane_kysely_migration_lock',\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, 'control-plane-migrations'),\n }),\n });\n const { error: controlPlaneMigrationError } =\n await controlPlaneMigrator.migrateToLatest();\n if (controlPlaneMigrationError) {\n throw controlPlaneMigrationError;\n }\n\n await sql`alter table core.flow set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter table core.issue_message_overwrite set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`drop schema core`.execute(controlPlaneBootstrapKysely);\n await sql`alter schema public rename to control_plane`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_public rename to public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_core rename to core`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`\n do $$\n declare\n definition text;\n begin\n select pg_get_functiondef(\n 'control_plane.normalize_tenant_slug(text)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.build_unique_tenant_slug(text,uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_operation_state(uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_destructive_job_operation()'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n end;\n $$;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.app (\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n )\n select\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n from public.app\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.currency (name)\n select name\n from public.currency\n on conflict (name) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel (\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n )\n select\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n from public.booking_channel\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel_icon_candidate (\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n )\n select\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n from public.booking_channel_icon_candidate\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.tenant (\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n )\n select\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n from public.tenant\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.token (\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n )\n select\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n from public.token\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n\n const controlPlaneKysely = withControlPlaneSchema(\n getKysely<ControlPlaneDB>(dialect, {\n repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane`,\n log: args.log,\n }),\n 'control_plane',\n 'control_plane'\n );\n controlPlaneKysely.destroy = async () => {};\n Object.defineProperty(kysely, 'controlPlaneKysely', {\n value: controlPlaneKysely,\n });\n\n const originalDestroy = kysely.destroy.bind(kysely);\n kysely.destroy = async () => {\n if (isDestroyed) return;\n isDestroyed = true;\n //const destroyTime = performance.now();\n await originalDestroy();\n //console.log(\n // `Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`\n //);\n };\n\n // console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);\n\n return kysely as LocalKysely;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["local/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;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AAE,CAAC,CAAC;IAC5C,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,oBAAoB,EAAE;QAClD,KAAK,EAAE,kBAAkB;KAC1B,CAAC,CAAC;IAEH,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpD,MAAM,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE;QAC1B,IAAI,WAAW;YAAE,OAAO;QACxB,WAAW,GAAG,IAAI,CAAC;QACnB,wCAAwC;QACxC,MAAM,eAAe,EAAE,CAAC;QACxB,cAAc;QACd,0EAA0E;QAC1E,IAAI;IACN,CAAC,CAAC;IAEF,iFAAiF;IAEjF,OAAO,MAAqB,CAAC;AAC/B,CAAC","sourcesContent":["import { promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport { PGlite } from '@electric-sql/pglite';\nimport { FileMigrationProvider, type LogConfig, Migrator, sql } from 'kysely';\nimport { PGliteDialect } from 'kysely-pglite-dialect';\nimport { type ControlPlaneDB, withControlPlaneSchema } from '../control-plane';\nimport type { ControlPlaneKysely } from '../index';\nimport { appMigrationEpoch } from '../migration';\nimport { getKysely } from '../plugins';\nimport type { DB } from '../v1.generated';\nimport { ensureDigestFunction, ensureGenRandomBytesFunction } from './digest';\n\nexport const root = path.join(__dirname, '../..');\nexport const dumpPath = path.join(root, '.data.tar');\n\nexport type UseLocalKyselyOptions = {\n repositoryName?: string;\n log?: true | LogConfig;\n};\n\nexport type LocalKysely = import('kysely').Kysely<DB> & {\n controlPlaneKysely: ControlPlaneKysely;\n};\n\nasync function migrateToLatest<Database>(\n kysely: import('kysely').Kysely<Database>\n) {\n const migrator = new Migrator({\n allowUnorderedMigrations: true,\n db: kysely,\n migrationTableSchema: appMigrationEpoch.tableSchema,\n migrationTableName: appMigrationEpoch.tableName,\n migrationLockTableName: appMigrationEpoch.lockTableName,\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, appMigrationEpoch.folder),\n }),\n });\n\n const { error } = await migrator.migrateToLatest();\n if (error) {\n throw error;\n }\n}\n\n// Every spec file used to replay this identical bootstrap - migrations, the\n// schema surgery, and the control-plane seed copies - against a freshly loaded\n// snapshot, which measured ~345ms per file against ~185ms for the snapshot load\n// alone. None of it depends on the caller, so it runs once and is cached as a\n// prepared snapshot keyed by everything that determines its content.\nconst preparedDumpPrefix = '.data.prepared.';\n\nlet preparedDumpPromise: Promise<Blob> | undefined;\n\nasync function preparedDumpKey(): Promise<string> {\n const [snapshot, appMigrations, controlPlaneMigrations] = await Promise.all([\n fs.stat(dumpPath),\n fs.readdir(path.join(root, appMigrationEpoch.folder)),\n fs.readdir(path.join(root, 'control-plane-migrations')),\n ]);\n\n const hasher = new Bun.CryptoHasher('sha256');\n hasher.update(`${snapshot.size}:${snapshot.mtimeMs}`);\n hasher.update(appMigrations.sort().join(','));\n hasher.update(controlPlaneMigrations.sort().join(','));\n // Source of the bootstrap itself, so editing the surgery rebuilds the\n // snapshot. Read through toString() rather than the file: this module is also\n // published as CommonJS, where import.meta is a compile error.\n for (const step of [\n buildPreparedDump,\n migrateToLatest,\n ensureDigestFunction,\n ensureGenRandomBytesFunction,\n ]) {\n hasher.update(step.toString());\n }\n return hasher.digest('hex').slice(0, 16);\n}\n\nasync function resolvePreparedDump(): Promise<Blob> {\n const key = await preparedDumpKey();\n const target = path.join(root, `${preparedDumpPrefix}${key}.tar`);\n const cached = Bun.file(target);\n if (await cached.exists()) {\n return cached;\n }\n\n const content = await buildPreparedDump();\n // Sibling workers build this concurrently on a cold cache; rename is atomic,\n // so a reader never observes a half-written snapshot.\n const staging = `${target}.${process.pid}.tmp`;\n await Bun.write(staging, content);\n await fs.rename(staging, target);\n\n // Only finished snapshots: a sibling worker's staging file also carries the\n // prefix, and removing it would break its rename.\n const current = path.basename(target);\n for (const entry of await fs.readdir(root)) {\n if (\n entry.startsWith(preparedDumpPrefix) &&\n entry.endsWith('.tar') &&\n entry !== current\n ) {\n await fs.rm(path.join(root, entry), { force: true });\n }\n }\n\n return content;\n}\n\nasync function buildPreparedDump(): Promise<Blob> {\n const pglite = new PGlite({\n loadDataDir: Bun.file(dumpPath),\n });\n const dialect = new PGliteDialect(pglite);\n const kysely = getKysely<DB>(dialect, {\n repositoryName: 'localTestingPrepare',\n });\n await ensureDigestFunction(kysely);\n await ensureGenRandomBytesFunction(kysely);\n await migrateToLatest(kysely);\n\n await sql`alter schema public rename to regional_public`.execute(kysely);\n await sql`alter schema core rename to regional_core`.execute(kysely);\n await sql`create schema public`.execute(kysely);\n await sql`create schema core`.execute(kysely);\n\n const controlPlaneBootstrapKysely = getKysely<ControlPlaneDB>(dialect, {\n repositoryName: 'localTestingPrepare/control-plane-bootstrap',\n });\n const controlPlaneMigrator = new Migrator({\n db: controlPlaneBootstrapKysely,\n migrationTableSchema: 'public',\n migrationTableName: 'control_plane_kysely_migration',\n migrationLockTableName: 'control_plane_kysely_migration_lock',\n provider: new FileMigrationProvider({\n fs,\n path,\n migrationFolder: path.join(root, 'control-plane-migrations'),\n }),\n });\n const { error: controlPlaneMigrationError } =\n await controlPlaneMigrator.migrateToLatest();\n if (controlPlaneMigrationError) {\n throw controlPlaneMigrationError;\n }\n\n await sql`alter table core.flow set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter table core.issue_message_overwrite set schema public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`drop schema core`.execute(controlPlaneBootstrapKysely);\n await sql`alter schema public rename to control_plane`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_public rename to public`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`alter schema regional_core rename to core`.execute(\n controlPlaneBootstrapKysely\n );\n await sql`\n do $$\n declare\n definition text;\n begin\n select pg_get_functiondef(\n 'control_plane.normalize_tenant_slug(text)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.build_unique_tenant_slug(text,uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_operation_state(uuid)'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n\n select pg_get_functiondef(\n 'control_plane.refresh_destructive_job_operation()'::regprocedure\n ) into definition;\n execute replace(definition, 'public.', 'control_plane.');\n end;\n $$;\n `.execute(controlPlaneBootstrapKysely);\n // The canonical VRPlatform tenant. Production code falls back to this exact id\n // in getCalculatedBillingPartnerId, so specs need it to exist without each of\n // them creating - and mutating - their own copy of a shared singleton.\n await kysely\n .insertInto('public.tenant')\n .values({\n id: '7bfec41f-aa3f-4602-95d2-f7996e4f0e59',\n name: 'VRPlatform',\n type: 'partner',\n status: 'active',\n isActive: true,\n isGeneralLedger: false,\n isOnboarding: false,\n partnerId: null,\n billingPartnerId: null,\n defaultCurrency: 'usd',\n statementStartAt: new Date('2020-01-01T00:00:00Z'),\n })\n .onConflict((oc) => oc.column('id').doNothing())\n .execute();\n\n await sql`\n insert into control_plane.app (\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n )\n select\n authentication,\n category,\n color,\n created_at,\n icon,\n icon_round,\n id,\n info,\n name,\n type,\n version\n from public.app\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.currency (name)\n select name\n from public.currency\n on conflict (name) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel (\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n )\n select\n channel_ref,\n color,\n created_at,\n icon,\n icon_provider,\n id,\n last_generate_run,\n logo,\n logo_provider,\n selected_booking_channel_icon_candidate_id,\n selected_booking_channel_logo_candidate_id,\n unique_ref,\n updated_at\n from public.booking_channel\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.booking_channel_icon_candidate (\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n )\n select\n booking_channel_id,\n cloudflare_image_id,\n color,\n comment,\n created_at,\n external_icon,\n height,\n id,\n provider,\n source,\n updated_at,\n width\n from public.booking_channel_icon_candidate\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.tenant (\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n )\n select\n billing_partner_id,\n billing_plan,\n created_at,\n data_region,\n id,\n name,\n partner_id,\n slug,\n status,\n storage_realm,\n type,\n unique_ref,\n updated_at\n from public.tenant\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n await sql`\n insert into control_plane.token (\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n )\n select\n created_at,\n expires_at,\n id,\n nano_id,\n payload,\n tenant_id,\n type,\n user_id\n from public.token\n on conflict (id) do nothing;\n `.execute(controlPlaneBootstrapKysely);\n\n const content = await pglite.dumpDataDir('none');\n await kysely.destroy();\n return content;\n}\n\nexport async function useLocalKysely(\n args: UseLocalKyselyOptions = {}\n): Promise<LocalKysely> {\n let isDestroyed = false;\n\n preparedDumpPromise ??= resolvePreparedDump();\n const pglite = new PGlite({\n loadDataDir: await preparedDumpPromise,\n });\n // The constructor is lazy. The bootstrap this replaced forced the snapshot to\n // load before returning, so keep that contract: a returned handle is usable,\n // and a bad snapshot fails here instead of inside an unrelated test.\n await pglite.waitReady;\n const dialect = new PGliteDialect(pglite);\n const kysely = getKysely<DB>(dialect, {\n repositoryName: args?.repositoryName ?? 'localTesting',\n log: args?.log,\n });\n\n const controlPlaneKysely = withControlPlaneSchema(\n getKysely<ControlPlaneDB>(dialect, {\n repositoryName: `${args.repositoryName ?? 'localTesting'}/control-plane`,\n log: args.log,\n }),\n 'control_plane',\n 'control_plane'\n );\n controlPlaneKysely.destroy = async () => {};\n Object.defineProperty(kysely, 'controlPlaneKysely', {\n value: controlPlaneKysely,\n });\n\n const originalDestroy = kysely.destroy.bind(kysely);\n kysely.destroy = async () => {\n if (isDestroyed) return;\n isDestroyed = true;\n //const destroyTime = performance.now();\n await originalDestroy();\n //console.log(\n // `Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`\n //);\n };\n\n // console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);\n\n return kysely as LocalKysely;\n}\n"]}
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import { type LocalKysely, type UseLocalKyselyOptions } from '../local';
|
|
2
|
-
|
|
2
|
+
type UseTestKyselyOptions = UseLocalKyselyOptions & {
|
|
3
|
+
/**
|
|
4
|
+
* Force a private database even when the worker shares one. Required by specs
|
|
5
|
+
* that model several regions at once, where two handles must be two distinct
|
|
6
|
+
* physical databases.
|
|
7
|
+
*/
|
|
8
|
+
isolated?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare global {
|
|
11
|
+
var __vrplatformSharedKysely: Promise<LocalKysely> | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Locally each worker process shares one database, because specs isolate by
|
|
15
|
+
* tenant and the faster suite is what makes running it before a push realistic.
|
|
16
|
+
*
|
|
17
|
+
* CI keeps a database per spec file. It retries nothing, so a badly scoped spec
|
|
18
|
+
* would surface there as a red build in an unrelated file rather than as a
|
|
19
|
+
* local failure the author can reproduce.
|
|
20
|
+
*
|
|
21
|
+
* VR_TEST_SHARED_KYSELY overrides either way; `isolated: true` opts a single
|
|
22
|
+
* call out when a spec genuinely needs two physical databases at once.
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveSharedKysely(env: Record<string, string | undefined>): boolean;
|
|
25
|
+
export declare function useTestAutoCleanupKysely(args?: UseTestKyselyOptions): Promise<LocalKysely>;
|
|
3
26
|
type ReturnType = LocalKysely & {
|
|
4
27
|
[Symbol.dispose]: () => Promise<void>;
|
|
5
28
|
};
|
|
6
|
-
export declare function useLocalKyselyResource(args?:
|
|
29
|
+
export declare function useLocalKyselyResource(args?: UseTestKyselyOptions): Promise<ReturnType>;
|
|
7
30
|
export {};
|
|
@@ -1,19 +1,49 @@
|
|
|
1
1
|
import { afterAll } from 'bun:test';
|
|
2
2
|
import { useLocalKysely, } from '../local';
|
|
3
|
+
/**
|
|
4
|
+
* Locally each worker process shares one database, because specs isolate by
|
|
5
|
+
* tenant and the faster suite is what makes running it before a push realistic.
|
|
6
|
+
*
|
|
7
|
+
* CI keeps a database per spec file. It retries nothing, so a badly scoped spec
|
|
8
|
+
* would surface there as a red build in an unrelated file rather than as a
|
|
9
|
+
* local failure the author can reproduce.
|
|
10
|
+
*
|
|
11
|
+
* VR_TEST_SHARED_KYSELY overrides either way; `isolated: true` opts a single
|
|
12
|
+
* call out when a spec genuinely needs two physical databases at once.
|
|
13
|
+
*/
|
|
14
|
+
export function resolveSharedKysely(env) {
|
|
15
|
+
if (env.VR_TEST_SHARED_KYSELY)
|
|
16
|
+
return env.VR_TEST_SHARED_KYSELY !== '0';
|
|
17
|
+
return !(env.CI || env.GITHUB_ACTIONS);
|
|
18
|
+
}
|
|
19
|
+
const sharedKysely = resolveSharedKysely(process.env);
|
|
20
|
+
function acquireKysely(args) {
|
|
21
|
+
if (!sharedKysely || args?.isolated)
|
|
22
|
+
return useLocalKysely(args);
|
|
23
|
+
globalThis.__vrplatformSharedKysely ??= useLocalKysely(args);
|
|
24
|
+
return globalThis.__vrplatformSharedKysely;
|
|
25
|
+
}
|
|
26
|
+
function ownsKysely(args) {
|
|
27
|
+
return !sharedKysely || Boolean(args?.isolated);
|
|
28
|
+
}
|
|
3
29
|
export async function useTestAutoCleanupKysely(args) {
|
|
4
|
-
const kysely = await
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
30
|
+
const kysely = await acquireKysely(args);
|
|
31
|
+
if (ownsKysely(args)) {
|
|
32
|
+
afterAll(async () => {
|
|
33
|
+
await kysely.destroy();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
8
36
|
return kysely;
|
|
9
37
|
}
|
|
10
38
|
export async function useLocalKyselyResource(args) {
|
|
11
|
-
const kysely = await
|
|
39
|
+
const kysely = await acquireKysely(args);
|
|
12
40
|
return new Proxy({}, {
|
|
13
41
|
get(_target, prop) {
|
|
14
42
|
// somehow not working if using Symbol.dispose
|
|
15
43
|
if (prop === Symbol.asyncDispose) {
|
|
16
44
|
return async () => {
|
|
45
|
+
if (!ownsKysely(args))
|
|
46
|
+
return;
|
|
17
47
|
await kysely.destroy();
|
|
18
48
|
};
|
|
19
49
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["test/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAGL,cAAc,GACf,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["test/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAGL,cAAc,GACf,MAAM,UAAU,CAAC;AAelB;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB,CACjC,GAAuC;IAEvC,IAAI,GAAG,CAAC,qBAAqB;QAAE,OAAO,GAAG,CAAC,qBAAqB,KAAK,GAAG,CAAC;IACxE,OAAO,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,cAAc,CAAC,CAAC;AACzC,CAAC;AAED,MAAM,YAAY,GAAG,mBAAmB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAEtD,SAAS,aAAa,CAAC,IAA2B;IAChD,IAAI,CAAC,YAAY,IAAI,IAAI,EAAE,QAAQ;QAAE,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC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AC;QACtB,CAAC;KACF,CACY,CAAC;AAClB,CAAC","sourcesContent":["import { afterAll } from 'bun:test';\nimport {\n type LocalKysely,\n type UseLocalKyselyOptions,\n useLocalKysely,\n} from '../local';\n\ntype UseTestKyselyOptions = UseLocalKyselyOptions & {\n /**\n * Force a private database even when the worker shares one. Required by specs\n * that model several regions at once, where two handles must be two distinct\n * physical databases.\n */\n isolated?: boolean;\n};\n\ndeclare global {\n var __vrplatformSharedKysely: Promise<LocalKysely> | undefined;\n}\n\n/**\n * Locally each worker process shares one database, because specs isolate by\n * tenant and the faster suite is what makes running it before a push realistic.\n *\n * CI keeps a database per spec file. It retries nothing, so a badly scoped spec\n * would surface there as a red build in an unrelated file rather than as a\n * local failure the author can reproduce.\n *\n * VR_TEST_SHARED_KYSELY overrides either way; `isolated: true` opts a single\n * call out when a spec genuinely needs two physical databases at once.\n */\nexport function resolveSharedKysely(\n env: Record<string, string | undefined>\n): boolean {\n if (env.VR_TEST_SHARED_KYSELY) return env.VR_TEST_SHARED_KYSELY !== '0';\n return !(env.CI || env.GITHUB_ACTIONS);\n}\n\nconst sharedKysely = resolveSharedKysely(process.env);\n\nfunction acquireKysely(args?: UseTestKyselyOptions): Promise<LocalKysely> {\n if (!sharedKysely || args?.isolated) return useLocalKysely(args);\n globalThis.__vrplatformSharedKysely ??= useLocalKysely(args);\n return globalThis.__vrplatformSharedKysely;\n}\n\nfunction ownsKysely(args?: UseTestKyselyOptions): boolean {\n return !sharedKysely || Boolean(args?.isolated);\n}\n\nexport async function useTestAutoCleanupKysely(args?: UseTestKyselyOptions) {\n const kysely = await acquireKysely(args);\n\n if (ownsKysely(args)) {\n afterAll(async () => {\n await kysely.destroy();\n });\n }\n\n return kysely;\n}\n\ntype ReturnType = LocalKysely & {\n [Symbol.dispose]: () => Promise<void>;\n};\n\nexport async function useLocalKyselyResource(\n args?: UseTestKyselyOptions\n): Promise<ReturnType> {\n const kysely = await acquireKysely(args);\n\n return new Proxy(\n {},\n {\n get(_target, prop) {\n // somehow not working if using Symbol.dispose\n if (prop === Symbol.asyncDispose) {\n return async () => {\n if (!ownsKysely(args)) return;\n await kysely.destroy();\n };\n }\n if (prop === 'kysely') {\n return kysely;\n }\n if (typeof kysely[prop] === 'function') {\n return kysely[prop].bind(kysely);\n }\n return kysely[prop];\n },\n }\n ) as ReturnType;\n}\n"]}
|