@jskit-ai/database-runtime 0.1.262 → 0.1.264
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 +16 -0
- package/package.json +6 -7
- package/src/server/database.js +7 -7
- package/src/server/databaseSetup.js +3 -3
- package/src/server/foreignKeyMigrations.js +64 -1
- package/src/server/knexMigrationConfig.js +7 -2
- package/src/server/testDatabases.js +109 -7
- package/src/server/types.js +1 -4
- package/src/shared/databaseConnection.js +20 -1
- package/src/shared/duplicateEntry.js +10 -1
- package/src/shared/index.js +1 -9
- package/src/shared/repositoryOptions.js +41 -119
- package/test/databaseConnection.postgres.test.js +66 -0
- package/test/databaseConnection.test.js +26 -1
- package/test/duplicateEntry.test.js +16 -0
- package/test/entrypoints.boundary.test.js +18 -2
- package/test/foreignKeyMigrations.postgres.test.js +22 -1
- package/test/foreignKeyMigrations.test.js +25 -1
- package/test/knexMigrationConfig.test.js +39 -8
- package/test/providerRuntime.test.js +2 -2
- package/test/repositoryOptions.test.js +81 -76
- package/test/testDatabases.test.js +37 -0
- package/src/shared/transactionManager.js +0 -31
- package/src/shared/visibility.js +0 -73
- package/test/runtimeCore.test.js +0 -51
- package/test/visibility.test.js +0 -93
|
@@ -1,43 +1,5 @@
|
|
|
1
1
|
import { normalizeCanonicalRecordIdText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The transaction a repository or service call runs in: its options' `trx`,
|
|
5
|
-
* or null for a call outside one (a call may pass no options).
|
|
6
|
-
* @param {{ trx?: import("../server/types.js").RuntimeKnexTransaction | null } | null} [options]
|
|
7
|
-
* @returns {import("../server/types.js").RuntimeKnexTransaction | null}
|
|
8
|
-
*/
|
|
9
|
-
function resolveTransaction(options) {
|
|
10
|
-
if (!options || typeof options !== "object") {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
return options.trx || null;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* A query's transaction (see resolveTransaction) and whether it locks the rows it reads.
|
|
18
|
-
* @param {{ trx?: import("../server/types.js").RuntimeKnexTransaction | null, forUpdate?: boolean } | null} [options]
|
|
19
|
-
*/
|
|
20
|
-
function resolveQueryOptions(options) {
|
|
21
|
-
return {
|
|
22
|
-
trx: resolveTransaction(options),
|
|
23
|
-
forUpdate: options?.forUpdate === true
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function resolveRepoClient(dbClient, options = {}) {
|
|
28
|
-
const { trx } = resolveQueryOptions(options);
|
|
29
|
-
return trx || dbClient;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function applyForUpdate(query, options = {}) {
|
|
33
|
-
const { forUpdate } = resolveQueryOptions(options);
|
|
34
|
-
// readable-check: allow method-probe - platform object: a Knex query builder that cannot lock rows is used as is
|
|
35
|
-
if (forUpdate && typeof query?.forUpdate === "function") {
|
|
36
|
-
return query.forUpdate();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return query;
|
|
40
|
-
}
|
|
2
|
+
import { isLostDuplicateInsertRace } from "./duplicateEntry.js";
|
|
41
3
|
|
|
42
4
|
function parseJsonObject(value, fallback = {}) {
|
|
43
5
|
const source = String(value || "").trim();
|
|
@@ -164,8 +126,14 @@ function toDbJson(value) {
|
|
|
164
126
|
|
|
165
127
|
/**
|
|
166
128
|
* True when the error reports a transaction the database aborted, and rolled
|
|
167
|
-
* back in full, as a deadlock victim
|
|
168
|
-
*
|
|
129
|
+
* back in full, as a deadlock victim, a SQLite write that found the database
|
|
130
|
+
* locked by another connection (SQLITE_BUSY), or, with MariaDB's
|
|
131
|
+
* innodb_snapshot_isolation on, a locking read of a row another transaction
|
|
132
|
+
* changed after this one's snapshot ("Record has changed since last read",
|
|
133
|
+
* errno 1020, ER_CHECKREAD); after the last two json-rest-api rolls its
|
|
134
|
+
* transaction back, so a new run starts from a fresh snapshot. A wrapper that
|
|
135
|
+
* reports its transaction committed (json-rest-api's transactionOutcome)
|
|
136
|
+
* never is one.
|
|
169
137
|
* @param {unknown} error
|
|
170
138
|
*/
|
|
171
139
|
function isDeadlockError(error) {
|
|
@@ -176,7 +144,8 @@ function isDeadlockError(error) {
|
|
|
176
144
|
return false;
|
|
177
145
|
}
|
|
178
146
|
const code = String(error.code || "").toUpperCase();
|
|
179
|
-
if (code === "ER_LOCK_DEADLOCK" || code === "40P01" ||
|
|
147
|
+
if (code === "ER_LOCK_DEADLOCK" || code === "ER_CHECKREAD" || code === "40P01" || code === "SQLITE_BUSY" ||
|
|
148
|
+
Number(error.errno) === 1213 || Number(error.errno) === 1020) {
|
|
180
149
|
return true;
|
|
181
150
|
}
|
|
182
151
|
error = error.cause;
|
|
@@ -187,6 +156,8 @@ function isDeadlockError(error) {
|
|
|
187
156
|
/**
|
|
188
157
|
* Runs a whole transaction again when the database chose it as a deadlock
|
|
189
158
|
* victim, so `run` must be safe to repeat: no external effects inside it.
|
|
159
|
+
* A lock wait that timed out is never run again: MySQL rolls back only the
|
|
160
|
+
* timed-out statement, so the transaction's earlier work could repeat.
|
|
190
161
|
* @template T
|
|
191
162
|
* @param {() => Promise<T>} run
|
|
192
163
|
* @param {{ retries?: number }} [options]
|
|
@@ -205,88 +176,42 @@ async function retryDeadlocks(run, { retries = 3 } = {}) {
|
|
|
205
176
|
}
|
|
206
177
|
}
|
|
207
178
|
|
|
208
|
-
async function runInTransaction(knex, callback, options = {}) {
|
|
209
|
-
if (typeof callback !== "function") {
|
|
210
|
-
throw new TypeError("runInTransaction requires callback.");
|
|
211
|
-
}
|
|
212
|
-
if (!knex || typeof knex !== "function") {
|
|
213
|
-
throw new TypeError("runInTransaction requires knex client.");
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// readable-check: allow method-probe - platform object: a Knex client without transaction() runs the callback on itself
|
|
217
|
-
if (typeof knex.transaction !== "function") {
|
|
218
|
-
return callback(knex);
|
|
219
|
-
}
|
|
220
|
-
// Inside a transaction this opens a savepoint; a deadlock aborts the
|
|
221
|
-
// outermost transaction, and only that one retries.
|
|
222
|
-
if (knex.isTransaction) {
|
|
223
|
-
return knex.transaction(callback);
|
|
224
|
-
}
|
|
225
|
-
return retryDeadlocks(() => knex.transaction(callback), options);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function createWithTransaction(knex) {
|
|
229
|
-
return function withTransaction(work) {
|
|
230
|
-
return runInTransaction(knex, work);
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
179
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
180
|
+
* Runs `run` once more when its insert lost a race for a unique key: another
|
|
181
|
+
* request inserted the same key first (an idempotency or request key, a
|
|
182
|
+
* revision number, a computed id), and `run`'s own transaction rolled back in
|
|
183
|
+
* full (isLostDuplicateInsertRace). The database reports the duplicate only
|
|
184
|
+
* once the winner has committed, so the second run reads a snapshot that holds
|
|
185
|
+
* the winner's row: its lookup by the key finds it, and every other check sees
|
|
186
|
+
* the current state. A second duplicate is final. `run` is a transaction
|
|
187
|
+
* opened with these `options`, usually `() => repository.withTransaction(options,
|
|
188
|
+
* work)`, and must be safe to repeat, as for a deadlock retry. Inside a
|
|
189
|
+
* caller's transaction (`options.trx`) the failed insert has failed that
|
|
190
|
+
* transaction, so `run` never runs again there: the error reaches the caller,
|
|
191
|
+
* whose own retry, if it has one, runs the whole of it again.
|
|
238
192
|
* @template T
|
|
239
|
-
* @param {{ trx?:
|
|
240
|
-
* @param {(
|
|
241
|
-
* The repository's withTransaction: opens a transaction and runs work in it.
|
|
242
|
-
* @param {(trx: import("../server/types.js").RuntimeKnexTransaction) => Promise<T>} work
|
|
193
|
+
* @param {{ trx?: unknown }} options The options `run` opens its transaction with.
|
|
194
|
+
* @param {() => Promise<T>} run
|
|
243
195
|
* @returns {Promise<T>}
|
|
244
196
|
*/
|
|
245
|
-
function
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(identifier)) {
|
|
252
|
-
throw new TypeError(`${label} must be a safe SQL identifier.`);
|
|
253
|
-
}
|
|
254
|
-
return identifier;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async function lockScopedRecordId({
|
|
258
|
-
trx,
|
|
259
|
-
tableName,
|
|
260
|
-
recordId,
|
|
261
|
-
scopeId,
|
|
262
|
-
idColumn = "id",
|
|
263
|
-
scopeColumn = "workspace_id"
|
|
264
|
-
} = {}) {
|
|
265
|
-
if (typeof trx !== "function") {
|
|
266
|
-
throw new TypeError("lockScopedRecordId requires a transaction.");
|
|
197
|
+
async function retryLostInsertRace(options, run, ...rest) {
|
|
198
|
+
if (!options || typeof options !== "object" || typeof run !== "function" || rest.length > 0) {
|
|
199
|
+
throw new TypeError(
|
|
200
|
+
"retryLostInsertRace() takes (options, run): the options run opens its transaction with, then " +
|
|
201
|
+
"run, such as () => repository.withTransaction(options, work)."
|
|
202
|
+
);
|
|
267
203
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
return
|
|
204
|
+
try {
|
|
205
|
+
return await run();
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (!isLostDuplicateInsertRace(error, options)) {
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
return run();
|
|
275
211
|
}
|
|
276
|
-
const row = await trx(table)
|
|
277
|
-
.select(recordKey)
|
|
278
|
-
.where(recordKey, normalizedRecordId)
|
|
279
|
-
.andWhere(scopeKey, normalizedScopeId)
|
|
280
|
-
.forUpdate()
|
|
281
|
-
.first();
|
|
282
|
-
return normalizeDbRecordId(row?.[recordKey]);
|
|
283
212
|
}
|
|
284
213
|
|
|
285
214
|
export {
|
|
286
|
-
resolveTransaction,
|
|
287
|
-
resolveQueryOptions,
|
|
288
|
-
resolveRepoClient,
|
|
289
|
-
applyForUpdate,
|
|
290
215
|
parseJsonObject,
|
|
291
216
|
stringifyJsonObject,
|
|
292
217
|
normalizeNullableString,
|
|
@@ -296,8 +221,5 @@ export {
|
|
|
296
221
|
toDbJson,
|
|
297
222
|
isDeadlockError,
|
|
298
223
|
retryDeadlocks,
|
|
299
|
-
|
|
300
|
-
createWithTransaction,
|
|
301
|
-
joinOrOpenTransaction,
|
|
302
|
-
lockScopedRecordId
|
|
224
|
+
retryLostInsertRace
|
|
303
225
|
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import knexFactory from "knex";
|
|
4
|
+
import { createDatabase } from "../src/server/database.js";
|
|
5
|
+
import { resolveKnexConnectionFromEnvironment } from "../src/shared/databaseConnection.js";
|
|
6
|
+
import { toDatabaseDateTimeUtc } from "../src/shared/dateUtils.js";
|
|
7
|
+
|
|
8
|
+
// Runs beside the PostgreSQL test database: run with DB_NAME set to TEST_DB_NAME (and the usual DB_HOST, DB_PORT,
|
|
9
|
+
// DB_USER and DB_PASSWORD), and DB_CLIENT unset or pg.
|
|
10
|
+
const TEST_DB_NAME = process.env.TEST_DB_NAME?.trim();
|
|
11
|
+
const ON_TEST_DATABASE = Boolean(TEST_DB_NAME) && process.env.DB_NAME === TEST_DB_NAME;
|
|
12
|
+
const POSTGRES_CLIENT = !process.env.DB_CLIENT || process.env.DB_CLIENT === "pg";
|
|
13
|
+
const SKIP = ON_TEST_DATABASE && POSTGRES_CLIENT ? false : "not on a PostgreSQL test database (DB_NAME=$TEST_DB_NAME)";
|
|
14
|
+
|
|
15
|
+
// The default time zone of the server in this test. node-postgres sends PGOPTIONS as each new connection's startup
|
|
16
|
+
// options, so every session starts in it, as on a server configured for that zone.
|
|
17
|
+
const SERVER_TIME_ZONE = "Australia/Perth";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Writes one date-time as the "datetime-utc" storage writes it (UTC, without an offset) into a timestamptz column,
|
|
21
|
+
* and reads it back, on one connection.
|
|
22
|
+
* @param {import("knex").Knex} knex
|
|
23
|
+
* @param {Date} instant
|
|
24
|
+
*/
|
|
25
|
+
function roundTrip(knex, instant) {
|
|
26
|
+
return knex.transaction(async (trx) => {
|
|
27
|
+
await trx.raw("CREATE TEMPORARY TABLE stamped (at timestamptz NOT NULL) ON COMMIT DROP");
|
|
28
|
+
await trx.raw("INSERT INTO stamped (at) VALUES (?)", [toDatabaseDateTimeUtc(instant)]);
|
|
29
|
+
const { rows: [{ TimeZone: session }] } = await trx.raw("SHOW TIME ZONE");
|
|
30
|
+
const { rows: [{ at, text }] } = await trx.raw("SELECT at, at::text AS text FROM stamped");
|
|
31
|
+
return { session, at, text };
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test("the runtime's PostgreSQL sessions run in UTC on a server that is not, so a timestamptz keeps the instant written", {
|
|
36
|
+
skip: SKIP
|
|
37
|
+
}, async (context) => {
|
|
38
|
+
const previousOptions = process.env.PGOPTIONS;
|
|
39
|
+
process.env.PGOPTIONS = `-c TimeZone=${SERVER_TIME_ZONE}`;
|
|
40
|
+
context.after(() => {
|
|
41
|
+
if (previousOptions === undefined) {
|
|
42
|
+
delete process.env.PGOPTIONS;
|
|
43
|
+
} else {
|
|
44
|
+
process.env.PGOPTIONS = previousOptions;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
const env = { ...process.env, DB_CLIENT: "pg" };
|
|
48
|
+
const serverDefault = knexFactory({
|
|
49
|
+
client: "pg",
|
|
50
|
+
connection: resolveKnexConnectionFromEnvironment(env, { client: "pg", defaultPort: 5432 }),
|
|
51
|
+
pool: { min: 0, max: 1 }
|
|
52
|
+
});
|
|
53
|
+
const { knex } = createDatabase({ driver: { DIALECT_ID: "pg" }, env });
|
|
54
|
+
context.after(() => Promise.all([serverDefault.destroy(), knex.destroy()]));
|
|
55
|
+
const instant = new Date("2026-11-08T04:05:06.789Z");
|
|
56
|
+
|
|
57
|
+
// Without the runtime's session setting, the server reads the UTC text in its own zone: eight hours early.
|
|
58
|
+
const shifted = await roundTrip(serverDefault, instant);
|
|
59
|
+
assert.equal(shifted.session, SERVER_TIME_ZONE);
|
|
60
|
+
assert.equal(instant.getTime() - shifted.at.getTime(), 8 * 60 * 60 * 1000);
|
|
61
|
+
|
|
62
|
+
const kept = await roundTrip(knex, instant);
|
|
63
|
+
assert.equal(kept.session, "UTC");
|
|
64
|
+
assert.equal(kept.at.toISOString(), instant.toISOString());
|
|
65
|
+
assert.equal(kept.text, "2026-11-08 04:05:06.789+00");
|
|
66
|
+
});
|
|
@@ -4,7 +4,8 @@ import {
|
|
|
4
4
|
parseDatabaseUrl,
|
|
5
5
|
resolveDatabaseClientFromEnvironment,
|
|
6
6
|
resolveDatabaseConnectionFromEnvironment,
|
|
7
|
-
resolveKnexConnectionFromEnvironment
|
|
7
|
+
resolveKnexConnectionFromEnvironment,
|
|
8
|
+
resolveKnexPool
|
|
8
9
|
} from "../src/shared/databaseConnection.js";
|
|
9
10
|
|
|
10
11
|
test("parseDatabaseUrl parses mysql url fields", () => {
|
|
@@ -103,3 +104,27 @@ test("resolveKnexConnectionFromEnvironment keeps only MySQL DATE columns as stri
|
|
|
103
104
|
assert.equal(postgresConnection.dateStrings, undefined);
|
|
104
105
|
assert.equal(postgresConnection.timezone, undefined);
|
|
105
106
|
});
|
|
107
|
+
|
|
108
|
+
test("resolveKnexPool sets each PostgreSQL session to UTC before the pool hands the connection out", async () => {
|
|
109
|
+
assert.equal(resolveKnexPool("mysql2"), undefined);
|
|
110
|
+
const pool = resolveKnexPool("pg");
|
|
111
|
+
const statements = [];
|
|
112
|
+
const connection = {
|
|
113
|
+
query(sql, callback) {
|
|
114
|
+
statements.push(sql);
|
|
115
|
+
callback(null);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const handedOut = await new Promise((resolve, reject) => {
|
|
119
|
+
pool.afterCreate(connection, (error, created) => (error ? reject(error) : resolve(created)));
|
|
120
|
+
});
|
|
121
|
+
assert.equal(handedOut, connection);
|
|
122
|
+
assert.deepEqual(statements, ["SET TIME ZONE 'UTC'"]);
|
|
123
|
+
|
|
124
|
+
// A connection that cannot set its time zone is not handed out.
|
|
125
|
+
const refusal = new Error("permission denied");
|
|
126
|
+
const failed = await new Promise((resolve) => {
|
|
127
|
+
pool.afterCreate({ query: (_sql, callback) => callback(refusal) }, (error) => resolve(error));
|
|
128
|
+
});
|
|
129
|
+
assert.equal(failed, refusal);
|
|
130
|
+
});
|
|
@@ -9,6 +9,22 @@ test("isDuplicateEntryError matches mysql and postgres duplicate signatures", ()
|
|
|
9
9
|
assert.equal(isDuplicateEntryError({ code: "ER_PARSE_ERROR", errno: 1064 }), false);
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
+
test("isDuplicateEntryError matches SQLite's unique and primary key violations, and no other constraint", () => {
|
|
13
|
+
const unique = Object.assign(new Error("UNIQUE constraint failed: users.email"), { code: "SQLITE_CONSTRAINT_UNIQUE" });
|
|
14
|
+
const primaryKey = Object.assign(new Error("UNIQUE constraint failed: users.id"), { code: "SQLITE_CONSTRAINT_PRIMARYKEY" });
|
|
15
|
+
assert.equal(isDuplicateEntryError(unique), true);
|
|
16
|
+
assert.equal(isDuplicateEntryError(primaryKey), true);
|
|
17
|
+
assert.equal(isDuplicateEntryError(new Error("Write failed", { cause: unique })), true);
|
|
18
|
+
assert.equal(isDuplicateEntryError(unique, { dialect: "sqlite" }), true);
|
|
19
|
+
assert.equal(isDuplicateEntryError(unique, { client: { client: { config: { client: "better-sqlite3" } } } }), true);
|
|
20
|
+
assert.equal(isDuplicateEntryError(unique, { dialect: "mysql" }), false);
|
|
21
|
+
assert.equal(isDuplicateEntryError({ code: "ER_DUP_ENTRY" }, { dialect: "sqlite" }), false);
|
|
22
|
+
for (const code of ["SQLITE_CONSTRAINT_NOTNULL", "SQLITE_CONSTRAINT_FOREIGNKEY", "SQLITE_CONSTRAINT_CHECK", "SQLITE_BUSY"]) {
|
|
23
|
+
assert.equal(isDuplicateEntryError({ code }), false, code);
|
|
24
|
+
}
|
|
25
|
+
assert.equal(isLostDuplicateInsertRace({ transactionOutcome: "rolledBack", cause: unique }, {}), true);
|
|
26
|
+
});
|
|
27
|
+
|
|
12
28
|
test("duplicate classification follows causes and retains the database error", () => {
|
|
13
29
|
const databaseError = Object.freeze({ code: "23505", message: "users_email_unique" });
|
|
14
30
|
const failure = new Error("Write failed", { cause: new Error("Storage failed", { cause: databaseError }) });
|
|
@@ -26,13 +26,29 @@ test("server provider module exports one declarative provider", () => {
|
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
test("shared entrypoint exports shared database utilities only", () => {
|
|
29
|
-
assert.equal(typeof sharedApi.createWithTransaction, "function");
|
|
30
29
|
assert.equal(typeof sharedApi.isDuplicateEntryError, "function");
|
|
31
30
|
assert.equal(typeof sharedApi.isDeadlockError, "function");
|
|
32
31
|
assert.equal(typeof sharedApi.retryDeadlocks, "function");
|
|
32
|
+
assert.equal(typeof sharedApi.retryLostInsertRace, "function");
|
|
33
33
|
assert.equal(typeof sharedApi.toDatabaseDateTimeUtc, "function");
|
|
34
34
|
assert.equal(typeof sharedApi.DatabaseProvider, "undefined");
|
|
35
|
-
for (const removed of [
|
|
35
|
+
for (const removed of [
|
|
36
|
+
"normalizeText",
|
|
37
|
+
"retentionTestables",
|
|
38
|
+
"createTransactionManager",
|
|
39
|
+
"createRepositoryScope",
|
|
40
|
+
"BaseRepository",
|
|
41
|
+
"runInTransaction",
|
|
42
|
+
"resolveTransaction",
|
|
43
|
+
"resolveQueryOptions",
|
|
44
|
+
"resolveRepoClient",
|
|
45
|
+
"applyForUpdate",
|
|
46
|
+
"createWithTransaction",
|
|
47
|
+
"joinOrOpenTransaction",
|
|
48
|
+
"lockScopedRecordId",
|
|
49
|
+
"applyVisibility",
|
|
50
|
+
"applyVisibilityOwners"
|
|
51
|
+
]) {
|
|
36
52
|
assert.equal(typeof sharedApi[removed], "undefined", removed);
|
|
37
53
|
}
|
|
38
54
|
});
|
|
@@ -4,7 +4,8 @@ import knexFactory from "knex";
|
|
|
4
4
|
import {
|
|
5
5
|
addMissingForeignKey,
|
|
6
6
|
dropForeignKeyIfPresent,
|
|
7
|
-
hasForeignKey
|
|
7
|
+
hasForeignKey,
|
|
8
|
+
listForeignKeys
|
|
8
9
|
} from "../src/server/foreignKeyMigrations.js";
|
|
9
10
|
|
|
10
11
|
// Runs in a disposable PostgreSQL schema of the test database: run with DB_NAME set to TEST_DB_NAME
|
|
@@ -90,3 +91,23 @@ test("PostgreSQL foreign key helpers find and drop a key by the name the databas
|
|
|
90
91
|
assert.equal(await keyCount(knex), 0);
|
|
91
92
|
assert.equal(await hasForeignKey(knex, KEY), false);
|
|
92
93
|
});
|
|
94
|
+
|
|
95
|
+
test("listForeignKeys on PostgreSQL answers the schema's keys, each column of a key in order", { skip: SKIP }, async (t) => {
|
|
96
|
+
const knex = await testDatabase(t);
|
|
97
|
+
await knex.raw("ALTER TABLE children ADD CONSTRAINT children_parent_link FOREIGN KEY (parent_id) REFERENCES parents (id)");
|
|
98
|
+
await knex.schema.createTable("pairs", (table) => {
|
|
99
|
+
table.integer("a");
|
|
100
|
+
table.integer("b");
|
|
101
|
+
table.primary(["a", "b"]);
|
|
102
|
+
});
|
|
103
|
+
await knex.schema.createTable("pair_links", (table) => {
|
|
104
|
+
table.integer("pair_b");
|
|
105
|
+
table.integer("pair_a");
|
|
106
|
+
table.foreign(["pair_b", "pair_a"], "pair_links_pair_fk").references(["b", "a"]).inTable("pairs");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
assert.deepEqual(await listForeignKeys(knex), [
|
|
110
|
+
{ name: "children_parent_link", tableName: "children", columns: ["parent_id"], referencedTableName: "parents", referencedColumns: ["id"] },
|
|
111
|
+
{ name: "pair_links_pair_fk", tableName: "pair_links", columns: ["pair_b", "pair_a"], referencedTableName: "pairs", referencedColumns: ["b", "a"] }
|
|
112
|
+
]);
|
|
113
|
+
});
|
|
@@ -4,7 +4,8 @@ import knexFactory from "knex";
|
|
|
4
4
|
import {
|
|
5
5
|
addMissingForeignKey,
|
|
6
6
|
dropForeignKeyIfPresent,
|
|
7
|
-
hasForeignKey
|
|
7
|
+
hasForeignKey,
|
|
8
|
+
listForeignKeys
|
|
8
9
|
} from "../src/server/foreignKeyMigrations.js";
|
|
9
10
|
|
|
10
11
|
const KEY = { tableName: "children", columnName: "parent_id", parentTable: "parents", onDelete: "SET NULL" };
|
|
@@ -46,3 +47,26 @@ test("addMissingForeignKey adds a missing key once, refuses rows without a paren
|
|
|
46
47
|
/children\.parent_id foreign key: the guardians table does not exist/u
|
|
47
48
|
);
|
|
48
49
|
});
|
|
50
|
+
|
|
51
|
+
test("listForeignKeys answers every key of the database, each column of a key in order", async (t) => {
|
|
52
|
+
const knex = knexFactory({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
53
|
+
t.after(() => knex.destroy());
|
|
54
|
+
await knex.schema.createTable("parents", (table) => table.integer("id").primary());
|
|
55
|
+
await knex.schema.createTable("pairs", (table) => {
|
|
56
|
+
table.integer("a");
|
|
57
|
+
table.integer("b");
|
|
58
|
+
table.primary(["a", "b"]);
|
|
59
|
+
});
|
|
60
|
+
await knex.schema.createTable("children", (table) => {
|
|
61
|
+
table.increments("id");
|
|
62
|
+
table.integer("parent_id").references("id").inTable("parents");
|
|
63
|
+
table.integer("pair_b");
|
|
64
|
+
table.integer("pair_a");
|
|
65
|
+
table.foreign(["pair_b", "pair_a"]).references(["b", "a"]).inTable("pairs");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
assert.deepEqual((await listForeignKeys(knex)).sort((left, right) => left.columns[0].localeCompare(right.columns[0])), [
|
|
69
|
+
{ name: "", tableName: "children", columns: ["pair_b", "pair_a"], referencedTableName: "pairs", referencedColumns: ["b", "a"] },
|
|
70
|
+
{ name: "", tableName: "children", columns: ["parent_id"], referencedTableName: "parents", referencedColumns: ["id"] }
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
@@ -26,10 +26,41 @@ test("createKnexMigrationConfig fixes MySQL without requiring DB_CLIENT", () =>
|
|
|
26
26
|
|
|
27
27
|
assert.equal(config.client, "mysql2");
|
|
28
28
|
assert.equal(config.connection.port, 3306);
|
|
29
|
-
assert.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
assert.equal(config.pool, undefined);
|
|
30
|
+
assert.deepEqual(config.migrations.directory, ["/srv/catalogue/migrations"]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("an application without migrations/constraints/ migrates, and its constraints run once the directory exists", async (context) => {
|
|
34
|
+
const appRoot = await mkdtemp(path.join(os.tmpdir(), "jskit-app-constraints-"));
|
|
35
|
+
context.after(() => rm(appRoot, { recursive: true, force: true }));
|
|
36
|
+
await mkdir(path.join(appRoot, "migrations"), { recursive: true });
|
|
37
|
+
await writeFile(path.join(appRoot, "package.json"), JSON.stringify({ dependencies: {} }));
|
|
38
|
+
await writeFile(path.join(appRoot, "migrations", "20260101000000_books.cjs"),
|
|
39
|
+
"exports.up = (knex) => knex.schema.createTable(\"books\", (table) => table.increments(\"id\"));\nexports.down = () => {};\n");
|
|
40
|
+
|
|
41
|
+
const migrate = async () => {
|
|
42
|
+
const { migrations } = await createKnexMigrationConfigFromApp({ client: "pg", environment: connectionEnvironment, appRoot });
|
|
43
|
+
const knex = knexFactory({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true, migrations });
|
|
44
|
+
try {
|
|
45
|
+
await knex.migrate.latest();
|
|
46
|
+
return { directories: migrations.directory, ran: await knex("knex_migrations").orderBy("id").pluck("name") };
|
|
47
|
+
} finally {
|
|
48
|
+
await knex.destroy();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
assert.deepEqual(await migrate(), {
|
|
53
|
+
directories: [path.join(appRoot, "migrations")],
|
|
54
|
+
ran: ["20260101000000_books.cjs"]
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await mkdir(path.join(appRoot, "migrations", "constraints"));
|
|
58
|
+
await writeFile(path.join(appRoot, "migrations", "constraints", "20260101000001_books_index.cjs"),
|
|
59
|
+
"exports.up = (knex) => knex.schema.alterTable(\"books\", (table) => table.index([\"id\"]));\nexports.down = () => {};\n");
|
|
60
|
+
assert.deepEqual(await migrate(), {
|
|
61
|
+
directories: [path.join(appRoot, "migrations"), path.join(appRoot, "migrations", "constraints")],
|
|
62
|
+
ran: ["20260101000000_books.cjs", "20260101000001_books_index.cjs"]
|
|
63
|
+
});
|
|
33
64
|
});
|
|
34
65
|
|
|
35
66
|
test("createKnexMigrationConfig fixes PostgreSQL without requiring DB_CLIENT", () => {
|
|
@@ -41,6 +72,8 @@ test("createKnexMigrationConfig fixes PostgreSQL without requiring DB_CLIENT", (
|
|
|
41
72
|
|
|
42
73
|
assert.equal(config.client, "pg");
|
|
43
74
|
assert.equal(config.connection.port, 5432);
|
|
75
|
+
// Migrations write in a UTC session too.
|
|
76
|
+
assert.equal(typeof config.pool.afterCreate, "function");
|
|
44
77
|
});
|
|
45
78
|
|
|
46
79
|
test("createKnexMigrationConfig requires the application's environment instead of reading DB_* from nothing", () => {
|
|
@@ -83,8 +116,7 @@ test("installed package migrations run from their owning packages without copied
|
|
|
83
116
|
assert.deepEqual(config.migrations.directory, [
|
|
84
117
|
path.join(packageRoot, "migrations"),
|
|
85
118
|
path.join(appRoot, "migrations"),
|
|
86
|
-
path.join(packageRoot, "migrations", "constraints")
|
|
87
|
-
path.join(appRoot, "migrations", "constraints")
|
|
119
|
+
path.join(packageRoot, "migrations", "constraints")
|
|
88
120
|
]);
|
|
89
121
|
});
|
|
90
122
|
|
|
@@ -265,8 +297,7 @@ test("a package's migrations and constraints run after those of the packages its
|
|
|
265
297
|
...packageDirectories,
|
|
266
298
|
path.join(appRoot, "migrations"),
|
|
267
299
|
path.join(directoryOf["@example/units-core:migrations"], "constraints"),
|
|
268
|
-
path.join(directoryOf["@example/assets-core:migrations"], "constraints")
|
|
269
|
-
path.join(appRoot, "migrations", "constraints")
|
|
300
|
+
path.join(directoryOf["@example/assets-core:migrations"], "constraints")
|
|
270
301
|
]);
|
|
271
302
|
});
|
|
272
303
|
|
|
@@ -119,8 +119,8 @@ test("DatabaseProvider exposes one cohesive database capability", async () => {
|
|
|
119
119
|
dateStrings: ["DATE"],
|
|
120
120
|
timezone: "Z"
|
|
121
121
|
});
|
|
122
|
-
|
|
123
|
-
assert.deepEqual(Object.keys(database).sort(), ["driver", "knex"
|
|
122
|
+
// One way to open a transaction: a table's handle. The database runtime offers none of its own.
|
|
123
|
+
assert.deepEqual(Object.keys(database).sort(), ["driver", "knex"]);
|
|
124
124
|
|
|
125
125
|
await runtime.shutdown();
|
|
126
126
|
assert.equal(database.knex.destroyCalls, 1);
|