-
-
Notifications
You must be signed in to change notification settings - Fork 151
fix(orm): add mutex for single-connection adapters #2805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * This mutex is used to ensure that only one operation at a time can | ||
| * acquire a connection from the driver. This is necessary when the | ||
| * driver only has a single connection, like SQLite and PGlite. | ||
| * | ||
| * @see {@link https://github.com/kysely-org/kysely/blob/478ec67b2de2568f5590a015d3e120644e81bd87/src/driver/connection-mutex.ts|Kysely Source} | ||
| */ | ||
| export class ConnectionMutex { | ||
| #promise?: Promise<void>; | ||
| #resolve?: () => void; | ||
|
|
||
| async obtainLock(): Promise<void> { | ||
| while (this.#promise) { | ||
| await this.#promise; | ||
| } | ||
|
|
||
| this.#promise = new Promise((resolve) => { | ||
| this.#resolve = resolve; | ||
| }); | ||
| } | ||
|
|
||
| releaseLock(): void { | ||
| const resolve = this.#resolve; | ||
|
|
||
| this.#promise = undefined; | ||
| this.#resolve = undefined; | ||
|
|
||
| resolve?.(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { describe, it } from 'vitest'; | ||
| import { schema } from './schema'; | ||
|
|
||
| // https://github.com/zenstackhq/zenstack/issues/2788 | ||
|
|
||
| describe('Regression for issue #2788', () => { | ||
| it('Promise.all does not break upsert', async () => { | ||
| const db = await createTestClient(schema); | ||
| const user = await db.user.create({ | ||
| data: { | ||
| email: 'test@zenstack.dev', | ||
| posts: { | ||
| create: [ | ||
| { | ||
| title: 'Post 1', | ||
| content: 'This is a test post', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| include: { posts: true }, | ||
| }); | ||
| console.log('User created:', user); | ||
|
|
||
| const posts: any[] = await db.post.findMany(); | ||
|
|
||
| posts[0].title = 'Post 1 Updated'; | ||
|
|
||
| posts.push({ | ||
| id: 'cmstai1q2000104js3i7s2d8l', | ||
| title: 'Post 2', | ||
| content: 'This is a test post', | ||
| authorId: user.id, | ||
| }); | ||
|
|
||
| await Promise.all( | ||
| posts.map(async (p) => { | ||
| await db.post.upsert({ | ||
| where: { id: p.id }, | ||
| update: { ...p }, | ||
| create: { title: p.title!, content: p.content!, authorId: p.authorId! }, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| const postsUpdated = await db.post.findMany(); | ||
| console.log('posts upserted:', postsUpdated); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // DO NOT MODIFY THIS FILE // | ||
| // This file is automatically generated by ZenStack CLI and should not be manually updated. // | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /* eslint-disable */ | ||
|
|
||
| import { type SchemaDef, type AttributeApplication, type FieldDefault, ExpressionUtils } from "@zenstackhq/schema"; | ||
| export class SchemaType implements SchemaDef { | ||
| provider = { | ||
| type: "sqlite" | ||
| } as const; | ||
| models = { | ||
| User: { | ||
| name: "User", | ||
| fields: { | ||
| id: { | ||
| name: "id", | ||
| type: "String", | ||
| id: true, | ||
| attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }] as readonly AttributeApplication[], | ||
| default: ExpressionUtils.call("cuid") as FieldDefault | ||
| }, | ||
| email: { | ||
| name: "email", | ||
| type: "String", | ||
| unique: true, | ||
| attributes: [{ name: "@unique" }, { name: "@email" }, { name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(6) }, { name: "max", value: ExpressionUtils.literal(32) }] }] as readonly AttributeApplication[] | ||
| }, | ||
| posts: { | ||
| name: "posts", | ||
| type: "Post", | ||
| array: true, | ||
| relation: { opposite: "author" } | ||
| } | ||
| }, | ||
| idFields: ["id"], | ||
| uniqueFields: { | ||
| id: { type: "String" }, | ||
| email: { type: "String" } | ||
| } | ||
| }, | ||
| Post: { | ||
| name: "Post", | ||
| fields: { | ||
| id: { | ||
| name: "id", | ||
| type: "String", | ||
| id: true, | ||
| attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }] as readonly AttributeApplication[], | ||
| default: ExpressionUtils.call("cuid") as FieldDefault | ||
| }, | ||
| createdAt: { | ||
| name: "createdAt", | ||
| type: "DateTime", | ||
| attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }] as readonly AttributeApplication[], | ||
| default: ExpressionUtils.call("now") as FieldDefault | ||
| }, | ||
| updatedAt: { | ||
| name: "updatedAt", | ||
| type: "DateTime", | ||
| updatedAt: true, | ||
| attributes: [{ name: "@updatedAt" }] as readonly AttributeApplication[] | ||
| }, | ||
| title: { | ||
| name: "title", | ||
| type: "String", | ||
| attributes: [{ name: "@length", args: [{ name: "min", value: ExpressionUtils.literal(1) }, { name: "max", value: ExpressionUtils.literal(256) }] }] as readonly AttributeApplication[] | ||
| }, | ||
| content: { | ||
| name: "content", | ||
| type: "String" | ||
| }, | ||
| published: { | ||
| name: "published", | ||
| type: "Boolean", | ||
| attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal(false) }] }] as readonly AttributeApplication[], | ||
| default: false as FieldDefault | ||
| }, | ||
| author: { | ||
| name: "author", | ||
| type: "User", | ||
| 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[], | ||
| relation: { opposite: "posts", fields: ["authorId"], references: ["id"], onDelete: "Cascade" } | ||
| }, | ||
| authorId: { | ||
| name: "authorId", | ||
| type: "String", | ||
| foreignKeyFor: [ | ||
| "author" | ||
| ] as readonly string[] | ||
| } | ||
| }, | ||
| idFields: ["id"], | ||
| uniqueFields: { | ||
| id: { type: "String" } | ||
| } | ||
| } | ||
| } as const; | ||
| authType = "User" as const; | ||
| plugins = {}; | ||
| } | ||
| export const schema = new SchemaType(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // This is a sample model to get you started. | ||
|
|
||
| /// A sample data source using local sqlite db. | ||
| datasource db { | ||
| provider = 'sqlite' | ||
| url = 'file:./dev.db' | ||
| } | ||
|
|
||
| /// User model | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| email String @unique @email @length(6, 32) | ||
| posts Post[] | ||
| } | ||
|
|
||
| /// Post model | ||
| model Post { | ||
| id String @id @default(cuid()) | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
| title String @length(1, 256) | ||
| content String | ||
| published Boolean @default(false) | ||
| author User @relation(fields: [authorId], references: [id], onDelete: Cascade) | ||
| authorId String | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.