@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
|
@@ -2,12 +2,11 @@ import test from "node:test";
|
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import {
|
|
4
4
|
isDeadlockError,
|
|
5
|
-
joinOrOpenTransaction,
|
|
6
|
-
lockScopedRecordId,
|
|
7
5
|
normalizeDbRecordId,
|
|
8
6
|
parseJsonValue,
|
|
9
7
|
resolveInsertedRecordId,
|
|
10
|
-
|
|
8
|
+
retryDeadlocks,
|
|
9
|
+
retryLostInsertRace
|
|
11
10
|
} from "../src/shared/repositoryOptions.js";
|
|
12
11
|
|
|
13
12
|
function deadlock() {
|
|
@@ -17,26 +16,6 @@ function deadlock() {
|
|
|
17
16
|
});
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
function fakeKnex({ isTransaction = false } = {}) {
|
|
21
|
-
return Object.assign(() => {}, { isTransaction, transaction: async (callback) => callback("trx") });
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function fakeLockTransaction(row = null) {
|
|
25
|
-
const calls = [];
|
|
26
|
-
const query = {
|
|
27
|
-
select(value) { calls.push(["select", value]); return this; },
|
|
28
|
-
where(key, value) { calls.push(["where", key, value]); return this; },
|
|
29
|
-
andWhere(key, value) { calls.push(["andWhere", key, value]); return this; },
|
|
30
|
-
forUpdate() { calls.push(["forUpdate"]); return this; },
|
|
31
|
-
async first() { calls.push(["first"]); return row; }
|
|
32
|
-
};
|
|
33
|
-
const trx = (table) => {
|
|
34
|
-
calls.push(["table", table]);
|
|
35
|
-
return query;
|
|
36
|
-
};
|
|
37
|
-
return { calls, trx };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
19
|
test("normalizeDbRecordId preserves canonical DB ids and rejects unsafe JS numbers", () => {
|
|
41
20
|
const unsafeNumericId = Number(9007199254740993n);
|
|
42
21
|
assert.equal(normalizeDbRecordId("9007199254740993"), "9007199254740993");
|
|
@@ -62,40 +41,15 @@ test("parseJsonValue parses stored JSON and falls back for empty, null or invali
|
|
|
62
41
|
assert.deepEqual(parseJsonValue(undefined, null, { fallback: [] }), []);
|
|
63
42
|
});
|
|
64
43
|
|
|
65
|
-
test("lockScopedRecordId locks an exact record inside its owner scope", async () => {
|
|
66
|
-
const { calls, trx } = fakeLockTransaction({ id: "42" });
|
|
67
|
-
assert.equal(await lockScopedRecordId({
|
|
68
|
-
trx,
|
|
69
|
-
tableName: "daycare_events",
|
|
70
|
-
recordId: 42,
|
|
71
|
-
scopeId: 7
|
|
72
|
-
}), "42");
|
|
73
|
-
assert.deepEqual(calls, [
|
|
74
|
-
["table", "daycare_events"],
|
|
75
|
-
["select", "id"],
|
|
76
|
-
["where", "id", "42"],
|
|
77
|
-
["andWhere", "workspace_id", "7"],
|
|
78
|
-
["forUpdate"],
|
|
79
|
-
["first"]
|
|
80
|
-
]);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("lockScopedRecordId rejects missing transactions and unsafe identifiers", async () => {
|
|
84
|
-
await assert.rejects(
|
|
85
|
-
() => lockScopedRecordId({ tableName: "records", recordId: 1, scopeId: 1 }),
|
|
86
|
-
/requires a transaction/u
|
|
87
|
-
);
|
|
88
|
-
const { trx } = fakeLockTransaction();
|
|
89
|
-
await assert.rejects(
|
|
90
|
-
() => lockScopedRecordId({ trx, tableName: "records; drop table", recordId: 1, scopeId: 1 }),
|
|
91
|
-
/safe SQL identifier/u
|
|
92
|
-
);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
44
|
test("isDeadlockError recognizes deadlock victims through causes, never a committed write", () => {
|
|
96
45
|
assert.equal(isDeadlockError(deadlock()), true);
|
|
97
46
|
assert.equal(isDeadlockError({ errno: 1213 }), true);
|
|
98
47
|
assert.equal(isDeadlockError({ code: "40P01" }), true);
|
|
48
|
+
assert.equal(isDeadlockError({ code: "SQLITE_BUSY", transactionOutcome: "rolledBack" }), true);
|
|
49
|
+
// MariaDB's snapshot isolation: a locking read of a row changed since the transaction's snapshot.
|
|
50
|
+
assert.equal(isDeadlockError({ code: "ER_CHECKREAD", errno: 1020 }), true);
|
|
51
|
+
assert.equal(isDeadlockError({ errno: 1020 }), true);
|
|
52
|
+
assert.equal(isDeadlockError(new Error("Write failed", { cause: { code: "ER_CHECKREAD", errno: 1020 } })), true);
|
|
99
53
|
assert.equal(isDeadlockError(new Error("Write failed", { cause: deadlock() })), true);
|
|
100
54
|
assert.equal(isDeadlockError({ code: "ER_LOCK_WAIT_TIMEOUT", errno: 1205 }), false);
|
|
101
55
|
assert.equal(isDeadlockError({ code: "ER_DUP_ENTRY", errno: 1062 }), false);
|
|
@@ -107,46 +61,97 @@ test("isDeadlockError recognizes deadlock victims through causes, never a commit
|
|
|
107
61
|
}
|
|
108
62
|
});
|
|
109
63
|
|
|
110
|
-
test("
|
|
64
|
+
test("retryDeadlocks runs again a transaction that read a row changed since its snapshot", async () => {
|
|
111
65
|
let attempts = 0;
|
|
112
|
-
const result = await
|
|
66
|
+
const result = await retryDeadlocks(async () => {
|
|
67
|
+
attempts += 1;
|
|
68
|
+
if (attempts === 1) {
|
|
69
|
+
throw new Error("Write failed", { cause: Object.assign(new Error("Record has changed since last read"), { code: "ER_CHECKREAD", errno: 1020 }) });
|
|
70
|
+
}
|
|
71
|
+
return "clean";
|
|
72
|
+
});
|
|
73
|
+
assert.deepEqual([result, attempts], ["clean", 2]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("retryDeadlocks runs a deadlock victim again and returns the clean run", async () => {
|
|
77
|
+
let attempts = 0;
|
|
78
|
+
const result = await retryDeadlocks(async () => {
|
|
113
79
|
attempts += 1;
|
|
114
80
|
if (attempts < 3) {
|
|
115
81
|
throw deadlock();
|
|
116
82
|
}
|
|
117
|
-
return
|
|
83
|
+
return "clean";
|
|
118
84
|
});
|
|
119
|
-
assert.equal(result, "
|
|
85
|
+
assert.equal(result, "clean");
|
|
120
86
|
assert.equal(attempts, 3);
|
|
121
87
|
});
|
|
122
88
|
|
|
123
|
-
test("
|
|
124
|
-
async function attemptsUntilRejected(
|
|
89
|
+
test("retryDeadlocks runs again only deadlock victims, a few times", async () => {
|
|
90
|
+
async function attemptsUntilRejected(error, options) {
|
|
125
91
|
let attempts = 0;
|
|
126
|
-
await assert.rejects(
|
|
92
|
+
await assert.rejects(retryDeadlocks(async () => {
|
|
127
93
|
attempts += 1;
|
|
128
94
|
throw error;
|
|
129
95
|
}, options), error);
|
|
130
96
|
return attempts;
|
|
131
97
|
}
|
|
132
|
-
|
|
133
|
-
|
|
98
|
+
assert.equal(await attemptsUntilRejected(new Error("Validation failed")), 1);
|
|
99
|
+
// A lock wait that timed out is not a deadlock: the database may have rolled back only the statement, so the
|
|
100
|
+
// transaction's earlier work could run twice. MySQL and MariaDB, then PostgreSQL's lock_timeout and NOWAIT.
|
|
101
|
+
for (const timeout of [
|
|
102
|
+
Object.assign(new Error("Lock wait timeout exceeded"), { code: "ER_LOCK_WAIT_TIMEOUT", errno: 1205 }),
|
|
103
|
+
Object.assign(new Error("canceling statement due to lock timeout"), { code: "55P03" }),
|
|
104
|
+
new Error("Write failed", { cause: Object.assign(new Error("Lock wait timeout exceeded"), { errno: 1205 }) })
|
|
105
|
+
]) {
|
|
106
|
+
assert.equal(isDeadlockError(timeout), false, timeout.message);
|
|
107
|
+
assert.equal(await attemptsUntilRejected(timeout), 1, timeout.message);
|
|
108
|
+
}
|
|
134
109
|
const victim = deadlock();
|
|
135
|
-
assert.equal(await attemptsUntilRejected(
|
|
136
|
-
assert.equal(await attemptsUntilRejected(
|
|
137
|
-
|
|
110
|
+
assert.equal(await attemptsUntilRejected(victim), 4);
|
|
111
|
+
assert.equal(await attemptsUntilRejected(victim, { retries: 0 }), 1);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// What a handle's withTransaction rethrows when its insert lost to a committed one: the duplicate, rolled back in full.
|
|
115
|
+
function lostInsertRace() {
|
|
116
|
+
const duplicate = Object.assign(new Error("Duplicate entry 'k' for key 'requests_key_unique'"), { code: "ER_DUP_ENTRY", errno: 1062 });
|
|
117
|
+
return Object.assign(new Error("Write failed", { cause: duplicate }), { transactionOutcome: "rolledBack" });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
test("retryLostInsertRace runs a transaction once more when its insert lost a unique race, and answers the second run", async () => {
|
|
121
|
+
let runs = 0;
|
|
122
|
+
const result = await retryLostInsertRace({ context: {} }, async () => {
|
|
123
|
+
runs += 1;
|
|
124
|
+
if (runs === 1) {
|
|
125
|
+
throw lostInsertRace();
|
|
126
|
+
}
|
|
127
|
+
return "the winner's record";
|
|
128
|
+
});
|
|
129
|
+
assert.deepEqual([result, runs], ["the winner's record", 2]);
|
|
138
130
|
});
|
|
139
131
|
|
|
140
|
-
test("
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
132
|
+
test("retryLostInsertRace runs again only a lost race outside a caller's transaction, and only once", async () => {
|
|
133
|
+
async function runsUntilRejected(options, error) {
|
|
134
|
+
let runs = 0;
|
|
135
|
+
await assert.rejects(retryLostInsertRace(options, async () => {
|
|
136
|
+
runs += 1;
|
|
137
|
+
throw error;
|
|
138
|
+
}), (thrown) => thrown === error);
|
|
139
|
+
return runs;
|
|
140
|
+
}
|
|
141
|
+
// A second duplicate is final.
|
|
142
|
+
assert.equal(await runsUntilRejected({}, lostInsertRace()), 2);
|
|
143
|
+
// Inside the caller's transaction the failed insert failed that transaction: the caller gets the error.
|
|
144
|
+
assert.equal(await runsUntilRejected({ trx: {} }, lostInsertRace()), 1);
|
|
145
|
+
// Not a duplicate, or a duplicate whose transaction did not roll back in full.
|
|
146
|
+
assert.equal(await runsUntilRejected({}, Object.assign(new Error("Validation failed"), { transactionOutcome: "rolledBack" })), 1);
|
|
147
|
+
assert.equal(await runsUntilRejected({}, Object.assign(lostInsertRace(), { transactionOutcome: "pending" })), 1);
|
|
148
|
+
assert.equal(await runsUntilRejected({}, lostInsertRace().cause), 1);
|
|
149
|
+
});
|
|
147
150
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
151
|
+
test("retryLostInsertRace takes (options, run)", async () => {
|
|
152
|
+
const run = async () => "ran";
|
|
153
|
+
for (const call of [() => retryLostInsertRace(run), () => retryLostInsertRace(undefined, run), () => retryLostInsertRace({}, run, {})]) {
|
|
154
|
+
await assert.rejects(call(), /retryLostInsertRace\(\) takes \(options, run\)/u);
|
|
155
|
+
}
|
|
156
|
+
assert.equal(await retryLostInsertRace({}, run), "ran");
|
|
152
157
|
});
|
|
@@ -6,8 +6,11 @@ import test from "node:test";
|
|
|
6
6
|
import knexFactory from "knex";
|
|
7
7
|
import { createKnexMigrationConfig } from "../src/server/knexMigrationConfig.js";
|
|
8
8
|
import {
|
|
9
|
+
TestDatabaseMissingError,
|
|
9
10
|
countQueries,
|
|
10
11
|
requireDisposableDatabase,
|
|
12
|
+
requireTestDatabase,
|
|
13
|
+
tableNames,
|
|
11
14
|
testDatabaseNames,
|
|
12
15
|
testDatabaseRun,
|
|
13
16
|
testDatabaseSnapshotKey
|
|
@@ -156,3 +159,37 @@ test("countQueries() records each statement a piece of work sends, so a read per
|
|
|
156
159
|
await knex.destroy();
|
|
157
160
|
}
|
|
158
161
|
});
|
|
162
|
+
|
|
163
|
+
test("a data test without a test database fails instead of skipping", () => {
|
|
164
|
+
for (const environment of [{}, { TEST_DB_NAME: " " }]) {
|
|
165
|
+
assert.throws(() => requireTestDatabase(environment), (error) =>
|
|
166
|
+
error instanceof TestDatabaseMissingError && error.message === "TEST_DB_NAME is not set; data tests never skip.");
|
|
167
|
+
}
|
|
168
|
+
assert.equal(requireTestDatabase({ TEST_DB_NAME: " catalogue_test " }), "catalogue_test");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("tableNames() reads PostgreSQL's tables under each name Knex gives its client", async () => {
|
|
172
|
+
for (const client of ["pg", "postgres", "postgresql"]) {
|
|
173
|
+
const statements = [];
|
|
174
|
+
const knex = {
|
|
175
|
+
client: { config: { client } },
|
|
176
|
+
async raw(sql) {
|
|
177
|
+
statements.push(sql);
|
|
178
|
+
return { rows: [{ name: "shelves" }, { name: "books" }] };
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
assert.deepEqual(await tableNames(knex), ["books", "shelves"], client);
|
|
182
|
+
assert.match(statements[0], /information_schema\.tables WHERE table_schema = current_schema\(\)/u, client);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test("tableNames() lists a database's own tables, sorted, without SQLite's internal ones", async () => {
|
|
187
|
+
const knex = knexFactory({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
188
|
+
try {
|
|
189
|
+
await knex.schema.createTable("shelves", (table) => table.increments("id"));
|
|
190
|
+
await knex.schema.createTable("books", (table) => table.increments("id"));
|
|
191
|
+
assert.deepEqual(await tableNames(knex), ["books", "shelves"]);
|
|
192
|
+
} finally {
|
|
193
|
+
await knex.destroy();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { retryDeadlocks } from "./repositoryOptions.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Runs work in a transaction of the database's Knex instance, retrying deadlocks,
|
|
5
|
-
* or in the transaction the caller already holds.
|
|
6
|
-
* @param {object} dependencies
|
|
7
|
-
* @param {import("../server/types.js").RuntimeKnex} dependencies.knex
|
|
8
|
-
*/
|
|
9
|
-
function createTransactionManager({ knex }) {
|
|
10
|
-
/**
|
|
11
|
-
* @template T
|
|
12
|
-
* @param {(trx: import("../server/types.js").RuntimeKnexTransaction) => Promise<T>} work
|
|
13
|
-
* @param {{ trx?: import("../server/types.js").RuntimeKnexTransaction | null }} [options] A transaction to run in instead of a new one.
|
|
14
|
-
*/
|
|
15
|
-
async function inTransaction(work, { trx = null } = {}) {
|
|
16
|
-
// readable-check: allow method-probe - input validation: the caller hands over the work to run
|
|
17
|
-
if (typeof work !== "function") {
|
|
18
|
-
throw new TypeError("inTransaction requires a callback function.");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (trx) {
|
|
22
|
-
return work(trx);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return retryDeadlocks(() => knex.transaction((nextTrx) => work(nextTrx)));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return Object.freeze({ inTransaction });
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export { createTransactionManager };
|
package/src/shared/visibility.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { normalizeVisibilityContext } from "@jskit-ai/kernel/shared/support/visibility";
|
|
2
|
-
|
|
3
|
-
const ALWAYS_FALSE_SQL = "1 = 0";
|
|
4
|
-
const DEFAULT_VISIBILITY_COLUMNS = Object.freeze({
|
|
5
|
-
scopeOwnerId: "workspace_id",
|
|
6
|
-
userId: "user_id"
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
function applyVisibility(queryBuilder, visibilityContext = {}) {
|
|
10
|
-
const context = normalizeVisibilityContext(visibilityContext);
|
|
11
|
-
const workspaceColumn = DEFAULT_VISIBILITY_COLUMNS.scopeOwnerId;
|
|
12
|
-
const userColumn = DEFAULT_VISIBILITY_COLUMNS.userId;
|
|
13
|
-
|
|
14
|
-
if (context.visibility === "public") {
|
|
15
|
-
return queryBuilder;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (context.visibility === "workspace") {
|
|
19
|
-
if (!context.scopeOwnerId) {
|
|
20
|
-
return queryBuilder.whereRaw(ALWAYS_FALSE_SQL);
|
|
21
|
-
}
|
|
22
|
-
return queryBuilder.where(workspaceColumn, context.scopeOwnerId);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (context.visibility === "user") {
|
|
26
|
-
if (!context.userId) {
|
|
27
|
-
return queryBuilder.whereRaw(ALWAYS_FALSE_SQL);
|
|
28
|
-
}
|
|
29
|
-
return queryBuilder.where(userColumn, context.userId);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!context.scopeOwnerId || !context.userId) {
|
|
33
|
-
return queryBuilder.whereRaw(ALWAYS_FALSE_SQL);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return queryBuilder.where(workspaceColumn, context.scopeOwnerId).where(userColumn, context.userId);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function applyVisibilityOwners(payload = {}, visibilityContext = {}) {
|
|
40
|
-
const source = payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
41
|
-
const context = normalizeVisibilityContext(visibilityContext);
|
|
42
|
-
|
|
43
|
-
if (context.visibility === "public") {
|
|
44
|
-
return {
|
|
45
|
-
...source
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (context.visibility === "workspace" && !context.scopeOwnerId) {
|
|
50
|
-
throw new Error("Visibility context requires scopeOwnerId.");
|
|
51
|
-
}
|
|
52
|
-
if (context.visibility === "user" && !context.userId) {
|
|
53
|
-
throw new Error("Visibility context requires userId.");
|
|
54
|
-
}
|
|
55
|
-
if (context.visibility === "workspace_user" && (!context.scopeOwnerId || !context.userId)) {
|
|
56
|
-
throw new Error("Visibility context requires scopeOwnerId and userId.");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const ownedPayload = {
|
|
60
|
-
...source
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
if (context.scopeOwnerId) {
|
|
64
|
-
ownedPayload[DEFAULT_VISIBILITY_COLUMNS.scopeOwnerId] = context.scopeOwnerId;
|
|
65
|
-
}
|
|
66
|
-
if (context.userId) {
|
|
67
|
-
ownedPayload[DEFAULT_VISIBILITY_COLUMNS.userId] = context.userId;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return ownedPayload;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export { DEFAULT_VISIBILITY_COLUMNS, applyVisibility, applyVisibilityOwners };
|
package/test/runtimeCore.test.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
|
|
4
|
-
import { createWithTransaction } from "../src/shared/index.js";
|
|
5
|
-
import { createTransactionManager } from "../src/shared/transactionManager.js";
|
|
6
|
-
|
|
7
|
-
function createKnexStub() {
|
|
8
|
-
return {
|
|
9
|
-
async transaction(callback) {
|
|
10
|
-
return callback({ trxId: "trx-1" });
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
test("transaction manager wraps callback in knex transaction", async () => {
|
|
16
|
-
const manager = createTransactionManager({ knex: createKnexStub() });
|
|
17
|
-
const result = await manager.inTransaction(async (trx) => ({ ok: true, trx }));
|
|
18
|
-
assert.equal(result.ok, true);
|
|
19
|
-
assert.equal(result.trx.trxId, "trx-1");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("transaction manager runs a deadlocked transaction again but never a borrowed one", async () => {
|
|
23
|
-
const manager = createTransactionManager({ knex: createKnexStub() });
|
|
24
|
-
const deadlock = Object.assign(new Error("Deadlock found when trying to get lock"), { code: "ER_LOCK_DEADLOCK" });
|
|
25
|
-
let attempts = 0;
|
|
26
|
-
const result = await manager.inTransaction(async (trx) => {
|
|
27
|
-
attempts += 1;
|
|
28
|
-
if (attempts === 1) {
|
|
29
|
-
throw deadlock;
|
|
30
|
-
}
|
|
31
|
-
return trx.trxId;
|
|
32
|
-
});
|
|
33
|
-
assert.equal(result, "trx-1");
|
|
34
|
-
assert.equal(attempts, 2);
|
|
35
|
-
|
|
36
|
-
attempts = 0;
|
|
37
|
-
await assert.rejects(manager.inTransaction(async () => {
|
|
38
|
-
attempts += 1;
|
|
39
|
-
throw deadlock;
|
|
40
|
-
}, { trx: { trxId: "outer" } }), deadlock);
|
|
41
|
-
assert.equal(attempts, 1);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("createWithTransaction creates a reusable withTransaction function", async () => {
|
|
45
|
-
const knex = Object.assign(() => {
|
|
46
|
-
throw new Error("query execution not expected");
|
|
47
|
-
}, createKnexStub());
|
|
48
|
-
const withTransaction = createWithTransaction(knex);
|
|
49
|
-
const result = await withTransaction(async (trx) => ({ id: trx.trxId }));
|
|
50
|
-
assert.deepEqual(result, { id: "trx-1" });
|
|
51
|
-
});
|
package/test/visibility.test.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import { applyVisibility, applyVisibilityOwners } from "../src/shared/visibility.js";
|
|
4
|
-
|
|
5
|
-
function createQueryBuilderStub() {
|
|
6
|
-
return {
|
|
7
|
-
calls: [],
|
|
8
|
-
where(...args) {
|
|
9
|
-
this.calls.push(["where", ...args]);
|
|
10
|
-
return this;
|
|
11
|
-
},
|
|
12
|
-
whereRaw(sql) {
|
|
13
|
-
this.calls.push(["whereRaw", sql]);
|
|
14
|
-
return this;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
test("applyVisibility appends scope filters to query builders", () => {
|
|
20
|
-
const publicQuery = createQueryBuilderStub();
|
|
21
|
-
applyVisibility(publicQuery, {
|
|
22
|
-
visibility: "public"
|
|
23
|
-
});
|
|
24
|
-
assert.deepEqual(publicQuery.calls, []);
|
|
25
|
-
|
|
26
|
-
const workspaceQuery = createQueryBuilderStub();
|
|
27
|
-
applyVisibility(workspaceQuery, {
|
|
28
|
-
visibility: "workspace",
|
|
29
|
-
scopeOwnerId: 12
|
|
30
|
-
});
|
|
31
|
-
assert.deepEqual(workspaceQuery.calls, [["where", "workspace_id", "12"]]);
|
|
32
|
-
|
|
33
|
-
const userQuery = createQueryBuilderStub();
|
|
34
|
-
applyVisibility(userQuery, {
|
|
35
|
-
visibility: "user",
|
|
36
|
-
userId: 7
|
|
37
|
-
});
|
|
38
|
-
assert.deepEqual(userQuery.calls, [["where", "user_id", "7"]]);
|
|
39
|
-
|
|
40
|
-
const deniedQuery = createQueryBuilderStub();
|
|
41
|
-
applyVisibility(deniedQuery, {
|
|
42
|
-
visibility: "workspace_user",
|
|
43
|
-
scopeOwnerId: 3
|
|
44
|
-
});
|
|
45
|
-
assert.deepEqual(deniedQuery.calls, [["whereRaw", "1 = 0"]]);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test("applyVisibilityOwners injects owner columns for write payloads", () => {
|
|
49
|
-
assert.deepEqual(
|
|
50
|
-
applyVisibilityOwners(
|
|
51
|
-
{
|
|
52
|
-
name: "Alice"
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
visibility: "public"
|
|
56
|
-
}
|
|
57
|
-
),
|
|
58
|
-
{
|
|
59
|
-
name: "Alice"
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
assert.deepEqual(
|
|
64
|
-
applyVisibilityOwners(
|
|
65
|
-
{
|
|
66
|
-
name: "Alice"
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
visibility: "workspace_user",
|
|
70
|
-
scopeOwnerId: 4,
|
|
71
|
-
userId: 9
|
|
72
|
-
}
|
|
73
|
-
),
|
|
74
|
-
{
|
|
75
|
-
name: "Alice",
|
|
76
|
-
workspace_id: "4",
|
|
77
|
-
user_id: "9"
|
|
78
|
-
}
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
assert.throws(
|
|
82
|
-
() =>
|
|
83
|
-
applyVisibilityOwners(
|
|
84
|
-
{
|
|
85
|
-
name: "Alice"
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
visibility: "user"
|
|
89
|
-
}
|
|
90
|
-
),
|
|
91
|
-
/requires userId/
|
|
92
|
-
);
|
|
93
|
-
});
|