Skip to content

Commit a4a25c4

Browse files
authored
fix(orm): add mutex for single-connection adapters (#2805)
1 parent ef87981 commit a4a25c4

6 files changed

Lines changed: 267 additions & 13 deletions

File tree

‎packages/orm/src/client/client-impl.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ export class ClientImpl {
122122
this.auth = baseClient.auth;
123123
this.slowQueries = baseClient.slowQueries;
124124
} else {
125-
const driver = new ZenStackDriver(options.dialect.createDriver(), new Log(this.$options.log ?? []));
126-
const compiler = options.dialect.createQueryCompiler();
127125
const adapter = options.dialect.createAdapter();
126+
const driver = new ZenStackDriver(
127+
options.dialect.createDriver(),
128+
new Log(this.$options.log ?? []),
129+
adapter,
130+
);
131+
const compiler = options.dialect.createQueryCompiler();
128132
const connectionProvider = new DefaultConnectionProvider(driver);
129133

130134
this.kyselyProps = {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This mutex is used to ensure that only one operation at a time can
3+
* acquire a connection from the driver. This is necessary when the
4+
* driver only has a single connection, like SQLite and PGlite.
5+
*
6+
* @see {@link https://github.com/kysely-org/kysely/blob/478ec67b2de2568f5590a015d3e120644e81bd87/src/driver/connection-mutex.ts|Kysely Source}
7+
*/
8+
export class ConnectionMutex {
9+
#promise?: Promise<void>;
10+
#resolve?: () => void;
11+
12+
async obtainLock(): Promise<void> {
13+
while (this.#promise) {
14+
await this.#promise;
15+
}
16+
17+
this.#promise = new Promise((resolve) => {
18+
this.#resolve = resolve;
19+
});
20+
}
21+
22+
releaseLock(): void {
23+
const resolve = this.#resolve;
24+
25+
this.#promise = undefined;
26+
this.#resolve = undefined;
27+
28+
resolve?.();
29+
}
30+
}

‎packages/orm/src/client/executor/zenstack-driver.ts‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
import type { CompiledQuery, DatabaseConnection, Driver, Log, QueryResult, TransactionSettings } from 'kysely';
1+
import type {
2+
CompiledQuery,
3+
DatabaseConnection,
4+
DialectAdapter,
5+
Driver,
6+
Log,
7+
QueryResult,
8+
TransactionSettings,
9+
} from 'kysely';
10+
import { ConnectionMutex } from './connection-mutex';
211

312
/**
413
* Copied from kysely's RuntimeDriver
514
*/
615
export class ZenStackDriver implements Driver {
716
readonly #driver: Driver;
817
readonly #log: Log;
18+
readonly #connectionMutex?: ConnectionMutex;
919

1020
#initPromise?: Promise<void>;
1121
#initDone: boolean;
1222
#destroyPromise?: Promise<void>;
1323
#connections = new WeakSet<DatabaseConnection>();
1424
#txConnections = new WeakMap<DatabaseConnection, Array<() => Promise<unknown>>>();
1525

16-
constructor(driver: Driver, log: Log) {
26+
constructor(driver: Driver, log: Log, adapter: DialectAdapter) {
1727
this.#initDone = false;
1828
this.#driver = driver;
1929
this.#log = log;
30+
31+
if (!adapter.supportsMultipleConnections) {
32+
this.#connectionMutex = new ConnectionMutex();
33+
}
2034
}
2135

2236
async init(): Promise<void> {
@@ -48,21 +62,30 @@ export class ZenStackDriver implements Driver {
4862
await this.init();
4963
}
5064

51-
const connection = await this.#driver.acquireConnection();
65+
await this.#connectionMutex?.obtainLock();
5266

53-
if (!this.#connections.has(connection)) {
54-
if (this.#needsLogging()) {
55-
this.#addLogging(connection);
56-
}
67+
try {
68+
const connection = await this.#driver.acquireConnection();
69+
if (!this.#connections.has(connection)) {
70+
if (this.#needsLogging()) {
71+
this.#addLogging(connection);
72+
}
5773

58-
this.#connections.add(connection);
74+
this.#connections.add(connection);
75+
}
76+
return connection;
77+
} catch (error) {
78+
this.#connectionMutex?.releaseLock();
79+
throw error;
5980
}
60-
61-
return connection;
6281
}
6382

6483
async releaseConnection(connection: DatabaseConnection): Promise<void> {
65-
await this.#driver.releaseConnection(connection);
84+
try {
85+
await this.#driver.releaseConnection(connection);
86+
} finally {
87+
this.#connectionMutex?.releaseLock();
88+
}
6689
}
6790

6891
async beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void> {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
import { schema } from './schema';
4+
5+
// https://github.com/zenstackhq/zenstack/issues/2788
6+
7+
describe('Regression for issue #2788', () => {
8+
it('does not error during concurrent upserts', async () => {
9+
const db = await createTestClient(schema);
10+
const user = await db.user.create({
11+
data: {
12+
13+
posts: {
14+
create: [
15+
{
16+
title: 'Post 1',
17+
content: 'This is a test post',
18+
},
19+
],
20+
},
21+
},
22+
include: { posts: true },
23+
});
24+
25+
let posts: any[] = await db.post.findMany();
26+
27+
posts[0].title = 'Post 1 Updated';
28+
29+
posts.push({
30+
id: 'cmstai1q2000104js3i7s2d8l',
31+
title: 'Post 2',
32+
content: 'This is a test post',
33+
authorId: user.id,
34+
});
35+
36+
await Promise.all(
37+
posts.map(async (p) => {
38+
await db.post.upsert({
39+
where: { id: p.id },
40+
update: { ...p },
41+
create: { title: p.title!, content: p.content!, authorId: p.authorId! },
42+
});
43+
}),
44+
);
45+
46+
posts = await db.post.findMany();
47+
48+
expect(posts.find((p) => p.title === 'Post 1 Updated')).toMatchObject({
49+
title: 'Post 1 Updated',
50+
content: 'This is a test post',
51+
published: false,
52+
});
53+
54+
expect(posts.find((p) => p.title === 'Post 2')).toMatchObject({
55+
title: 'Post 2',
56+
content: 'This is a test post',
57+
published: false,
58+
});
59+
60+
await expect(
61+
db.post.findUnique({
62+
where: {
63+
id: 'cmstai1q2000104js3i7s2d8l',
64+
},
65+
}),
66+
).resolves.toBeNull();
67+
});
68+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////
2+
// DO NOT MODIFY THIS FILE //
3+
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
4+
//////////////////////////////////////////////////////////////////////////////////////////////
5+
6+
/* eslint-disable */
7+
8+
import { type SchemaDef, type AttributeApplication, type FieldDefault, ExpressionUtils } from "@zenstackhq/schema";
9+
export class SchemaType implements SchemaDef {
10+
provider = {
11+
type: "sqlite"
12+
} as const;
13+
models = {
14+
User: {
15+
name: "User",
16+
fields: {
17+
id: {
18+
name: "id",
19+
type: "String",
20+
id: true,
21+
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }] as readonly AttributeApplication[],
22+
default: ExpressionUtils.call("cuid") as FieldDefault
23+
},
24+
email: {
25+
name: "email",
26+
type: "String",
27+
unique: true,
28+
attributes: [{ name: "@unique" }, { name: "@email" }, { name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(6) }, { name: "max", value: ExpressionUtils.literal(32) }] }] as readonly AttributeApplication[]
29+
},
30+
posts: {
31+
name: "posts",
32+
type: "Post",
33+
array: true,
34+
relation: { opposite: "author" }
35+
}
36+
},
37+
idFields: ["id"],
38+
uniqueFields: {
39+
id: { type: "String" },
40+
email: { type: "String" }
41+
}
42+
},
43+
Post: {
44+
name: "Post",
45+
fields: {
46+
id: {
47+
name: "id",
48+
type: "String",
49+
id: true,
50+
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }] as readonly AttributeApplication[],
51+
default: ExpressionUtils.call("cuid") as FieldDefault
52+
},
53+
createdAt: {
54+
name: "createdAt",
55+
type: "DateTime",
56+
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }] as readonly AttributeApplication[],
57+
default: ExpressionUtils.call("now") as FieldDefault
58+
},
59+
updatedAt: {
60+
name: "updatedAt",
61+
type: "DateTime",
62+
updatedAt: true,
63+
attributes: [{ name: "@updatedAt" }] as readonly AttributeApplication[]
64+
},
65+
title: {
66+
name: "title",
67+
type: "String",
68+
attributes: [{ name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(1) }, { name: "max", value: ExpressionUtils.literal(256) }] }] as readonly AttributeApplication[]
69+
},
70+
content: {
71+
name: "content",
72+
type: "String"
73+
},
74+
published: {
75+
name: "published",
76+
type: "Boolean",
77+
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal(false) }] }] as readonly AttributeApplication[],
78+
default: false as FieldDefault
79+
},
80+
author: {
81+
name: "author",
82+
type: "User",
83+
attributes: [{ name: "@relation", args: [{ name: "fields", value: ExpressionUtils.array("String", [ExpressionUtils.field("authorId")]) }, { name: "references", value: ExpressionUtils.array("String", [ExpressionUtils.field("id")]) }, { name: "onDelete", value: ExpressionUtils.literal("Cascade") }] }] as readonly AttributeApplication[],
84+
relation: { opposite: "posts", fields: ["authorId"], references: ["id"], onDelete: "Cascade" }
85+
},
86+
authorId: {
87+
name: "authorId",
88+
type: "String",
89+
foreignKeyFor: [
90+
"author"
91+
] as readonly string[]
92+
}
93+
},
94+
idFields: ["id"],
95+
uniqueFields: {
96+
id: { type: "String" }
97+
}
98+
}
99+
} as const;
100+
authType = "User" as const;
101+
plugins = {};
102+
}
103+
export const schema = new SchemaType();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This is a sample model to get you started.
2+
3+
/// A sample data source using local sqlite db.
4+
datasource db {
5+
provider = 'sqlite'
6+
url = 'file:./dev.db'
7+
}
8+
9+
/// User model
10+
model User {
11+
id String @id @default(cuid())
12+
email String @unique @email @length(6, 32)
13+
posts Post[]
14+
}
15+
16+
/// Post model
17+
model Post {
18+
id String @id @default(cuid())
19+
createdAt DateTime @default(now())
20+
updatedAt DateTime @updatedAt
21+
title String @length(1, 256)
22+
content String
23+
published Boolean @default(false)
24+
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
25+
authorId String
26+
}

0 commit comments

Comments
 (0)