add inner transactions to avoid aborting (#4072)#4125
add inner transactions to avoid aborting (#4072)#4125ildyria merged 1 commit intoLycheeOrg:masterfrom
Conversation
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 1✅ Passed checks (1 passed)
✏️ 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. Comment |
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php
Outdated
Show resolved
Hide resolved
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php
Show resolved
Hide resolved
There was a problem hiding this comment.
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 fixbefore committing to ensure consistent formatting.Also applies to: 38-44
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php
Show resolved
Hide resolved
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php
Outdated
Show resolved
Hide resolved
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php
Outdated
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. 🚀 New features to boost your workflow:
|
|
If you can do |
There was a problem hiding this comment.
♻️ Duplicate comments (1)
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php (1)
56-73:⚠️ Potential issue | 🟠 MajorDo not suppress all
QueryExceptions in inner transactions.At Line 56 and Line 70, every
QueryExceptionis 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".
Damn, another tool to learn !! 😃 |
There was a problem hiding this comment.
♻️ Duplicate comments (2)
database/migrations/2026_01_19_193315_refactor_photo_avoid_duplicates.php (2)
56-63:⚠️ Potential issue | 🟠 MajorAdd SQLSTATE filtering to only suppress duplicate-key violations.
Catching all
QueryExceptionerrors 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 | 🟠 MajorSame 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; }
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