Skip to content
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

feat: database transactions #48

Merged
merged 4 commits into from
Jan 3, 2025
Merged

feat: database transactions #48

merged 4 commits into from
Jan 3, 2025

Conversation

0xnigir1
Copy link
Collaborator

@0xnigir1 0xnigir1 commented Dec 31, 2024

🤖 Linear

Closes GIT-217 GIT-214

Description

As part of advanced recovery & error handling, we implement database transactions so we have a more granular control on DB operations, having the ability to rollback changes when -in a set of Changesets- one of the operation fails.

Note: the callback pattern isn't the best option but we're limited to it given the current Transaction API from Kysely

Checklist before requesting a review

  • I have conducted a self-review of my code.
  • I have conducted a QA.
  • If it is a core feature, I have included comprehensive tests.

@0xnigir1 0xnigir1 self-assigned this Dec 31, 2024
Copy link

linear bot commented Dec 31, 2024

Copy link
Collaborator

@jahabeebs jahabeebs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks: good
comments: few

}
}
await this.transactionManager.runInTransaction(async (tx) => {
this.logger.debug("Starting transaction...");
Copy link
Collaborator

@jahabeebs jahabeebs Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe would say "starting changeset transaction" or something since I imagine the repo may have other transactions happening

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally somewhere there's a completed message too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed: 6d5b44e

@@ -96,25 +96,23 @@ export class KyselyApplicationRepository implements IApplicationRepository {
}

/* @inheritdoc */
async insertApplication(application: NewApplication): Promise<void> {
async insertApplication(application: NewApplication, tx?: Kysely<Database>): Promise<void> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be called db not tx? elsewhere in the repo you define db: Kysely

}

/* @inheritdoc */
async updateApplication(
where: { id: string; chainId: ChainId; roundId: string },
application: PartialApplication,
tx?: Kysely<Database>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

import { Database, ITransactionManager } from "../../internal.js";

export class KyselyTransactionManager implements ITransactionManager<Kysely<Database>> {
constructor(private readonly db: Kysely<Database>) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing, it's a db not a tx right 🤔

@@ -0,0 +1,8 @@
// packages/repository/src/types/transaction.types.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete comment


import { Database } from "../internal.js";

export type KyselyTransaction = Kysely<Database>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we using Kysely<Database> for the params that are "tx" rather than using KyselyTransaction? It might be nice to use KyselyTransaction for those if these really are txs so that it looks intentional rather than both txs and db params typed as Kysely<Database>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch ser, will update to the Transaction type from Kysely, this also addresses your prev comments and avoids the confussion

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed: 6d5b44e

@0xnigir1 0xnigir1 requested a review from jahabeebs December 31, 2024 19:53
Copy link
Collaborator

@jahabeebs jahabeebs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems tests are failing

Copy link
Collaborator

@jahabeebs jahabeebs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After pipeline fix will approve 👌

@0xnigir1 0xnigir1 requested a review from jahabeebs January 2, 2025 01:42
Copy link
Collaborator

@0xkenj1 0xkenj1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gj ser, some minor commnets

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to test reveted records on the database, like an in memory pg ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe i am not sure how txs work. Changes are applied one by one and if some fails it reverts the applied ? or it applies all the changes at the same time and if something fails nothing is applied ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

txs are all or nothing, so all changes are applied or neither of them. regarding tests, yeah that would be nice but these are e2e tests not unit so i was planning on attacking them on the e2e milestone, we don't want to test Kysely library itself but rather that the whole system works as expected

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a todo or linear task for testing invalid inputs to handlers ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add it to Enhancements milestone 🫡

@0xnigir1 0xnigir1 merged commit 0a645ba into dev Jan 3, 2025
6 checks passed
@0xnigir1 0xnigir1 deleted the feat/db-transactions branch January 3, 2025 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants