Skip to content

add inner transactions to avoid aborting (#4072)#4125

Merged
ildyria merged 1 commit intoLycheeOrg:masterfrom
FredPraca:master
Feb 28, 2026
Merged

add inner transactions to avoid aborting (#4072)#4125
ildyria merged 1 commit intoLycheeOrg:masterfrom
FredPraca:master

Conversation

@FredPraca
Copy link
Contributor

@FredPraca FredPraca commented Feb 27, 2026

This PR aims at fixing the migration for dealing with duplicates.
There seems to be an incorrect behavior in certain cases as stated in the discussion #4072.

The goal is to avoid aborting the transaction when dealing with photos that are already linked to albums.
To do this, inner transactions have been setup to deal with exceptions and this way, avoiding outer transaction abortion.

Moreover the outer transaction was put inside the function to clearly states the fact that we are locally manually dealing with transactions.

Summary by CodeRabbit

  • Refactor
    • Improved photo deduplication reliability by isolating per-item operations so individual record conflicts no longer abort the entire cleanup.
    • Enhanced error handling to continue processing when single records fail, reducing interruptions during bulk dedupe while preserving overall cleanup and data consistency.

@FredPraca FredPraca requested a review from a team as a code owner February 27, 2026 17:22
@coderabbitai
Copy link

coderabbitai bot commented Feb 27, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds use Illuminate\Database\QueryException and nests per-photo inner DB transactions (begin/commit/rollBack) with explicit QueryException handling around updates to photo_album and purchasables, while preserving the outer DB::transaction in up() and keeping final cleanup/deletes; minor formatting tweaks.

Changes

Cohort / File(s) Summary
Migration: per-photo transaction changes
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php
Adds use Illuminate\Database\QueryException; wraps each photo update in its own DB::beginTransaction()/commit()/rollBack; catches QueryException to rollback and continue; rethrows generic \Exception; keeps outer transaction and final cleanup deletes; minor indentation/formatting adjustments.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hop through rows and checks with care,
I nest my transactions soft as air,
If a query trips, I roll it back — then dart,
Continuing fixes, one tidy part,
Clean data springs from every careful start.

🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 90b0c8b and a28475a.

📒 Files selected for processing (1)
  • database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php

@FredPraca FredPraca marked this pull request as draft February 27, 2026 18:17
@FredPraca FredPraca marked this pull request as ready for review February 28, 2026 02:24
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php (1)

19-21: Inconsistent indentation: spaces used instead of tabs.

Lines 19–21 and 38–84 use spaces for indentation while the rest of the file uses tabs. As per coding guidelines, run vendor/bin/php-cs-fixer fix before committing to ensure consistent formatting.

Also applies to: 38-44


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a28475a and 863dc52.

📒 Files selected for processing (1)
  • database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php

@FredPraca FredPraca marked this pull request as draft February 28, 2026 09:44
@codecov
Copy link

codecov bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.55%. Comparing base (6ee6e26) to head (e0241ba).
⚠️ Report is 1 commits behind head on master.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ildyria
Copy link
Member

ildyria commented Feb 28, 2026

If you can do make formatting, that should fix the formatting issues ;)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php (1)

56-73: ⚠️ Potential issue | 🟠 Major

Do not suppress all QueryExceptions in inner transactions.

At Line 56 and Line 70, every QueryException is ignored. That can hide real DB errors (not just duplicate-link collisions), then Lines 78-83 still delete rows, which can lose associations/data. Please only swallow duplicate-key violations and rethrow everything else.

Suggested fix
                 } catch (QueryException $e) {
-                    // If this crashes this means that the link already exists between the photo and the album.
-                    // We ignore it.
-                    // So we rollback this part
                     DB::rollBack();
+                    $sql_state = (string) ($e->errorInfo[0] ?? $e->getCode());
+                    $driver_code = $e->errorInfo[1] ?? null;
+                    $is_duplicate = $sql_state === '23505'
+                        || ($sql_state === '23000' && in_array($driver_code, [1062, 19], true));
+                    if (!$is_duplicate) {
+                        throw $e;
+                    }
                 } catch (\Exception $e) {
                     throw $e;
                 }
@@
                 } catch (QueryException $e) {
-                    // Same shit as above. :)
                     DB::rollBack();
+                    $sql_state = (string) ($e->errorInfo[0] ?? $e->getCode());
+                    $driver_code = $e->errorInfo[1] ?? null;
+                    $is_duplicate = $sql_state === '23505'
+                        || ($sql_state === '23000' && in_array($driver_code, [1062, 19], true));
+                    if (!$is_duplicate) {
+                        throw $e;
+                    }
                 } catch (\Exception $e) {
                     throw $e;
                 }
In Laravel (QueryException/PDO), what SQLSTATE and driver error codes indicate duplicate-key violations for MySQL/MariaDB, PostgreSQL, and SQLite?

As per coding guidelines: "Use in_array() with true as the third parameter".


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 36a4d15 and bbdc63e.

📒 Files selected for processing (1)
  • database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php

@FredPraca
Copy link
Contributor Author

If you can do make formatting, that should fix the formatting issues ;)

Damn, another tool to learn !! 😃

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (2)
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php (2)

56-63: ⚠️ Potential issue | 🟠 Major

Add SQLSTATE filtering to only suppress duplicate-key violations.

Catching all QueryException errors without checking the error code will silently swallow connection errors, syntax errors, timeouts, and other real failures. Filter by SQLSTATE code to only suppress duplicate-key violations ('23000' for MySQL/SQLite, '23505' for PostgreSQL).

Proposed fix
             } catch (QueryException $e) {
                 // If this crashes this means that the link already exists between the photo and the album.
                 // We ignore it.
                 // So we rollback this part
                 DB::rollBack();
+                $sql_state = $e->getCode();
+                if (!in_array($sql_state, ['23000', '23505'], true)) {
+                    throw $e;
+                }
             } catch (\Exception $e) {
                 throw $e;
             }

70-75: ⚠️ Potential issue | 🟠 Major

Same issue: Add SQLSTATE filtering here as well.

This catch block has the same problem—it will silently suppress all query exceptions, not just duplicate-key violations.

Proposed fix
             } catch (QueryException $e) {
                 // Same shit as above. :)
                 DB::rollBack();
+                $sql_state = $e->getCode();
+                if (!in_array($sql_state, ['23000', '23505'], true)) {
+                    throw $e;
+                }
             } catch (\Exception $e) {
                 throw $e;
             }

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bbdc63e and e0241ba.

📒 Files selected for processing (1)
  • database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php

@ildyria ildyria enabled auto-merge (squash) February 28, 2026 12:07
@ildyria ildyria merged commit 88ef5f9 into LycheeOrg:master Feb 28, 2026
43 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.

2 participants