@jskit-ai/database-runtime 0.1.276 → 0.1.277

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
+ - **Breaking:** `PROD_DB_NAME` is gone. A test database, or one of its numbered copies, named like `STAGING_DB_NAME` or `PRODUCTION_DB_NAME`, the Staging and Production databases an application names on a server it shares with its test databases, is refused as one named like `PROD_DB_NAME` was (`... must differ from the application's DB_NAME, DATABASE_URL, STAGING_DB_NAME and PRODUCTION_DB_NAME (name).`). While `PROD_DB_NAME`, or another variable JSKIT no longer reads, is set, `requireDisposableDatabase`, `prepareTestDatabases` and `testDatabaseRun` (before it chooses one file at a time) refuse, naming what replaced it.
5
6
  - **Breaking:** test databases are refused in staging and production (`APP_ENV`), not under `NODE_ENV=production`: `Test databases are never prepared when APP_ENV is staging.` and `Package data tests never run when APP_ENV is production.`; a missing `APP_ENV` throws.
6
7
  - **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.
7
8
  - `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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/database-runtime",
3
- "version": "0.1.276",
3
+ "version": "0.1.277",
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.27"
85
+ "@jskit-ai/kernel": "0.2.28"
86
86
  }
87
87
  }
@@ -15,8 +15,10 @@
15
15
  // - PostgreSQL: a prepared database, `<name>_snapshot`, that every copy is created from with
16
16
  // CREATE DATABASE ... TEMPLATE (STRATEGY FILE_COPY from PostgreSQL 15).
17
17
  //
18
- // The database the application connects to (DB_NAME, the one DATABASE_URL names, the Knex configuration's own) and
19
- // PROD_DB_NAME are never touched. Two runs at once need different database names, as before.
18
+ // The database the application connects to (DB_NAME, the one DATABASE_URL names, the Knex configuration's own), and the
19
+ // Staging and Production databases an application names on that server (STAGING_DB_NAME, PRODUCTION_DB_NAME), are never
20
+ // touched. A preparation refuses to start while a variable JSKIT no longer reads is set, PROD_DB_NAME among them
21
+ // (`assertNoRetiredVariables`). Two runs at once need different database names, as before.
20
22
  //
21
23
  // A database test that guards a path's cost counts the statements the path sends with countQueries(), or, for a route
22
24
  // of a server the test booted, countRuntimeQueries(); expectFlatQueries() in @jskit-ai/resource-runtime/test compares
@@ -29,7 +31,8 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, w
29
31
  import os from "node:os";
30
32
  import path from "node:path";
31
33
  import { fileURLToPath } from "node:url";
32
- import { appEnvironment, isDeployed } from "@jskit-ai/kernel/shared/support/appEnvironment";
34
+ import { assertNoRetiredVariables } from "@jskit-ai/kernel/server/runtime/appEnvironment";
35
+ import { appEnvironment, isStagingOrProduction } from "@jskit-ai/kernel/shared/support/appEnvironment";
33
36
  import { parseDatabaseUrl, resolveKnexConnectionFromEnvironment, resolveKnexPool } from "../shared/databaseConnection.js";
34
37
  import { detectDialectFromClient } from "../shared/dialect.js";
35
38
  import { RUNTIME_QUERY_CHANNEL } from "./database.js";
@@ -39,6 +42,8 @@ const SNAPSHOT_DIRECTORY = path.join("node_modules", ".cache", "jskit-test-datab
39
42
  // Room for a `_snapshot` or copy suffix within MySQL's 64 and PostgreSQL's 63 characters.
40
43
  const DISPOSABLE_NAME = /^[A-Za-z0-9_]{1,48}$/u;
41
44
  const TEST_DATABASE_VARIABLES = Object.freeze(["TEST_DB_NAME", "BROWSER_TEST_DB_NAME"]);
45
+ // The Staging and Production databases an application names on a server it shares with its test databases.
46
+ const PROTECTED_DATABASE_VARIABLES = Object.freeze(["STAGING_DB_NAME", "PRODUCTION_DB_NAME"]);
42
47
  const MYSQL_ACCESS_DENIED = new Set(["ER_DBACCESS_DENIED_ERROR", "ER_ACCESS_DENIED_ERROR", "ER_SPECIFIC_ACCESS_DENIED_ERROR"]);
43
48
  // A database or table the user may not use (not a wrong password, which fails the run).
44
49
  const MYSQL_REFUSED_PRIVILEGE = new Set(["ER_DBACCESS_DENIED_ERROR", "ER_SPECIFIC_ACCESS_DENIED_ERROR", "ER_TABLEACCESS_DENIED_ERROR"]);
@@ -92,24 +97,31 @@ function connectedDatabaseName(environment) {
92
97
  }
93
98
 
94
99
  /**
95
- * The disposable database `variable` names, checked with its first `count - 1` copies: never in staging or
96
- * production (APP_ENV), never the application's database (DB_NAME, DATABASE_URL's, PROD_DB_NAME, and `connected`, the
97
- * one its Knex configuration names), and never the other test database or its copies.
100
+ * The disposable database `variable` names, checked with its first `count - 1` copies: never while a variable JSKIT no
101
+ * longer reads is set (`assertNoRetiredVariables`, PROD_DB_NAME among them), never in staging or production (APP_ENV),
102
+ * never the application's database (DB_NAME, DATABASE_URL's and `connected`, the one its Knex configuration names) or
103
+ * its Staging or Production database (STAGING_DB_NAME, PRODUCTION_DB_NAME), and never the other test database or its
104
+ * copies.
98
105
  * @param {Record<string, string | undefined>} environment
99
106
  * @param {"TEST_DB_NAME" | "BROWSER_TEST_DB_NAME"} variable
100
107
  * @param {number} [count]
101
108
  * @param {{ connected?: string }} [options]
102
109
  */
103
110
  function requireDisposableDatabase(environment, variable, count = 1, { connected = "" } = {}) {
104
- if (isDeployed(environment)) {
111
+ assertNoRetiredVariables(environment);
112
+ if (isStagingOrProduction(environment)) {
105
113
  throw new Error(`Test databases are never prepared when APP_ENV is ${appEnvironment(environment)}.`);
106
114
  }
107
115
  const name = String(environment[variable] || "").trim();
108
116
  if (!DISPOSABLE_NAME.test(name)) {
109
117
  throw new Error(`${variable} must name a disposable test database: letters, digits and _, at most 48 characters.`);
110
118
  }
111
- const application = [connectedDatabaseName(environment), environment.DB_NAME, environment.PROD_DB_NAME, connected]
112
- .map(folded).filter(Boolean);
119
+ const application = [
120
+ connectedDatabaseName(environment),
121
+ environment.DB_NAME,
122
+ ...PROTECTED_DATABASE_VARIABLES.map((protectedName) => environment[protectedName]),
123
+ connected
124
+ ].map(folded).filter(Boolean);
113
125
  const others = TEST_DATABASE_VARIABLES.filter((other) => other !== variable)
114
126
  .map((other) => folded(environment[other])).filter(Boolean);
115
127
  // `copy` is `original` or one of its numbered copies, whatever the count of either run.
@@ -117,7 +129,8 @@ function requireDisposableDatabase(environment, variable, count = 1, { connected
117
129
  (copy.startsWith(`${original}_`) && /^\d+$/u.test(copy.slice(original.length + 1)));
118
130
  for (const candidate of testDatabaseNames(name, count).map(folded)) {
119
131
  if (application.includes(candidate)) {
120
- throw new Error(`${variable} and its copies must differ from the application's DB_NAME, DATABASE_URL and PROD_DB_NAME (${candidate}).`);
132
+ throw new Error(`${variable} and its copies must differ from the application's DB_NAME, DATABASE_URL, ` +
133
+ `STAGING_DB_NAME and PRODUCTION_DB_NAME (${candidate}).`);
121
134
  }
122
135
  }
123
136
  for (const other of others) {
@@ -488,6 +501,7 @@ function refusedPrivilege(error) {
488
501
  * before any test file: what it answers joins the summary, and a throw fails the run.
489
502
  */
490
503
  async function testDatabaseRun({ knexConfig, environment, count, schema, appRoot = process.cwd(), checkDatabase }) {
504
+ assertNoRetiredVariables(environment);
491
505
  const oneAtATime = (reason) => ({
492
506
  concurrency: 1,
493
507
  env: {},
@@ -635,7 +649,7 @@ async function createPackageTestKnex(context, { environment }) {
635
649
  context.after(() => knex.destroy());
636
650
  return knex;
637
651
  }
638
- if (isDeployed(environment)) {
652
+ if (isStagingOrProduction(environment)) {
639
653
  throw new Error(`Package data tests never run when APP_ENV is ${appEnvironment(environment)}.`);
640
654
  }
641
655
  const connection = {
@@ -9,6 +9,7 @@ import {
9
9
  TestDatabaseMissingError,
10
10
  createPackageTestKnex,
11
11
  countQueries,
12
+ prepareTestDatabases,
12
13
  requireDisposableDatabase,
13
14
  requireTestDatabase,
14
15
  tableNames,
@@ -38,9 +39,10 @@ test("a test database and its copies are never the application's database, stagi
38
39
  for (const [overrides, variable, count, message] of [
39
40
  [{ TEST_DB_NAME: "" }, "TEST_DB_NAME", 1, /must name a disposable test database/u],
40
41
  [{ TEST_DB_NAME: "catalogue-test" }, "TEST_DB_NAME", 1, /must name a disposable test database/u],
41
- [{ TEST_DB_NAME: "Catalogue" }, "TEST_DB_NAME", 1, /DB_NAME, DATABASE_URL and PROD_DB_NAME \(catalogue\)/u],
42
+ [{ TEST_DB_NAME: "Catalogue" }, "TEST_DB_NAME", 1, /DB_NAME, DATABASE_URL, STAGING_DB_NAME and PRODUCTION_DB_NAME \(catalogue\)/u],
42
43
  [{ DB_NAME: "", DATABASE_URL: "mysql://u:p@db/catalogue_test" }, "TEST_DB_NAME", 1, /DATABASE_URL/u],
43
- [{ PROD_DB_NAME: "catalogue_test_2" }, "TEST_DB_NAME", 3, /PROD_DB_NAME \(catalogue_test_2\)/u],
44
+ [{ PRODUCTION_DB_NAME: "catalogue_test_2" }, "TEST_DB_NAME", 3, /PRODUCTION_DB_NAME \(catalogue_test_2\)/u],
45
+ [{ STAGING_DB_NAME: "catalogue_test_1" }, "TEST_DB_NAME", 2, /STAGING_DB_NAME and PRODUCTION_DB_NAME \(catalogue_test_1\)/u],
44
46
  [{ BROWSER_TEST_DB_NAME: "catalogue_test" }, "TEST_DB_NAME", 1, /the other test database/u],
45
47
  [{ BROWSER_TEST_DB_NAME: "catalogue_test_1" }, "TEST_DB_NAME", 2, /the other test database/u],
46
48
  [{ TEST_DB_NAME: "catalogue_browser_2" }, "BROWSER_TEST_DB_NAME", 1, /the other test database/u],
@@ -53,7 +55,7 @@ test("a test database and its copies are never the application's database, stagi
53
55
  }
54
56
  // The database the Knex configuration connects to, whatever the environment says.
55
57
  assert.throws(() => requireDisposableDatabase({ ...environment, DB_NAME: "" }, "TEST_DB_NAME", 2, { connected: "Catalogue_Test_1" }),
56
- /PROD_DB_NAME \(catalogue_test_1\)/u);
58
+ /PRODUCTION_DB_NAME \(catalogue_test_1\)/u);
57
59
  });
58
60
 
59
61
  test("package data tests on PostgreSQL never run in staging or production, before any connection", async (context) => {
@@ -126,10 +128,32 @@ test("with no usable test database, a run's test files run one at a time on the
126
128
 
127
129
  test("a test database copy named like a protected database fails the run instead of running on it", async () => {
128
130
  const knexConfig = createKnexMigrationConfig({ client: "mysql2", environment: connectionEnvironment, appRoot: "/srv/catalogue" });
129
- const environment = { ...connectionEnvironment, TEST_DB_NAME: "catalogue_test", PROD_DB_NAME: "catalogue_test_1" };
130
- await assert.rejects(testDatabaseRun({ knexConfig, environment, count: 4 }), /PROD_DB_NAME \(catalogue_test_1\)/u);
131
+ const environment = { ...connectionEnvironment, TEST_DB_NAME: "catalogue_test", PRODUCTION_DB_NAME: "catalogue_test_1" };
132
+ await assert.rejects(testDatabaseRun({ knexConfig, environment, count: 4 }), /PRODUCTION_DB_NAME \(catalogue_test_1\)/u);
131
133
  });
132
134
 
135
+ test("test databases are never prepared while PROD_DB_NAME or AUTH_LOCAL_FILE_PRODUCTION_ACK is set, naming what replaced it",
136
+ async () => {
137
+ const knexConfig = createKnexMigrationConfig({ client: "mysql2", environment: connectionEnvironment, appRoot: "/srv/catalogue" });
138
+ for (const [retired, message] of [
139
+ [{ PROD_DB_NAME: "catalogue" }, "PROD_DB_NAME is no longer read: name that database in STAGING_DB_NAME (your Staging " +
140
+ "database) or PRODUCTION_DB_NAME (your Production database)."],
141
+ [{ AUTH_LOCAL_FILE_PRODUCTION_ACK: "true" }, "AUTH_LOCAL_FILE_PRODUCTION_ACK is no longer read: set " +
142
+ "AUTH_LOCAL_FILE_STAGING_OR_PRODUCTION_ACK=true instead, which the local file auth backend needs in staging and " +
143
+ "production."]
144
+ ]) {
145
+ const named = { ...connectionEnvironment, TEST_DB_NAME: "catalogue_test", ...retired };
146
+ assert.throws(() => requireDisposableDatabase(named, "TEST_DB_NAME", 2), { message });
147
+ assert.throws(() => requireDisposableDatabase({ ...named, BROWSER_TEST_DB_NAME: "catalogue_browser" },
148
+ "BROWSER_TEST_DB_NAME", 2), { message }, "a browser run's preparation");
149
+ await assert.rejects(prepareTestDatabases({ knexConfig, environment: named, count: 2 }), { message });
150
+ await assert.rejects(testDatabaseRun({ knexConfig, environment: named, count: 2 }), { message });
151
+ // Before a run decides to go one file at a time on the connected database, too.
152
+ await assert.rejects(testDatabaseRun({ knexConfig, environment: { ...connectionEnvironment, ...retired }, count: 2 }),
153
+ { message });
154
+ }
155
+ });
156
+
133
157
  test("countQueries() records each statement a piece of work sends, so a read per row stands out from a batched read", async () => {
134
158
  const knex = knexFactory({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
135
159
  try {