Skip to content

Optimize: Wrap custom field creation in transaction#145

Merged
ManukMinasyan merged 2 commits intorelaticle:mainfrom
NeloNew:fix/sentry-n1-custom-fields-transaction
Feb 21, 2026
Merged

Optimize: Wrap custom field creation in transaction#145
ManukMinasyan merged 2 commits intorelaticle:mainfrom
NeloNew:fix/sentry-n1-custom-fields-transaction

Conversation

@NeloNew
Copy link
Copy Markdown
Contributor

@NeloNew NeloNew commented Feb 20, 2026

Fixes Sentry Issues

Resolves #142

  • RELATICLE-CRM-2Z (14 events)
  • RELATICLE-CRM-30 (4 events)
  • RELATICLE-CRM-31 (13 events)

Problem

During team creation, ~20 custom fields are created in a loop. Each call to $migrator->create() runs in its own transaction, adding overhead.

Solution

Wrap all custom field creation calls in a single DB transaction to reduce overhead.

Changes

// Before: 20 separate transactions
foreach (self::MODEL_ENUM_MAP as $modelClass => $enumClass) {
    foreach ($enumClass::cases() as $enum) {
        $this->createCustomField($modelClass, $enum);
    }
}

// After: Single transaction
DB::transaction(function () {
    foreach (self::MODEL_ENUM_MAP as $modelClass => $enumClass) {
        foreach ($enumClass::cases() as $enum) {
            $this->createCustomField($modelClass, $enum);
        }
    }
});

Impact

  • Reduces transaction overhead during team creation
  • Improves registration/OAuth performance
  • Maintains atomicity (all-or-nothing)
  • No breaking changes

Testing

  • Manual testing: Team creation via registration
  • Sentry monitoring: Verify reduced N+1 alerts

Notes

This is a performance optimization that doesn't change functionality. For further optimization, the relaticle/custom-fields package could add a batch create method to eliminate the EXISTS checks entirely.

Fixes Sentry issues RELATICLE-CRM-2Z, RELATICLE-CRM-30, RELATICLE-CRM-31

Wraps the ~20 custom field creation calls in a single DB transaction
to reduce overhead during team creation.

Impact:
- Reduces transaction overhead
- Improves performance during registration/OAuth
- Still creates all fields atomically

Closes #142
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes team creation performance by wrapping custom field creation in a single database transaction. The change addresses Sentry issues related to N+1 queries and transaction overhead during team registration and OAuth flows.

Changes:

  • Wraps the custom field creation loop in a DB::transaction() to reduce overhead from ~20 individual transactions to 1 single transaction
  • Adds an inline comment explaining the optimization purpose
  • Maintains atomicity by ensuring all custom fields are created or none are (all-or-nothing behavior)

Copy link
Copy Markdown
Contributor

@ManukMinasyan ManukMinasyan left a comment

Choose a reason for hiding this comment

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

Two small fixes needed:

  1. Unused variable capture$team in the use clause is not referenced inside the closure. Change function () use ($team): void to function (): void.

  2. Unnecessary comment — The // Wrap custom field creation in transaction to reduce overhead comment restates what DB::transaction() already communicates. Remove it.

-        // Wrap custom field creation in transaction to reduce overhead
-        DB::transaction(function () use ($team): void {
+        DB::transaction(function (): void {

Otherwise LGTM — clean, focused change.

- Remove unused $team from closure
- Remove redundant comment
@ManukMinasyan
Copy link
Copy Markdown
Contributor

Heey, @NeloNew is this PR ready for merge?

@ManukMinasyan ManukMinasyan merged commit 73968af into relaticle:main Feb 21, 2026
3 checks passed
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.

3 participants