Repository navigation
fix: Transaktionszähler fällt nicht mehr unter null #104
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
Open
micharoesler
wants to merge
2
commits into
5.x
Choose a base branch
from
fix/transaction-counter-below-zero
base: 5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Artemeon Core - Web Application Framework. | ||
| * | ||
| * (c) Artemeon <www.artemeon.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Artemeon\Database\Tests; | ||
|
|
||
| use Artemeon\Database\Connection; | ||
| use Artemeon\Database\Exception\CommitException; | ||
| use Artemeon\Database\Schema\DataType; | ||
| use Exception; | ||
| use Override; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| class ConnectionTransactionBalanceTest extends ConnectionTestCase | ||
| { | ||
| private const string TABLE_NAME = 'agp_temp_autotest_balance'; | ||
|
|
||
| private Connection $connection; | ||
|
|
||
| #[Override] | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->connection = $this->getConnection(); | ||
| $this->connection->createTable(self::TABLE_NAME, ['temp_id' => [DataType::CHAR20, false]], ['temp_id']); | ||
| self::getLogger()->records = []; | ||
| } | ||
|
|
||
| #[Override] | ||
| protected function tearDown(): void | ||
| { | ||
| $this->connection->dropTable(self::TABLE_NAME); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testCommitWithoutOpenTransactionKeepsTheNextTransactionIntact(): void | ||
| { | ||
| $this->connection->commit(); | ||
|
|
||
| $this->connection->beginTransaction(); | ||
| $this->insertRow(); | ||
| $this->connection->rollBack(); | ||
|
|
||
| $this->assertSame(0, $this->countRows()); | ||
| } | ||
|
|
||
| public function testRollBackWithoutOpenTransactionKeepsTheNextCommitIntact(): void | ||
| { | ||
| $this->connection->rollBack(); | ||
|
|
||
| $this->connection->beginTransaction(); | ||
| $this->connection->beginTransaction(); | ||
| $this->insertRow(); | ||
| $this->connection->commit(); | ||
| $this->connection->commit(); | ||
|
|
||
| $this->assertSame(1, $this->countRows()); | ||
| } | ||
|
|
||
| public function testCommitWithoutOpenTransactionLogsAnErrorWithTheCaller(): void | ||
| { | ||
| $this->connection->commit(); | ||
|
|
||
| $this->assertLogEntryPointsTo('error', '/^Commit without an open transaction/', __FUNCTION__); | ||
| } | ||
|
|
||
| public function testRollBackWithoutOpenTransactionLogsAWarningWithTheCaller(): void | ||
| { | ||
| $this->connection->rollBack(); | ||
|
|
||
| $this->assertLogEntryPointsTo('warning', '/^Rollback without an open transaction/', __FUNCTION__); | ||
| } | ||
|
|
||
| public function testTransactionalDoesNotRollBackAfterItsOwnCommitFailed(): void | ||
| { | ||
| try { | ||
| $this->connection->transactional(function (): void { | ||
| $this->connection->beginTransaction(); | ||
| $this->connection->rollBack(); | ||
| }); | ||
| $this->fail('The nested rollback must make the commit fail.'); | ||
| } catch (CommitException) { | ||
| } | ||
|
|
||
| $this->assertSame([], $this->getLogEntries('warning')); | ||
| } | ||
|
|
||
| public function testFailingDriverRollbackOfADirtyCommitKeepsTheNextCommitIntact(): void | ||
| { | ||
| $this->connection->beginTransaction(); | ||
| $this->connection->beginTransaction(); | ||
| $this->connection->rollBack(); | ||
| // Ends the real transaction behind the connection's back, so that SQLite rejects the driver rollback | ||
| // of the dirty commit below. That rejected rollback is the case this test covers. | ||
| $this->connection->_pQuery('COMMIT'); | ||
|
|
||
| // The SQLite3 extension also raises a PHP warning for the rejected statement. | ||
| set_error_handler(static fn (): bool => true, E_WARNING); | ||
|
|
||
| try { | ||
| $this->connection->commit(); | ||
| } catch (Exception) { | ||
| // Depending on the driver, the rejected rollback or the CommitException ends the commit. | ||
| } finally { | ||
| restore_error_handler(); | ||
| } | ||
|
|
||
| $this->connection->beginTransaction(); | ||
| $this->insertRow(); | ||
| $this->connection->commit(); | ||
|
|
||
| $this->assertSame(1, $this->countRows()); | ||
| } | ||
|
|
||
| private function assertLogEntryPointsTo(string $level, string $messagePattern, string $caller): void | ||
| { | ||
| $entries = $this->getLogEntries($level); | ||
|
|
||
| $this->assertCount(1, $entries); | ||
| $this->assertMatchesRegularExpression($messagePattern, $entries[0]['message']); | ||
| $this->assertIsString($entries[0]['context']['trace'] ?? null); | ||
| $this->assertStringContainsString($caller, $entries[0]['context']['trace']); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<array{level: mixed, message: string, context: array<mixed>}> | ||
| */ | ||
| private function getLogEntries(string $level): array | ||
| { | ||
| return array_values(array_filter( | ||
| self::getLogger()->records, | ||
| static fn (array $record): bool => $record['level'] === $level, | ||
| )); | ||
| } | ||
|
|
||
| private function insertRow(): void | ||
| { | ||
| $this->connection->insert(self::TABLE_NAME, ['temp_id' => $this->generateSystemid()]); | ||
| } | ||
|
|
||
| private function countRows(): int | ||
| { | ||
| $count = $this->connection->fetchOne('SELECT COUNT(*) FROM ' . self::TABLE_NAME); | ||
| $this->assertIsNumeric($count); | ||
|
|
||
| return (int) $count; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Artemeon Core - Web Application Framework. | ||
| * | ||
| * (c) Artemeon <www.artemeon.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Artemeon\Database\Tests; | ||
|
|
||
| use Override; | ||
| use Psr\Log\AbstractLogger; | ||
| use Stringable; | ||
|
|
||
| final class RecordingLogger extends AbstractLogger | ||
| { | ||
| /** @var list<array{level: mixed, message: string, context: array<mixed>}> */ | ||
| public array $records = []; | ||
|
|
||
| /** | ||
| * @param array<mixed> $context | ||
| */ | ||
| #[Override] | ||
| public function log(mixed $level, string | Stringable $message, array $context = []): void | ||
| { | ||
| $this->records[] = ['level' => $level, 'message' => (string) $message, 'context' => $context]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aber haben wir in der
transactionalnicht allg. das Problem, dass hier sowas wie dieRedirectExceptionnicht erkannt wird, bei einerRedirectExceptionmüsste ja eincommitpassieren, im AGP haben wir das an den meisten stellen. Man kann natürlich auch hinterfragen, ob dieRedirectExceptioneine gute Idee ist, aber das würde hier dann auch ein rollback und kein commit erzeugen.Wir bräuchten im DB Layer sonst ein marker interface was man bei der
RedirectExceptionimplementieren kann und was dann hier auch verwendet wird.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, aber ich glaube, das ist eigentlich ein eigenes Thema und evtl. möchte Roman da auch noch was zu sagen.
Das Rollback bei einer RedirectException im Callback gab es in transactional() ja auch schon vorher, der PR ändert daran nichts.
Beim Marker-Interface bin ich mir noch unsicher, weil das DB-Paket dann wissen müsste, dass eine Exception auch "Erfolg" bedeuten kann.
Ich schlage vor dafür einfach ein eigenes Ticket bzw. einen eigenen PR aufzumachen und dort zu klären, wie mit dem Redirect umgegangen wird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Das eigentliche Problem ist doch, dass das AGP das falsch handhabt.
Es konnte sich ja im Grunde nur deshalb einschleichen weil das hier nicht laut kracht (wie es eigentlihc sein sollte, wie bei anderen ORM), sondern silent versucht wird irgnedwie drumrum zu arbeiten.
Sollten wir nicht darauf hinarbeiten dass das AGP gefixt wird, anstatt hier das stille Schlucken der Fehler noch weiter zu manifestieren?
Das führt ja auch zu nicht wirklcih gut vorhersehbarem Verhalten am Ende.
Vorschlag:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ja, sehe ich eigentlich genauso, das Ignorieren soll ja nur der Übergang sein.
Ich habe den commit() ohne offene Transaktion jetzt als Error geloggt, dann taucht das auch auf und man kann die Stellen in core-ng nach und nach abarbeiten.
Wenn da nichts mehr kommt, lassen wir commit() richtig failen.
Den rollBack() habe ich erstmal als Warning gelassen, weil der nach einem fehlgeschlagenen Commit ja auch an korrekten Stellen auftritt.
Bevor der eine Exception wirft, müssten die Stellen mit try/commit/catch/rollBack halt erstmal auf transactional() umgestellt werden, aber da sind wir noch nciht.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@romankonz @micharoesler bin auch bei euch, dass das im AGP korrekt laufen muss, aber dafür ist ja auch das
transactionaldass der count immer clean ist. Das Problem mit derRedirectExceptionist aber ja noch separat, also im AGP wirdtransactionalverwendet, in dem Closure wird AGP Code aufgerufen, der wirft eineRedirectExceptionund dann gibts ein Rollback anstelle von einem Commit. Das ist auf jeden Fall ein Verhalten was wir fixen sollten.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ja, da hast du recht, das sollten wir auf jeden Fall fixen.
Ich würde das aber eigentlich im AGP lösen und nicht in der Library.
Wir haben ja schon eine eigene Database-Klasse, die von Connection erbt, da könnten wir transactional() überschreiben und bei einer RedirectException committen statt zurückrollen.
Dann greift das automatisch überall und die Library muss nichts von Redirects wissen.
Ich würde dafür ein eigenes Ticket bzw. einen eigenen PR aufmachen, aktuell ist auch keine Stelle betroffen, das hat sich Claude angeschaut.
Wäre das für dich ok, dann könnten wir den PR hier abschließen?