@jskit-ai/database-runtime-mysql 0.1.258 → 0.1.259

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/database-runtime-mysql",
3
- "version": "0.1.258",
3
+ "version": "0.1.259",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -12,7 +12,7 @@
12
12
  "./shared/dialect": "./src/shared/dialect.js"
13
13
  },
14
14
  "dependencies": {
15
- "@jskit-ai/database-runtime": "0.1.260",
15
+ "@jskit-ai/database-runtime": "0.1.261",
16
16
  "mysql2": "^3.11.2"
17
17
  },
18
18
  "jskit": {
@@ -56,6 +56,6 @@
56
56
  }
57
57
  },
58
58
  "peerDependencies": {
59
- "@jskit-ai/kernel": "0.2.11"
59
+ "@jskit-ai/kernel": "0.2.12"
60
60
  }
61
61
  }
@@ -61,27 +61,38 @@ metadata. Do not combine it with the PostgreSQL pattern's workflow: a MySQL
61
61
  application's workflow has no `postgres` service, `POSTGRES_*` variables, port
62
62
  `5432`, or `pg_isready` option.
63
63
 
64
- `example/tests/browser/database.js` is the application-owned browser preparation
65
- helper. It requires the exact `BROWSER_TEST_DB_NAME`, rejects the ordinary and
66
- disposable `TEST_DB_NAME` databases,
67
- retains the schema and migration ledger, applies pending migrations, then clears
68
- test rows and calls one explicit fixture seed transaction. The seed must restore
69
- any migration-owned baseline rows the app needs. Call it once before starting
70
- the isolated test server, never in `beforeEach`. It closes its connections without
71
- dropping the database. The launcher selects that database only for its own server
72
- and identity process, disables external effects, and proves actual server identity
73
- before the suite writes data. It must not edit the normal environment. Provision
74
- both test databases with exact grants for the application account. A disposable
75
- integration or migration test must never drop the browser database.
76
-
77
- Startup reports whether the schema exists, the number of migrations applied,
78
- and the migration, fixture-reset and seed durations. Use those measurements to
79
- diagnose a slow start before rerunning a browser case.
64
+ Tests get their own databases from
65
+ `@jskit-ai/database-runtime/server/testDatabases`. The `test` script runs
66
+ `jskit test --databases`: each Node test file running at once gets its own
67
+ copy of `TEST_DB_NAME` (`<name>`, `<name>_1`, ...), restored from a stored
68
+ snapshot of the current migrations' schema and the rows they write, in
69
+ `node_modules/.cache/jskit-test-databases/`. The snapshot is replayed once, and
70
+ again only when a migration's name or contents or `package-lock.json` change;
71
+ a file that migrates or drops tables leaves its copy to be restored before the
72
+ next file takes it.
73
+
74
+ `example/tests/e2e/databases.js` is the browser run's `prepare` module for
75
+ `browserTestConfig({ prepare: "tests/e2e/databases.js" })` from
76
+ `@jskit-ai/jskit-catalog/browser-tests`. It gives each Playwright worker's
77
+ server its own copy of `BROWSER_TEST_DB_NAME`, restored from the same kind of
78
+ snapshot and seeded in one transaction by the app's fixture `seed`, which
79
+ restores any migration-owned baseline rows the app needs. Every server serves
80
+ the production build. Both refuse the application's database (`DB_NAME`,
81
+ `DATABASE_URL`'s, the Knex connection's), `PROD_DB_NAME` and the other test
82
+ database and its copies. Provision `TEST_DB_NAME`, `BROWSER_TEST_DB_NAME` and
83
+ their numbered copies with grants for the application account (a `<app>_%`
84
+ grant covers them); a user who may create no copies runs one file or worker at
85
+ a time. Two test runs at once need different database names. Test-only server
86
+ settings that disable external effects (email, payments) go in
87
+ `browserTestConfig({ env })`; a suite that writes data proves the server's
88
+ identity first.
89
+
90
+ Each preparation reports how many databases it made, whether it replayed the
91
+ migrations for a new snapshot, and the restore and seed durations. Use those
92
+ measurements to diagnose a slow start before rerunning a browser case.
80
93
 
81
94
  Keep fixtures small for the selected test scope. Run related cases in one suite
82
- invocation. Use the development launcher for ordinary browser checks; test the
83
- production build separately when needed. Fresh-schema migration proofs are
84
- separate from browser startup.
95
+ invocation. Fresh-schema migration proofs are separate from browser startup.
85
96
 
86
97
  ## Variation points
87
98
 
@@ -99,11 +110,10 @@ it as `seed` to `prepareDatabaseFromApp()`.
99
110
  - Run `npm run db:migrate:status` and the application verification command.
100
111
  - Exercise one transaction and one negative connection case.
101
112
  - When a seed exists, run `db:prepare` twice and require the second run to be safe.
102
- - For browser preparation, run a disposable test between two starts: the second applies no
103
- old migrations, stale fixture data is removed, baseline rows are restored,
104
- foreign-key checks remain enabled, and the normal database is unchanged.
105
- Also prove that a new pending migration is applied. Keep this as a regression
106
- test when adapting the example; reject a harness that recreates the schema.
113
+ - For the test databases, run the suite twice: the second run restores the
114
+ stored snapshot instead of replaying the migrations, stale rows are gone,
115
+ baseline rows are restored, foreign keys are enforced, and the normal
116
+ database is unchanged. A new migration takes a new snapshot.
107
117
  - Confirm `.github/workflows/verify.yml` parses as YAML and declares only the
108
118
  `mariadb` service. GitHub reports an unparseable workflow only as a failed
109
119
  run with "no jobs were run".
@@ -5,9 +5,10 @@
5
5
  "db:prepare": "node ./scripts/prepare-database.js",
6
6
  "db:migrate": "knex --knexfile ./knexfile.js migrate:latest",
7
7
  "db:migrate:rollback": "knex --knexfile ./knexfile.js migrate:rollback",
8
- "db:migrate:status": "knex --knexfile ./knexfile.js migrate:list"
8
+ "db:migrate:status": "knex --knexfile ./knexfile.js migrate:list",
9
+ "test": "jskit test --databases"
9
10
  },
10
11
  "dependencies": {
11
- "@jskit-ai/database-runtime-mysql": "0.1.258"
12
+ "@jskit-ai/database-runtime-mysql": "0.1.259"
12
13
  }
13
14
  }
@@ -0,0 +1,25 @@
1
+ import { prepareTestDatabases } from "@jskit-ai/database-runtime/server/testDatabases";
2
+ import knexConfig from "../../knexfile.js";
3
+
4
+ // The browser run's `prepare` module: `browserTestConfig({ prepare: "tests/e2e/databases.js" })` in
5
+ // playwright.config.mjs runs it once, before the servers start. Every Playwright worker gets its own copy of
6
+ // BROWSER_TEST_DB_NAME (`<name>`, `<name>_1`, ...), restored from the schema snapshot of the current migrations, then
7
+ // seeded; the normal and disposable TEST_DB_NAME databases are refused. It is also given each worker's `baseUrls`,
8
+ // for a server setting that names its own address.
9
+
10
+ /** The fixtures the specs rely on, including any migration-owned baseline rows the app needs. */
11
+ async function seed(transaction) {
12
+ await transaction("fixtures").insert({ id: 1, label: "Browser fixture" });
13
+ }
14
+
15
+ export default async function prepareBrowserDatabases({ workers }) {
16
+ const { databases, summary } = await prepareTestDatabases({
17
+ knexConfig,
18
+ environment: process.env,
19
+ variable: "BROWSER_TEST_DB_NAME",
20
+ count: workers,
21
+ seed
22
+ });
23
+ console.log(`[browser-tests] ${summary}.`);
24
+ return databases.map((database) => ({ DB_NAME: database }));
25
+ }
@@ -0,0 +1,351 @@
1
+ import assert from "node:assert/strict";
2
+ import { execFile } from "node:child_process";
3
+ import { createHash } from "node:crypto";
4
+ import { mkdtemp, mkdir, readFile, rm, stat, symlink, writeFile } from "node:fs/promises";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+ import test from "node:test";
8
+ import { fileURLToPath } from "node:url";
9
+ import { promisify } from "node:util";
10
+ import Fastify from "fastify";
11
+ import knex from "knex";
12
+ import { createCapabilityRuntime, defineProvider } from "@jskit-ai/kernel/shared/capabilities";
13
+ import { DatabaseProvider } from "@jskit-ai/database-runtime/server/providers/DatabaseProvider";
14
+ import { createKnexMigrationConfig } from "@jskit-ai/database-runtime/server/knexMigrationConfig";
15
+ import {
16
+ RUN_SETTINGS_FILE,
17
+ countQueries,
18
+ countRuntimeQueries,
19
+ matchesSnapshot,
20
+ prepareTestDatabases,
21
+ restoreTestDatabase,
22
+ testDatabaseRun
23
+ } from "@jskit-ai/database-runtime/server/testDatabases";
24
+ import { MysqlDatabaseDriverProvider } from "../src/server/providers/MysqlDatabaseDriverProvider.js";
25
+
26
+ const BOOKS = `exports.up = async (knex) => {
27
+ await knex.schema.createTable("books", (table) => { table.increments("id"); table.string("title"); table.date("published_on"); });
28
+ await knex.schema.createTable("reviews", (table) => { table.increments("id"); table.integer("book_id").unsigned().references("books.id"); });
29
+ await knex("books").insert({ id: 1, title: "Written by the migration", published_on: "2020-01-02" });
30
+ };
31
+ exports.down = () => {};
32
+ `;
33
+ const execFileAsync = promisify(execFile);
34
+ const SHELVES = "exports.up = (knex) => knex.schema.createTable(\"shelves\", (table) => table.increments(\"id\"));\nexports.down = () => {};\n";
35
+
36
+ test("each test database is restored from one snapshot, rebuilt only when a migration changes", {
37
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
38
+ }, async (context) => {
39
+ const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-test-databases-mysql-"));
40
+ await mkdir(path.join(appRoot, "migrations", "constraints"), { recursive: true });
41
+ await writeFile(path.join(appRoot, "migrations", "20200101000000_books.cjs"), BOOKS);
42
+ const name = `${process.env.TEST_DB_NAME}_copies`;
43
+ // The application's database is one this test never creates; only `name` and its copies are touched.
44
+ const environment = { ...process.env, DB_NAME: `${process.env.TEST_DB_NAME}_application`, DATABASE_URL: "", TEST_DB_NAME: name };
45
+ const knexConfig = createKnexMigrationConfig({ client: "mysql2", environment, appRoot });
46
+ const server = { ...knexConfig.connection, database: undefined };
47
+ const admin = knex({ client: "mysql2", connection: server });
48
+ const names = [name, `${name}_1`, `${name}_2`];
49
+ const on = (database) => knex({ client: "mysql2", connection: { ...server, database } });
50
+ context.after(async () => {
51
+ for (const database of names) await admin.raw("DROP DATABASE IF EXISTS ??", [database]);
52
+ await admin.destroy();
53
+ await rm(appRoot, { recursive: true, force: true });
54
+ });
55
+ const seed = (transaction) => transaction("books").insert({ id: 2, title: "Seeded" });
56
+
57
+ const first = await prepareTestDatabases({ knexConfig, environment, count: 3, seed, appRoot });
58
+ assert.deepEqual(first.databases, names);
59
+ assert.match(first.summary, /1 migrations replayed once for a new snapshot/u);
60
+ for (const database of names) {
61
+ const db = on(database);
62
+ try {
63
+ assert.deepEqual(await db("books").orderBy("id").select("id", "title"), [
64
+ { id: 1, title: "Written by the migration" },
65
+ { id: 2, title: "Seeded" }
66
+ ], database);
67
+ assert.deepEqual(await db("knex_migrations").pluck("name"), ["20200101000000_books.cjs"]);
68
+ // The foreign key came with the table's definition and is enforced.
69
+ await assert.rejects(db("reviews").insert({ book_id: 99 }), /foreign key constraint/iu);
70
+ } finally {
71
+ await db.destroy();
72
+ }
73
+ }
74
+
75
+ const second = on(names[1]);
76
+ try {
77
+ await second("books").insert({ id: 3, title: "Stale" });
78
+ } finally {
79
+ await second.destroy();
80
+ }
81
+ const again = await prepareTestDatabases({ knexConfig, environment, count: 3, seed, appRoot });
82
+ assert.match(again.summary, /stored snapshot/u);
83
+ const restored = on(names[1]);
84
+ try {
85
+ assert.deepEqual(await restored("books").orderBy("id").pluck("id"), [1, 2]);
86
+ assert.equal(await matchesSnapshot(knexConfig, names[1], again.snapshot), true);
87
+ // A file that migrates down or drops a table leaves its database dirty, until it is restored.
88
+ await restored.schema.dropTable("reviews");
89
+ assert.equal(await matchesSnapshot(knexConfig, names[1], again.snapshot), false);
90
+ await restoreTestDatabase(knexConfig, names[1], again.snapshot);
91
+ assert.equal(await matchesSnapshot(knexConfig, names[1], again.snapshot), true);
92
+ } finally {
93
+ await restored.destroy();
94
+ }
95
+ // A file that drops its whole database leaves the next file a restored one.
96
+ await admin.raw("DROP DATABASE ??", [names[2]]);
97
+ assert.equal(await matchesSnapshot(knexConfig, names[2], again.snapshot), false);
98
+ await restoreTestDatabase(knexConfig, names[2], again.snapshot);
99
+ assert.equal(await matchesSnapshot(knexConfig, names[2], again.snapshot), true);
100
+
101
+ await writeFile(path.join(appRoot, "migrations", "20200102000000_shelves.cjs"), SHELVES);
102
+ const changed = await prepareTestDatabases({ knexConfig, environment, count: 2, appRoot });
103
+ assert.notEqual(changed.snapshot.key, again.snapshot.key);
104
+ assert.match(changed.summary, /2 migrations replayed once for a new snapshot/u);
105
+ assert.deepEqual(changed.snapshot.tables, ["books", "knex_migrations", "knex_migrations_lock", "reviews", "shelves"]);
106
+
107
+ // jskit test --databases: one database per file at once, handed to each file's process by the preload. The
108
+ // environment names the run's directory only; the password is in a file only this user may read.
109
+ const run = await testDatabaseRun({ knexConfig, environment, count: 2, appRoot });
110
+ try {
111
+ assert.equal(run.concurrency, 2);
112
+ assert.match(run.preload, /testDatabaseFile\.js$/u);
113
+ assert.deepEqual(Object.keys(run.env), ["JSKIT_TEST_DATABASES"]);
114
+ const settingsFile = path.join(run.env.JSKIT_TEST_DATABASES, RUN_SETTINGS_FILE);
115
+ assert.equal((await stat(settingsFile)).mode & 0o777, 0o600);
116
+ assert.equal((await stat(run.env.JSKIT_TEST_DATABASES)).mode & 0o077, 0);
117
+ const settings = JSON.parse(await readFile(settingsFile, "utf8"));
118
+ assert.deepEqual(settings.databases, [name, `${name}_1`]);
119
+ assert.deepEqual(Object.keys(settings.knexConfig).sort(), ["client", "connection", "migrations"]);
120
+ if (knexConfig.connection.password) assert.equal(JSON.stringify(run.env).includes(knexConfig.connection.password), false);
121
+ } finally {
122
+ run.cleanup();
123
+ }
124
+
125
+ // The application's own database was never created or touched.
126
+ const [[application]] = await admin.raw("SELECT COUNT(*) AS count FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?",
127
+ [environment.DB_NAME]);
128
+ assert.equal(Number(application.count), 0);
129
+ });
130
+
131
+ const SHELF_COLOUR = "exports.up = (knex) => knex.schema.alterTable(\"shelves\", (table) => table.string(\"colour\"));\n" +
132
+ "exports.down = () => {};\n";
133
+
134
+ test("an application whose migrations start from a baseline schema builds its snapshot with its own schema build", {
135
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
136
+ }, async (context) => {
137
+ const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-test-databases-baseline-"));
138
+ await mkdir(path.join(appRoot, "migrations", "constraints"), { recursive: true });
139
+ await writeFile(path.join(appRoot, "migrations", "20200101000000_shelf_colour.cjs"), SHELF_COLOUR);
140
+ await writeFile(path.join(appRoot, "baseline.sql"), "CREATE TABLE `shelves` (`id` INT PRIMARY KEY, `label` VARCHAR(40))");
141
+ const name = `${process.env.TEST_DB_NAME}_baseline`;
142
+ const environment = { ...process.env, DB_NAME: `${process.env.TEST_DB_NAME}_application`, DATABASE_URL: "", TEST_DB_NAME: name };
143
+ const knexConfig = createKnexMigrationConfig({ client: "mysql2", environment, appRoot });
144
+ const server = { ...knexConfig.connection, database: undefined };
145
+ const admin = knex({ client: "mysql2", connection: server });
146
+ context.after(async () => {
147
+ for (const database of [name, `${name}_1`]) await admin.raw("DROP DATABASE IF EXISTS ??", [database]);
148
+ await admin.destroy();
149
+ await rm(appRoot, { recursive: true, force: true });
150
+ });
151
+ // The baseline and the reference rows every test database starts with, then the migrations.
152
+ const schema = {
153
+ files: ["baseline.sql"],
154
+ async build(db) {
155
+ await db.raw(await readFile(path.join(appRoot, "baseline.sql"), "utf8"));
156
+ await db("shelves").insert({ id: 1, label: "Reference" });
157
+ await db.migrate.latest();
158
+ }
159
+ };
160
+
161
+ // The migrations alone cannot build this schema.
162
+ await assert.rejects(prepareTestDatabases({ knexConfig, environment, count: 2, appRoot }), /shelves/u);
163
+ const first = await prepareTestDatabases({ knexConfig, environment, count: 2, schema, appRoot });
164
+ assert.match(first.summary, /schema built once for a new snapshot/u);
165
+ assert.deepEqual(first.snapshot.migrations, ["20200101000000_shelf_colour.cjs"]);
166
+ for (const database of first.databases) {
167
+ const db = knex({ client: "mysql2", connection: { ...server, database } });
168
+ try {
169
+ assert.deepEqual(await db("shelves").select("id", "label", "colour"), [{ id: 1, label: "Reference", colour: null }]);
170
+ assert.equal(await matchesSnapshot(knexConfig, database, first.snapshot), true, database);
171
+ } finally {
172
+ await db.destroy();
173
+ }
174
+ }
175
+ assert.match((await prepareTestDatabases({ knexConfig, environment, count: 2, schema, appRoot })).summary, /stored snapshot/u);
176
+ await writeFile(path.join(appRoot, "baseline.sql"), "CREATE TABLE `shelves` (`id` BIGINT PRIMARY KEY, `label` VARCHAR(40))");
177
+ const changed = await prepareTestDatabases({ knexConfig, environment, count: 2, schema, appRoot });
178
+ assert.notEqual(changed.snapshot.key, first.snapshot.key);
179
+ assert.match(changed.summary, /schema built once for a new snapshot/u);
180
+ });
181
+
182
+ const REPO_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
183
+ const JSKIT_CLI = path.join(REPO_ROOT, "tooling", "jskit-catalog", "scripts", "jskit.mjs");
184
+ const FIXTURE_KNEXFILE = `import { createKnexMigrationConfig } from "@jskit-ai/database-runtime/server/knexMigrationConfig";
185
+ try {
186
+ process.loadEnvFile(".env");
187
+ } catch (error) {
188
+ if (error?.code !== "ENOENT") throw error;
189
+ }
190
+ export default createKnexMigrationConfig({ client: "mysql2", environment: process.env });
191
+ `;
192
+ // Each file waits until the other has started, so both hold a database at once, then records what it was given (the
193
+ // password as a digest).
194
+ const FIXTURE_TEST = (name, other) => `import { createHash } from "node:crypto";
195
+ import { existsSync, writeFileSync } from "node:fs";
196
+ import test from "node:test";
197
+ test("${name}", async () => {
198
+ writeFileSync("started-${name}", "");
199
+ const deadline = Date.now() + 30000;
200
+ while (!existsSync("started-${other}") && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 20));
201
+ writeFileSync("seen-${name}.json", JSON.stringify({
202
+ db: process.env.DB_NAME, test: process.env.TEST_DB_NAME, settings: process.env.JSKIT_TEST_DATABASES ?? null,
203
+ dotenv: process.env.FIXTURE_ONLY_IN_DOTENV ?? null, other: existsSync("started-${other}"),
204
+ connection: { host: process.env.DB_HOST, port: process.env.DB_PORT, user: process.env.DB_USER,
205
+ password: createHash("sha256").update(process.env.DB_PASSWORD ?? "(none)").digest("hex") }
206
+ }));
207
+ });
208
+ `;
209
+
210
+ test("jskit test --databases gives each test file running at once its own restored copy and its connection, and nothing else of the .env", {
211
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
212
+ }, async (context) => {
213
+ const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-test-databases-cli-"));
214
+ const name = `${process.env.TEST_DB_NAME}_cli`;
215
+ const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT || 3306), user: process.env.DB_USER,
216
+ password: process.env.DB_PASSWORD };
217
+ const admin = knex({ client: "mysql2", connection: server });
218
+ context.after(async () => {
219
+ for (const database of [name, `${name}_1`]) await admin.raw("DROP DATABASE IF EXISTS ??", [database]);
220
+ await admin.destroy();
221
+ await rm(appRoot, { recursive: true, force: true });
222
+ });
223
+ await mkdir(path.join(appRoot, "migrations", "constraints"), { recursive: true });
224
+ await mkdir(path.join(appRoot, "test"), { recursive: true });
225
+ await writeFile(path.join(appRoot, "migrations", "20200101000000_books.cjs"), BOOKS);
226
+ await writeFile(path.join(appRoot, "package.json"), JSON.stringify({ name: "cli-fixture", type: "module", private: true }));
227
+ await writeFile(path.join(appRoot, "knexfile.js"), FIXTURE_KNEXFILE);
228
+ // The knexfile loads the connection from it, which each test file gets with its database; nothing else of it.
229
+ await writeFile(path.join(appRoot, ".env"), [
230
+ `DB_HOST=${server.host}`, `DB_PORT=${server.port}`, `DB_USER=${server.user}`, `DB_PASSWORD=${server.password ?? ""}`,
231
+ "FIXTURE_ONLY_IN_DOTENV=1", ""
232
+ ].join("\n"), { mode: 0o600 });
233
+ await writeFile(path.join(appRoot, "test", "a.test.js"), FIXTURE_TEST("a", "b"));
234
+ await writeFile(path.join(appRoot, "test", "b.test.js"), FIXTURE_TEST("b", "a"));
235
+ // The fixture's own node_modules (its snapshot cache goes there), resolving JSKIT's packages from this repository.
236
+ await mkdir(path.join(appRoot, "node_modules"));
237
+ await symlink(path.join(REPO_ROOT, "node_modules", "@jskit-ai"), path.join(appRoot, "node_modules", "@jskit-ai"), "dir");
238
+
239
+ const env = { ...process.env, V64_TEST_CONCURRENCY: "2", DB_NAME: `${process.env.TEST_DB_NAME}_cli_application`,
240
+ TEST_DB_NAME: name, DATABASE_URL: "" };
241
+ for (const name of ["NODE_TEST_CONTEXT", "FIXTURE_ONLY_IN_DOTENV", "DB_HOST", "DB_PORT", "DB_USER", "DB_PASSWORD"]) {
242
+ delete env[name];
243
+ }
244
+ const { stderr } = await execFileAsync(process.execPath, [JSKIT_CLI, "test", "--databases"], { cwd: appRoot, env });
245
+ assert.match(stderr, /jskit test: 2 test database\(s\) from .+_cli ready in \d+ms: 1 migrations replayed once/u);
246
+ assert.match(stderr, /jskit test: 2 test file\(s\) at a time \(test databases\)\./u);
247
+ const seen = await Promise.all(["a", "b"].map(async (file) => JSON.parse(await readFile(path.join(appRoot, `seen-${file}.json`), "utf8"))));
248
+ assert.deepEqual(seen.map(({ db }) => db).sort(), [name, `${name}_1`]);
249
+ const connection = { host: server.host, port: String(server.port), user: server.user,
250
+ password: createHash("sha256").update(server.password ?? "").digest("hex") };
251
+ for (const { db, test: testDatabase, settings, dotenv, other, connection: given } of seen) {
252
+ assert.equal(testDatabase, db);
253
+ assert.equal(settings, null);
254
+ assert.equal(dotenv, null);
255
+ assert.equal(other, true);
256
+ assert.deepEqual(given, connection);
257
+ }
258
+ const copy = knex({ client: "mysql2", connection: { ...server, database: `${name}_1` } });
259
+ try {
260
+ assert.deepEqual(await copy("books").pluck("title"), ["Written by the migration"]);
261
+ } finally {
262
+ await copy.destroy();
263
+ }
264
+
265
+ // A focused run of one file prepares one database, from the stored snapshot.
266
+ const focused = await execFileAsync(process.execPath, [JSKIT_CLI, "test", "--databases", "test/a.test.js"], { cwd: appRoot, env });
267
+ assert.match(focused.stderr, /jskit test: 1 test database\(s\) from .+_cli ready in \d+ms: stored snapshot/u);
268
+ });
269
+
270
+ test("countQueries() counts a MySQL or MariaDB path's statements, its transactions' included", {
271
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
272
+ }, async (context) => {
273
+ const database = `${process.env.TEST_DB_NAME}_queries`;
274
+ const environment = { ...process.env, DB_NAME: database, DATABASE_URL: "" };
275
+ const { connection } = createKnexMigrationConfig({ client: "mysql2", environment, appRoot: os.tmpdir() });
276
+ const server = knex({ client: "mysql2", connection: { ...connection, database: undefined } });
277
+ await server.raw("CREATE DATABASE IF NOT EXISTS ??", [database]);
278
+ const db = knex({ client: "mysql2", connection });
279
+ context.after(async () => {
280
+ await db.destroy();
281
+ await server.raw("DROP DATABASE IF EXISTS ??", [database]);
282
+ await server.destroy();
283
+ });
284
+ await db.schema.createTable("books", (table) => { table.increments("id"); table.string("title"); });
285
+ await db("books").insert(Array.from({ length: 30 }, (_, index) => ({ title: `Book ${index + 1}` })));
286
+ const ids = await db("books").orderBy("id").pluck("id");
287
+
288
+ const perBook = await countQueries(db, () => Promise.all(ids.map((id) => db("books").where({ id }).first("title"))));
289
+ assert.deepEqual([perBook.count, perBook.queries[29]], [30, "select `title` from `books` where `id` = ? limit ?"]);
290
+ const together = await countQueries(db, () => db("books").whereIn("id", ids).pluck("title"));
291
+ assert.deepEqual([together.count, together.result.length], [1, 30]);
292
+ const transaction = await countQueries(db, () => db.transaction((trx) => trx("books").count({ books: "*" })));
293
+ assert.deepEqual(transaction.queries, ["BEGIN;", "select count(*) as `books` from `books`", "COMMIT;"]);
294
+ });
295
+
296
+ test("countRuntimeQueries() sees a route of a server booted in the test read per row, at 10 rows and at 200", {
297
+ skip: !process.env.TEST_DB_NAME || !process.env.DB_HOST
298
+ }, async (context) => {
299
+ const database = `${process.env.TEST_DB_NAME}_routes`;
300
+ const environment = { ...process.env, DB_NAME: database, DATABASE_URL: "" };
301
+ const { connection } = createKnexMigrationConfig({ client: "mysql2", environment, appRoot: os.tmpdir() });
302
+ const server = knex({ client: "mysql2", connection: { ...connection, database: undefined } });
303
+ await server.raw("CREATE DATABASE IF NOT EXISTS ??", [database]);
304
+ const testKnex = knex({ client: "mysql2", connection });
305
+ const app = Fastify({ logger: false });
306
+ const routes = defineProvider({
307
+ id: "test.books.routes",
308
+ requires: { database: "runtime.database" },
309
+ setup({ database: { knex: db } }) {
310
+ // The list's books, read one by one, and the same list in one read.
311
+ app.get("/books", async (request) => {
312
+ const ids = await db("books").orderBy("id").limit(Number(request.query.limit)).pluck("id");
313
+ const books = [];
314
+ for (const id of ids) books.push(await db("books").where({ id }).first("id", "title"));
315
+ return books;
316
+ });
317
+ app.get("/books/batched", async (request) => db("books").orderBy("id").limit(Number(request.query.limit)).select("id", "title"));
318
+ return {};
319
+ }
320
+ });
321
+ const runtime = createCapabilityRuntime({
322
+ inputs: { "runtime.env": environment },
323
+ providers: [MysqlDatabaseDriverProvider, DatabaseProvider, routes]
324
+ });
325
+ context.after(async () => {
326
+ await app.close();
327
+ await runtime.shutdown();
328
+ await testKnex.destroy();
329
+ await server.raw("DROP DATABASE IF EXISTS ??", [database]);
330
+ await server.destroy();
331
+ });
332
+ await testKnex.schema.createTable("books", (table) => { table.increments("id"); table.string("title"); });
333
+ await testKnex("books").insert(Array.from({ length: 200 }, (_, index) => ({ title: `Book ${index + 1}` })));
334
+ await runtime.start();
335
+
336
+ const listed = async (url) => {
337
+ const { result: response, count, queries } = await countRuntimeQueries(async () => {
338
+ // The test's own Knex is not the runtime's, so its statements do not count.
339
+ await testKnex("books").count({ books: "*" });
340
+ return app.inject(url);
341
+ });
342
+ assert.equal(response.statusCode, 200, url);
343
+ return { count, books: response.json().length, last: queries.at(-1) };
344
+ };
345
+ const perBook = "select `id`, `title` from `books` where `id` = ? limit ?";
346
+ assert.deepEqual(await listed("/books?limit=10"), { count: 11, books: 10, last: perBook });
347
+ assert.deepEqual(await listed("/books?limit=200"), { count: 201, books: 200, last: perBook });
348
+ const batched = "select `id`, `title` from `books` order by `id` asc limit ?";
349
+ assert.deepEqual(await listed("/books/batched?limit=10"), { count: 1, books: 10, last: batched });
350
+ assert.deepEqual(await listed("/books/batched?limit=200"), { count: 1, books: 200, last: batched });
351
+ });
@@ -1,68 +0,0 @@
1
- import knex from "knex";
2
-
3
- // Application-owned test setup. Schema migration tests use a separate fresh database.
4
- async function prepareBrowserTestDatabase({ knexConfig, environment = process.env, seed }) {
5
- const started = performance.now();
6
- if (environment.NODE_ENV === "production") throw new Error("Browser tests cannot prepare a production runtime.");
7
- if (typeof seed !== "function") throw new TypeError("An explicit browser fixture seed is required.");
8
- if (knexConfig?.client !== "mysql2" || !knexConfig.connection || typeof knexConfig.connection !== "object") {
9
- throw new Error("Browser preparation requires a resolved MySQL Knex configuration.");
10
- }
11
- const database = String(environment.BROWSER_TEST_DB_NAME || "").trim();
12
- if (!/^[a-zA-Z0-9_]{1,64}$/.test(database)) throw new Error("BROWSER_TEST_DB_NAME must name the exact browser database.");
13
- const protectedNames = [knexConfig.connection.database, environment.DB_NAME, environment.TEST_DB_NAME];
14
- if (environment.DATABASE_URL) {
15
- try { protectedNames.push(decodeURIComponent(new URL(environment.DATABASE_URL).pathname.slice(1))); }
16
- catch { throw new Error("DATABASE_URL must be a valid database URL."); }
17
- }
18
- if (!knexConfig.connection.database || protectedNames.some(name => String(name || "").trim().toLowerCase() === database.toLowerCase())) {
19
- throw new Error("BROWSER_TEST_DB_NAME must differ from the application and disposable TEST_DB_NAME databases.");
20
- }
21
- const adminConnection = { ...knexConfig.connection };
22
- delete adminConnection.database;
23
- const admin = knex({ ...knexConfig, connection: adminConnection, pool: { min: 0, max: 1 } });
24
- let schemaExisted;
25
- try {
26
- const [[row]] = await admin.raw("SELECT COUNT(*) AS count FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?", [database]);
27
- schemaExisted = Number(row.count) === 1;
28
- await admin.raw("CREATE DATABASE IF NOT EXISTS ??", [database]);
29
- }
30
- finally { await admin.destroy(); }
31
- console.info(`Browser database ${database}: ${schemaExisted ? "existing schema" : "new schema"}.`);
32
- const db = knex({ ...knexConfig, connection: { ...knexConfig.connection, database } });
33
- try {
34
- const migrationStarted = performance.now();
35
- const [, migrations] = await db.migrate.latest();
36
- const migrationMs = Math.round(performance.now() - migrationStarted);
37
- console.info(`Browser migrations: ${migrations.length} applied in ${migrationMs}ms.`);
38
- const resetStarted = performance.now();
39
- let resetTables = 0;
40
- const connection = await db.client.acquireConnection();
41
- try {
42
- const [[actual]] = await db.raw("SELECT DATABASE() AS databaseName").connection(connection);
43
- if (actual.databaseName !== database) throw new Error("Browser reset requires the exact BROWSER_TEST_DB_NAME connection.");
44
- const [tables] = await db.raw(
45
- "SELECT TABLE_NAME AS name FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_TYPE = 'BASE TABLE'", [database]
46
- ).connection(connection);
47
- const ledger = knexConfig.migrations?.tableName || "knex_migrations";
48
- await db.raw("SET FOREIGN_KEY_CHECKS = 0").connection(connection);
49
- try {
50
- for (const { name } of tables) {
51
- if (name === ledger || name === `${ledger}_lock`) continue;
52
- await db.raw("TRUNCATE TABLE ??", [name]).connection(connection);
53
- resetTables += 1;
54
- }
55
- } finally { await db.raw("SET FOREIGN_KEY_CHECKS = 1").connection(connection); }
56
- } finally { await db.client.releaseConnection(connection); }
57
- const resetMs = Math.round(performance.now() - resetStarted);
58
- console.info(`Browser fixture reset: ${resetTables} tables in ${resetMs}ms.`);
59
- const seedStarted = performance.now();
60
- await db.transaction(seed);
61
- const seedMs = Math.round(performance.now() - seedStarted);
62
- const totalMs = Math.round(performance.now() - started);
63
- console.info(`Browser fixture seed: ${seedMs}ms; database preparation total: ${totalMs}ms.`);
64
- return { database, migrations, schemaExisted, timings: { migrationMs, resetMs, seedMs, totalMs } };
65
- } finally { await db.destroy(); }
66
- }
67
-
68
- export { prepareBrowserTestDatabase };
@@ -1,82 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import knex from "knex";
4
- import { prepareBrowserTestDatabase } from "../patterns/mysql-application/example/tests/browser/database.js";
5
-
6
- const seed = async (db) => db("fixtures").insert({ id: 1, label: "baseline" });
7
-
8
- test("browser preparation rejects missing, ordinary and production databases before connecting", async () => {
9
- const knexConfig = { client: "mysql2", connection: { database: "application" } };
10
- for (const environment of [
11
- {}, { BROWSER_TEST_DB_NAME: "application" }, { BROWSER_TEST_DB_NAME: "APPLICATION" },
12
- { BROWSER_TEST_DB_NAME: "other", DB_NAME: "other" },
13
- { BROWSER_TEST_DB_NAME: "other", DATABASE_URL: "mysql://localhost/other" },
14
- { BROWSER_TEST_DB_NAME: "tests", TEST_DB_NAME: "TESTS" },
15
- { BROWSER_TEST_DB_NAME: "tests", NODE_ENV: "production" }
16
- ]) {
17
- await assert.rejects(prepareBrowserTestDatabase({ knexConfig, environment, seed }), /TEST_DB_NAME|production/u);
18
- }
19
- await assert.rejects(prepareBrowserTestDatabase({ knexConfig, environment: { BROWSER_TEST_DB_NAME: "tests" } }), /seed/u);
20
- });
21
-
22
- test("native browser preparation retains schema, clears data and applies only new migrations", {
23
- skip: !process.env.BROWSER_TEST_DB_NAME || !process.env.TEST_DB_NAME || !process.env.DB_HOST
24
- }, async () => {
25
- const environment = process.env;
26
- const connection = { host: environment.DB_HOST, port: Number(environment.DB_PORT), user: environment.DB_USER,
27
- password: environment.DB_PASSWORD, database: environment.DB_NAME };
28
- const admin = knex({ client: "mysql2", connection: { ...connection, database: undefined } });
29
- const [[existing]] = await admin.raw("SELECT COUNT(*) AS n FROM information_schema.SCHEMATA WHERE SCHEMA_NAME IN (?, ?)", [environment.BROWSER_TEST_DB_NAME, environment.TEST_DB_NAME]);
30
- if (Number(existing.n)) {
31
- await admin.destroy();
32
- throw new Error("The native pattern regression requires fresh, explicitly supplied test database names.");
33
- }
34
- let db;
35
- let applied = 0;
36
- const migrations = [{ name: "001_fixture.js", async up(db) {
37
- applied += 1;
38
- await db.schema.createTable("fixtures", table => { table.increments("id"); table.string("label"); });
39
- await db.schema.createTable("children", table => { table.increments("id"); table.integer("parent_id").unsigned().references("fixtures.id"); });
40
- }, async down() {} }];
41
- const knexConfig = { client: "mysql2", connection, migrations: { migrationSource: {
42
- getMigrations: () => migrations, getMigrationName: migration => migration.name, getMigration: migration => migration
43
- } } };
44
- try {
45
- const [normalBefore] = await admin.raw("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?", [environment.DB_NAME]);
46
- const first = await prepareBrowserTestDatabase({ knexConfig, environment, seed });
47
- assert.deepEqual(first.migrations, ["001_fixture.js"]);
48
- assert.equal(first.schemaExisted, false);
49
- db = knex({ client: "mysql2", connection: { ...connection, database: environment.BROWSER_TEST_DB_NAME } });
50
- const ledger = await db("knex_migrations").select();
51
- await db("fixtures").insert({ id: 2, label: "stale" });
52
- await db("children").insert({ parent_id: 2 });
53
- // A disposable integration test between browser starts must not erase its schema.
54
- await admin.raw("CREATE DATABASE ??", [environment.TEST_DB_NAME]);
55
- await admin.raw("CREATE TABLE ??.proof (id INT PRIMARY KEY)", [environment.TEST_DB_NAME]);
56
- await admin.raw("DROP DATABASE ??", [environment.TEST_DB_NAME]);
57
- const second = await prepareBrowserTestDatabase({ knexConfig, environment, seed });
58
- assert.equal(second.schemaExisted, true);
59
- assert.deepEqual(second.migrations, []);
60
- assert.equal(applied, 1);
61
- assert.deepEqual(await db("knex_migrations").select(), ledger);
62
- assert.deepEqual(await db("fixtures").select(), [{ id: 1, label: "baseline" }]);
63
- assert.deepEqual(await db("children").select(), []);
64
- // The preparation connection has foreign-key checks restored before seeding.
65
- await assert.rejects(prepareBrowserTestDatabase({ knexConfig, environment,
66
- seed: async db => db("children").insert({ parent_id: 99 }) }), /foreign key constraint/iu);
67
- migrations.push({ name: "002_pending.js", async up(db) {
68
- await db.schema.alterTable("fixtures", table => table.string("extra"));
69
- }, async down() {} });
70
- const third = await prepareBrowserTestDatabase({ knexConfig, environment, seed });
71
- assert.deepEqual(third.migrations, ["002_pending.js"]);
72
- assert.equal(await db.schema.hasColumn("fixtures", "extra"), true);
73
- assert.equal(applied, 1);
74
- const [normalAfter] = await admin.raw("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?", [environment.DB_NAME]);
75
- assert.deepEqual(normalAfter, normalBefore);
76
- } finally {
77
- await db?.destroy();
78
- await admin.raw("DROP DATABASE IF EXISTS ??", [environment.BROWSER_TEST_DB_NAME]);
79
- await admin.raw("DROP DATABASE IF EXISTS ??", [environment.TEST_DB_NAME]);
80
- await admin.destroy();
81
- }
82
- });