@jskit-ai/database-runtime 0.1.275 → 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 +2 -0
- package/package.json +2 -2
- package/src/server/testDatabases.js +27 -12
- package/test/testDatabases.test.js +45 -8
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
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.
|
|
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.
|
|
5
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.
|
|
6
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.
|
|
7
9
|
- `./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`).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/database-runtime",
|
|
3
|
-
"version": "0.1.
|
|
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.
|
|
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
|
-
//
|
|
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,6 +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";
|
|
34
|
+
import { assertNoRetiredVariables } from "@jskit-ai/kernel/server/runtime/appEnvironment";
|
|
35
|
+
import { appEnvironment, isStagingOrProduction } from "@jskit-ai/kernel/shared/support/appEnvironment";
|
|
32
36
|
import { parseDatabaseUrl, resolveKnexConnectionFromEnvironment, resolveKnexPool } from "../shared/databaseConnection.js";
|
|
33
37
|
import { detectDialectFromClient } from "../shared/dialect.js";
|
|
34
38
|
import { RUNTIME_QUERY_CHANNEL } from "./database.js";
|
|
@@ -38,6 +42,8 @@ const SNAPSHOT_DIRECTORY = path.join("node_modules", ".cache", "jskit-test-datab
|
|
|
38
42
|
// Room for a `_snapshot` or copy suffix within MySQL's 64 and PostgreSQL's 63 characters.
|
|
39
43
|
const DISPOSABLE_NAME = /^[A-Za-z0-9_]{1,48}$/u;
|
|
40
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"]);
|
|
41
47
|
const MYSQL_ACCESS_DENIED = new Set(["ER_DBACCESS_DENIED_ERROR", "ER_ACCESS_DENIED_ERROR", "ER_SPECIFIC_ACCESS_DENIED_ERROR"]);
|
|
42
48
|
// A database or table the user may not use (not a wrong password, which fails the run).
|
|
43
49
|
const MYSQL_REFUSED_PRIVILEGE = new Set(["ER_DBACCESS_DENIED_ERROR", "ER_SPECIFIC_ACCESS_DENIED_ERROR", "ER_TABLEACCESS_DENIED_ERROR"]);
|
|
@@ -91,24 +97,31 @@ function connectedDatabaseName(environment) {
|
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
/**
|
|
94
|
-
* The disposable database `variable` names, checked with its first `count - 1` copies: never
|
|
95
|
-
*
|
|
96
|
-
*
|
|
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.
|
|
97
105
|
* @param {Record<string, string | undefined>} environment
|
|
98
106
|
* @param {"TEST_DB_NAME" | "BROWSER_TEST_DB_NAME"} variable
|
|
99
107
|
* @param {number} [count]
|
|
100
108
|
* @param {{ connected?: string }} [options]
|
|
101
109
|
*/
|
|
102
110
|
function requireDisposableDatabase(environment, variable, count = 1, { connected = "" } = {}) {
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
assertNoRetiredVariables(environment);
|
|
112
|
+
if (isStagingOrProduction(environment)) {
|
|
113
|
+
throw new Error(`Test databases are never prepared when APP_ENV is ${appEnvironment(environment)}.`);
|
|
105
114
|
}
|
|
106
115
|
const name = String(environment[variable] || "").trim();
|
|
107
116
|
if (!DISPOSABLE_NAME.test(name)) {
|
|
108
117
|
throw new Error(`${variable} must name a disposable test database: letters, digits and _, at most 48 characters.`);
|
|
109
118
|
}
|
|
110
|
-
const application = [
|
|
111
|
-
|
|
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);
|
|
112
125
|
const others = TEST_DATABASE_VARIABLES.filter((other) => other !== variable)
|
|
113
126
|
.map((other) => folded(environment[other])).filter(Boolean);
|
|
114
127
|
// `copy` is `original` or one of its numbered copies, whatever the count of either run.
|
|
@@ -116,7 +129,8 @@ function requireDisposableDatabase(environment, variable, count = 1, { connected
|
|
|
116
129
|
(copy.startsWith(`${original}_`) && /^\d+$/u.test(copy.slice(original.length + 1)));
|
|
117
130
|
for (const candidate of testDatabaseNames(name, count).map(folded)) {
|
|
118
131
|
if (application.includes(candidate)) {
|
|
119
|
-
throw new Error(`${variable} and its copies must differ from the application's DB_NAME, DATABASE_URL
|
|
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}).`);
|
|
120
134
|
}
|
|
121
135
|
}
|
|
122
136
|
for (const other of others) {
|
|
@@ -487,6 +501,7 @@ function refusedPrivilege(error) {
|
|
|
487
501
|
* before any test file: what it answers joins the summary, and a throw fails the run.
|
|
488
502
|
*/
|
|
489
503
|
async function testDatabaseRun({ knexConfig, environment, count, schema, appRoot = process.cwd(), checkDatabase }) {
|
|
504
|
+
assertNoRetiredVariables(environment);
|
|
490
505
|
const oneAtATime = (reason) => ({
|
|
491
506
|
concurrency: 1,
|
|
492
507
|
env: {},
|
|
@@ -634,8 +649,8 @@ async function createPackageTestKnex(context, { environment }) {
|
|
|
634
649
|
context.after(() => knex.destroy());
|
|
635
650
|
return knex;
|
|
636
651
|
}
|
|
637
|
-
if (environment
|
|
638
|
-
throw new Error(
|
|
652
|
+
if (isStagingOrProduction(environment)) {
|
|
653
|
+
throw new Error(`Package data tests never run when APP_ENV is ${appEnvironment(environment)}.`);
|
|
639
654
|
}
|
|
640
655
|
const connection = {
|
|
641
656
|
...resolveKnexConnectionFromEnvironment(environment, { client: "pg", defaultPort: 5432 }),
|
|
@@ -7,7 +7,9 @@ import knexFactory from "knex";
|
|
|
7
7
|
import { createKnexMigrationConfig } from "../src/server/knexMigrationConfig.js";
|
|
8
8
|
import {
|
|
9
9
|
TestDatabaseMissingError,
|
|
10
|
+
createPackageTestKnex,
|
|
10
11
|
countQueries,
|
|
12
|
+
prepareTestDatabases,
|
|
11
13
|
requireDisposableDatabase,
|
|
12
14
|
requireTestDatabase,
|
|
13
15
|
tableNames,
|
|
@@ -17,6 +19,7 @@ import {
|
|
|
17
19
|
} from "../src/server/testDatabases.js";
|
|
18
20
|
|
|
19
21
|
const connectionEnvironment = Object.freeze({
|
|
22
|
+
APP_ENV: "test",
|
|
20
23
|
DB_HOST: "127.0.0.1",
|
|
21
24
|
DB_NAME: "catalogue",
|
|
22
25
|
DB_USER: "catalogue_user",
|
|
@@ -29,27 +32,39 @@ test("a run's test databases are the designated one, then its numbered copies",
|
|
|
29
32
|
assert.deepEqual(testDatabaseNames("catalogue_test", 3), ["catalogue_test", "catalogue_test_1", "catalogue_test_2"]);
|
|
30
33
|
});
|
|
31
34
|
|
|
32
|
-
test("a test database and its copies are never the application's database, production or the other test database", () => {
|
|
35
|
+
test("a test database and its copies are never the application's database, staging, production or the other test database", () => {
|
|
33
36
|
const environment = { ...connectionEnvironment, TEST_DB_NAME: "catalogue_test", BROWSER_TEST_DB_NAME: "catalogue_browser" };
|
|
34
37
|
assert.equal(requireDisposableDatabase(environment, "TEST_DB_NAME", 4), "catalogue_test");
|
|
35
38
|
assert.equal(requireDisposableDatabase(environment, "BROWSER_TEST_DB_NAME", 4), "catalogue_browser");
|
|
36
39
|
for (const [overrides, variable, count, message] of [
|
|
37
40
|
[{ TEST_DB_NAME: "" }, "TEST_DB_NAME", 1, /must name a disposable test database/u],
|
|
38
41
|
[{ TEST_DB_NAME: "catalogue-test" }, "TEST_DB_NAME", 1, /must name a disposable test database/u],
|
|
39
|
-
[{ TEST_DB_NAME: "Catalogue" }, "TEST_DB_NAME", 1, /DB_NAME, DATABASE_URL and
|
|
42
|
+
[{ TEST_DB_NAME: "Catalogue" }, "TEST_DB_NAME", 1, /DB_NAME, DATABASE_URL, STAGING_DB_NAME and PRODUCTION_DB_NAME \(catalogue\)/u],
|
|
40
43
|
[{ DB_NAME: "", DATABASE_URL: "mysql://u:p@db/catalogue_test" }, "TEST_DB_NAME", 1, /DATABASE_URL/u],
|
|
41
|
-
[{
|
|
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],
|
|
42
46
|
[{ BROWSER_TEST_DB_NAME: "catalogue_test" }, "TEST_DB_NAME", 1, /the other test database/u],
|
|
43
47
|
[{ BROWSER_TEST_DB_NAME: "catalogue_test_1" }, "TEST_DB_NAME", 2, /the other test database/u],
|
|
44
48
|
[{ TEST_DB_NAME: "catalogue_browser_2" }, "BROWSER_TEST_DB_NAME", 1, /the other test database/u],
|
|
45
|
-
[{ NODE_ENV: "production" }, "TEST_DB_NAME", 1,
|
|
49
|
+
[{ APP_ENV: "staging", NODE_ENV: "production" }, "TEST_DB_NAME", 1, /^Test databases are never prepared when APP_ENV is staging\.$/u],
|
|
50
|
+
[{ APP_ENV: "production", NODE_ENV: "production" }, "TEST_DB_NAME", 1, /when APP_ENV is production/u],
|
|
51
|
+
[{ APP_ENV: "" }, "TEST_DB_NAME", 1, /^APP_ENV is not set/u]
|
|
46
52
|
]) {
|
|
47
|
-
assert.throws(() => requireDisposableDatabase({ ...environment, ...overrides }, variable, count), message,
|
|
53
|
+
assert.throws(() => requireDisposableDatabase({ ...environment, ...overrides }, variable, count), { message },
|
|
48
54
|
JSON.stringify(overrides));
|
|
49
55
|
}
|
|
50
56
|
// The database the Knex configuration connects to, whatever the environment says.
|
|
51
57
|
assert.throws(() => requireDisposableDatabase({ ...environment, DB_NAME: "" }, "TEST_DB_NAME", 2, { connected: "Catalogue_Test_1" }),
|
|
52
|
-
/
|
|
58
|
+
/PRODUCTION_DB_NAME \(catalogue_test_1\)/u);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("package data tests on PostgreSQL never run in staging or production, before any connection", async (context) => {
|
|
62
|
+
for (const APP_ENV of ["staging", "production"]) {
|
|
63
|
+
await assert.rejects(
|
|
64
|
+
createPackageTestKnex(context, { environment: { ...connectionEnvironment, DB_CLIENT: "pg", APP_ENV, NODE_ENV: "production" } }),
|
|
65
|
+
{ message: `Package data tests never run when APP_ENV is ${APP_ENV}.` }
|
|
66
|
+
);
|
|
67
|
+
}
|
|
53
68
|
});
|
|
54
69
|
|
|
55
70
|
test("the snapshot key changes when a migration changes, is added or is renamed, and only then", async (context) => {
|
|
@@ -113,10 +128,32 @@ test("with no usable test database, a run's test files run one at a time on the
|
|
|
113
128
|
|
|
114
129
|
test("a test database copy named like a protected database fails the run instead of running on it", async () => {
|
|
115
130
|
const knexConfig = createKnexMigrationConfig({ client: "mysql2", environment: connectionEnvironment, appRoot: "/srv/catalogue" });
|
|
116
|
-
const environment = { ...connectionEnvironment, TEST_DB_NAME: "catalogue_test",
|
|
117
|
-
await assert.rejects(testDatabaseRun({ knexConfig, environment, count: 4 }), /
|
|
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);
|
|
118
133
|
});
|
|
119
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
|
+
|
|
120
157
|
test("countQueries() records each statement a piece of work sends, so a read per row stands out from a batched read", async () => {
|
|
121
158
|
const knex = knexFactory({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
122
159
|
try {
|