@jskit-ai/database-runtime 0.1.272 → 0.1.274

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - **Behaviour change.** `jskit test --databases` gives every test file the snapshot, rows included: a test database an earlier file of the run used is restored before the next file takes it. It was restored only when its tables or migration ledger had changed (a migration test), so rows an earlier file wrote stayed, and a file's result could depend on which files ran before it on the same copy (for example an app's first account, which only an empty database admits). The run's first file on each copy still takes it as the run restored it; each later file costs one restore, a template copy on PostgreSQL and a table rebuild on MySQL and MariaDB.
5
6
  - `countQueries(knex, work)` and `countRuntimeQueries(work)` count only the statements `work` sends, with those of the async work it starts (they run it in an `AsyncLocalStorage` scope): another test, or a background loop, sending through the same Knex or runtime meanwhile is left out, where its statements counted too and could make a flat path look as if it grew. A loop started before `work` that sends on its behalf is left out as well. A count nested in another counts for both, as before.
6
7
  - `./server/testDatabases` exports `createPackageTestKnex(t, { environment })`, the Knex a package's own data test runs on when it creates its tables in the test: an in-memory SQLite database, or on a `DB_CLIENT=pg` run a schema of the test's own in `TEST_DB_NAME` (on DB_HOST, DB_PORT, DB_USER and DB_PASSWORD), in UTC, with a pool of up to eight connections for concurrent transactions, dropped with everything in it after the test. It refuses `NODE_ENV=production`, and a pg run without `TEST_DB_NAME` fails (`TestDatabaseMissingError`).
7
8
  - `listForeignKeys(knex)` (`./server/foreignKeyMigrations`) answers every foreign key of the connected database's tables in one statement, each with its table, its columns in order and the table and columns it references, on MySQL, MariaDB, PostgreSQL (the current schema) and SQLite (whose keys have no name). `testDatabaseRun` takes `checkDatabase(knex)`, run once on the prepared `TEST_DB_NAME` before any test file: its answer joins the summary and a throw fails the run.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/database-runtime",
3
- "version": "0.1.272",
3
+ "version": "0.1.274",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -82,6 +82,6 @@
82
82
  }
83
83
  },
84
84
  "peerDependencies": {
85
- "@jskit-ai/kernel": "0.2.23"
85
+ "@jskit-ai/kernel": "0.2.25"
86
86
  }
87
87
  }
@@ -33,7 +33,8 @@ function createKnexMigrationConfig({
33
33
  }
34
34
  const dialectId = normalizeDatabaseClient(client);
35
35
  const resolvedMigrationsDirectory = path.resolve(appRoot, migrationsDirectory);
36
- // An application needs no constraints/ until it has a constraint; Knex refuses a missing directory.
36
+ // An application needs no migrations/ until it has a migration of its own (its packages may hold all its tables), and
37
+ // no constraints/ until it has a constraint; Knex refuses a missing directory.
37
38
  const applicationConstraintsDirectory = path.join(resolvedMigrationsDirectory, "constraints");
38
39
  const pool = resolveKnexPool(dialectId);
39
40
 
@@ -50,7 +51,7 @@ function createKnexMigrationConfig({
50
51
  // can then reference application tables such as users.
51
52
  directory: [
52
53
  ...packageMigrationDirectories.map((directory) => path.resolve(directory)),
53
- resolvedMigrationsDirectory,
54
+ ...(existsSync(resolvedMigrationsDirectory) ? [resolvedMigrationsDirectory] : []),
54
55
  ...packageConstraintDirectories.map((directory) => path.resolve(directory)),
55
56
  ...(existsSync(applicationConstraintsDirectory) ? [applicationConstraintsDirectory] : [])
56
57
  ],
@@ -1,15 +1,16 @@
1
1
  // Preloaded (`--import`) by `jskit test --databases` into every test file's process. When the run has disposable
2
2
  // databases (JSKIT_TEST_DATABASES names the run's directory, from testDatabaseRun() in testDatabases.js), the file takes
3
3
  // one no other file is using, for as long as it runs, as both DB_NAME and TEST_DB_NAME, with the connection it was
4
- // restored on (DB_HOST, DB_PORT, DB_USER, DB_PASSWORD): it never reaches the developer's database. A database a file
5
- // left with other tables or another migration ledger (a migration test) is restored from the snapshot before the next
6
- // file takes it.
4
+ // restored on (DB_HOST, DB_PORT, DB_USER, DB_PASSWORD): it never reaches the developer's database. Every file starts
5
+ // from the snapshot, rows included: a database an earlier file of the run used is restored before the next file takes
6
+ // it, since that file may have written rows, or changed tables or the migration ledger (a migration test).
7
7
  //
8
8
  // The directory holds the run's settings (only this user may read them: they include the connection password, which
9
- // reaches only the test files' own processes) and a lock file per database holding its user's process id. The runner's own `node --test` process and every worker
9
+ // reaches only the test files' own processes), a lock file per database holding its user's process id, and a marker
10
+ // per database an earlier file used. The runner's own `node --test` process and every worker
10
11
  // thread a test starts load this module too, since they inherit --import: there it does nothing (and loads nothing
11
12
  // more), and nothing a file starts takes a database of its own.
12
- import { closeSync, openSync, readFileSync, rmSync, writeSync } from "node:fs";
13
+ import { closeSync, existsSync, openSync, readFileSync, rmSync, writeFileSync, writeSync } from "node:fs";
13
14
  import path from "node:path";
14
15
  import process from "node:process";
15
16
  import { isMainThread } from "node:worker_threads";
@@ -25,7 +26,10 @@ function isRunning(pid) {
25
26
  }
26
27
  }
27
28
 
28
- /** Takes the first free database; a lock left by a process that died without releasing it is taken over. */
29
+ /**
30
+ * Takes the first free database, and answers it and whether an earlier file of the run used it; a lock left by a
31
+ * process that died without releasing it is taken over.
32
+ */
29
33
  async function claimDatabase(directory, databases) {
30
34
  const deadline = Date.now() + CLAIM_TIMEOUT_MS;
31
35
  while (Date.now() < deadline) {
@@ -36,7 +40,10 @@ async function claimDatabase(directory, databases) {
36
40
  writeSync(descriptor, String(process.pid));
37
41
  closeSync(descriptor);
38
42
  process.once("exit", () => rmSync(lock, { force: true }));
39
- return database;
43
+ const marker = path.join(directory, `${index}.used`);
44
+ const used = existsSync(marker);
45
+ if (!used) writeFileSync(marker, "");
46
+ return { database, used };
40
47
  } catch (error) {
41
48
  if (error.code !== "EEXIST") throw error;
42
49
  }
@@ -56,10 +63,11 @@ async function claimDatabase(directory, databases) {
56
63
  const directory = process.env.JSKIT_TEST_DATABASES;
57
64
  if (isMainThread && process.env.NODE_TEST_CONTEXT && directory) {
58
65
  delete process.env.JSKIT_TEST_DATABASES;
59
- const { RUN_SETTINGS_FILE, matchesSnapshot, restoreTestDatabase } = await import("./testDatabases.js");
66
+ const { RUN_SETTINGS_FILE, restoreTestDatabase } = await import("./testDatabases.js");
60
67
  const { databases, snapshot, knexConfig } = JSON.parse(readFileSync(path.join(directory, RUN_SETTINGS_FILE), "utf8"));
61
- const database = await claimDatabase(directory, databases);
62
- if (!(await matchesSnapshot(knexConfig, database, snapshot))) {
68
+ // The run restored every database as it started; one an earlier file used holds what that file left.
69
+ const { database, used } = await claimDatabase(directory, databases);
70
+ if (used) {
63
71
  await restoreTestDatabase(knexConfig, database, snapshot);
64
72
  }
65
73
  const { host, port, user, password } = knexConfig.connection;
@@ -1,4 +1,5 @@
1
1
  import assert from "node:assert/strict";
2
+ import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
2
3
  import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
3
4
  import os from "node:os";
4
5
  import path from "node:path";
@@ -17,17 +18,20 @@ const connectionEnvironment = Object.freeze({
17
18
  DB_PASSWORD: "secret"
18
19
  });
19
20
 
20
- test("createKnexMigrationConfig fixes MySQL without requiring DB_CLIENT", () => {
21
+ test("createKnexMigrationConfig fixes MySQL without requiring DB_CLIENT", (context) => {
22
+ const appRoot = mkdtempSync(path.join(os.tmpdir(), "jskit-app-mysql-"));
23
+ context.after(() => rmSync(appRoot, { recursive: true, force: true }));
24
+ mkdirSync(path.join(appRoot, "migrations"));
21
25
  const config = createKnexMigrationConfig({
22
26
  client: "mysql2",
23
27
  environment: connectionEnvironment,
24
- appRoot: "/srv/catalogue"
28
+ appRoot
25
29
  });
26
30
 
27
31
  assert.equal(config.client, "mysql2");
28
32
  assert.equal(config.connection.port, 3306);
29
33
  assert.equal(config.pool, undefined);
30
- assert.deepEqual(config.migrations.directory, ["/srv/catalogue/migrations"]);
34
+ assert.deepEqual(config.migrations.directory, [path.join(appRoot, "migrations")]);
31
35
  });
32
36
 
33
37
  test("an application without migrations/constraints/ migrates, and its constraints run once the directory exists", async (context) => {
@@ -63,6 +67,31 @@ test("an application without migrations/constraints/ migrates, and its constrain
63
67
  });
64
68
  });
65
69
 
70
+ test("an application without migrations/ migrates the tables its packages hold", async (context) => {
71
+ const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-app-no-migrations-"));
72
+ context.after(() => rm(appRoot, { recursive: true, force: true }));
73
+ const packageRoot = path.join(appRoot, "node_modules", "@example", "ledger-core");
74
+ await mkdir(path.join(packageRoot, "migrations"), { recursive: true });
75
+ await writeFile(path.join(appRoot, "package.json"), JSON.stringify({ dependencies: { "@example/ledger-core": "1.0.0" } }));
76
+ await writeFile(path.join(packageRoot, "package.json"), JSON.stringify({
77
+ name: "@example/ledger-core",
78
+ version: "1.0.0",
79
+ jskit: { kind: "runtime", migrations: { directories: ["migrations"] } }
80
+ }));
81
+ await writeFile(path.join(packageRoot, "migrations", "20260101000000_entries.cjs"),
82
+ "exports.up = (knex) => knex.schema.createTable(\"entries\", (table) => table.increments(\"id\"));\nexports.down = () => {};\n");
83
+
84
+ const { migrations } = await createKnexMigrationConfigFromApp({ client: "pg", environment: connectionEnvironment, appRoot });
85
+ assert.deepEqual(migrations.directory, [path.join(packageRoot, "migrations")]);
86
+ const knex = knexFactory({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true, migrations });
87
+ try {
88
+ await knex.migrate.latest();
89
+ assert.deepEqual(await knex("knex_migrations").orderBy("id").pluck("name"), ["20260101000000_entries.cjs"]);
90
+ } finally {
91
+ await knex.destroy();
92
+ }
93
+ });
94
+
66
95
  test("createKnexMigrationConfig fixes PostgreSQL without requiring DB_CLIENT", () => {
67
96
  const config = createKnexMigrationConfig({
68
97
  client: "pg",
@@ -90,6 +119,7 @@ test("installed package migrations run from their owning packages without copied
90
119
  });
91
120
 
92
121
  const packageRoot = path.join(appRoot, "node_modules", "@example", "accounts-core");
122
+ await mkdir(path.join(appRoot, "migrations"), { recursive: true });
93
123
  await mkdir(path.join(packageRoot, "migrations", "constraints"), { recursive: true });
94
124
  await writeFile(path.join(appRoot, "package.json"), JSON.stringify({
95
125
  dependencies: { "@example/accounts-core": "1.0.0" }
@@ -292,6 +322,8 @@ test("a package's migrations and constraints run after those of the packages its
292
322
  directoryOf["@example/notes-core:migrations"]
293
323
  ];
294
324
  assert.deepEqual(await discoverPackageMigrationDirectories({ appRoot }), packageDirectories);
325
+ // The application's own migrations run after every package's and before any constraints.
326
+ await mkdir(path.join(appRoot, "migrations"));
295
327
  const { migrations } = await createKnexMigrationConfigFromApp({ client: "mysql2", environment: connectionEnvironment, appRoot });
296
328
  assert.deepEqual(migrations.directory, [
297
329
  ...packageDirectories,