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: wrap multi-resource database operations in transactions #16

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

devin-ai-integration[bot]
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Feb 6, 2025

This PR adds transaction support to all multi-resource database operations in the services folder, ensuring atomic updates when related records are modified together.

Key changes:

  • Using Drizzle's transaction API directly (no custom wrapper)
  • All DB operations within a function moved inside transactions
  • Inngest events kept outside transactions
  • Added error handling with automatic rollbacks
  • Consistent transaction pattern across all services

Examples of operations now wrapped in transactions:

  • Moderation + Record updates
  • User Action + User updates
  • Record + User updates during ingestion
  • Summary updates (e.g. flaggedRecordsCount)
  • Appeal + Message updates
  • Rule + Strategy updates (including createCustomRule)
  • Webhook endpoint operations
  • Ruleset find-or-create operations

Link to Devin run: https://app.devin.ai/sessions/a4f676cc74194cd2906fbecf2537cc10

Requested by: [email protected]

- Add transaction helper to db/index.ts
- Wrap all multi-resource operations in transactions
- Keep Inngest events outside transaction scope
- Add proper error handling and rollbacks

Co-Authored-By: [email protected] <[email protected]>
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add "(aside)" to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@s3ththompson
Copy link
Contributor

We don't need a custom wrapper around Drizzle's tx transactions. Please use this syntax:

await db.transaction(async (tx) => {
  await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
  await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
});

Do not delete any existing comments. Do not add any new comments. Do not move blocks of code unnecessarily.

Add transactions whenever we query something and use the result in a subsequent update. For example, there should be a transaction in services/appeal-actions.ts. There are other examples like this. Please update.

services/appeal-actions.ts Show resolved Hide resolved
services/appeal-actions.ts Show resolved Hide resolved
services/user-actions.ts Show resolved Hide resolved
.where(and(eq(schema.users.clerkOrganizationId, clerkOrganizationId), eq(schema.users.id, record.userId)));
}

// Keep Inngest event outside transaction
Copy link
Contributor

Choose a reason for hiding this comment

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

don't add this comment

.where(and(eq(schema.users.clerkOrganizationId, clerkOrganizationId), eq(schema.users.id, record.userId)));
}

// Keep Inngest event outside transaction
Copy link
Contributor

Choose a reason for hiding this comment

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

don't add this comment

services/user-actions.ts Show resolved Hide resolved

// sync the record user status with the new status
Copy link
Contributor

Choose a reason for hiding this comment

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

don't delete this comment

});
} catch (error) {
console.error(error);
// Keep Inngest event outside transaction
Copy link
Contributor

Choose a reason for hiding this comment

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

don't add this comment

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.

1 participant