-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep SQLite AI incremented after rollback (#1167)
- Loading branch information
Showing
6 changed files
with
189 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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
154 changes: 154 additions & 0 deletions
154
src/Persistence/Sql/Sqlite/PreserveAutoincrementOnRollbackConnectionMiddleware.php
This file contains 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,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Persistence\Sql\Sqlite; | ||
|
||
use Atk4\Data\Persistence\Sql\Exception; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; | ||
|
||
class PreserveAutoincrementOnRollbackConnectionMiddleware extends AbstractConnectionMiddleware | ||
{ | ||
private static string $libraryVersion; | ||
|
||
private function createExpressionFromStringLiteral(string $value): Expression | ||
{ | ||
return new Expression('\'' . str_replace('\'', '\'\'', $value) . '\''); | ||
} | ||
|
||
/** | ||
* @return array<string, array<string, int>> | ||
*/ | ||
protected function listSequences(): array | ||
{ | ||
if ((self::$libraryVersion ?? null) === null) { | ||
$getLibraryVersionSql = (new Query()) | ||
->field('sqlite_version()') | ||
->render()[0]; | ||
self::$libraryVersion = $this->query($getLibraryVersionSql)->fetchOne(); | ||
} | ||
|
||
if (version_compare(self::$libraryVersion, '3.37') < 0) { | ||
$listAllSchemasSql = (new Query()) | ||
->table('pragma_database_list') | ||
->field('name') | ||
->render()[0]; | ||
$allSchemas = $this->query($listAllSchemasSql)->fetchFirstColumn(); | ||
|
||
$schemas = []; | ||
foreach ($allSchemas as $schema) { | ||
$dummySelectFromSqliteSequenceTableSql = (new Query()) | ||
->table($schema . '.sqlite_sequence') | ||
->field('name') | ||
->render()[0]; | ||
try { | ||
$this->query($dummySelectFromSqliteSequenceTableSql)->fetchFirstColumn(); | ||
$schemas[] = $schema; | ||
} catch (\Exception $e) { | ||
while ($e->getPrevious() !== null) { | ||
$e = $e->getPrevious(); | ||
} | ||
|
||
if (!str_contains($e->getMessage(), 'HY000') | ||
|| !str_contains($e->getMessage(), 'no such table: ' . $schema . '.sqlite_sequence') | ||
) { | ||
throw $e; | ||
} | ||
} | ||
} | ||
} else { | ||
$listSchemasSql = (new Query()) | ||
->table('pragma_table_list') | ||
->field('schema') | ||
->where('name', $this->createExpressionFromStringLiteral('sqlite_sequence')) | ||
->render()[0]; | ||
$schemas = $this->query($listSchemasSql)->fetchFirstColumn(); | ||
} | ||
|
||
$res = []; | ||
if ($schemas !== []) { | ||
$listSequencesSql = implode("\nUNION ALL\n", array_map(function (string $schema) { | ||
return (new Query()) | ||
->table($schema . '.sqlite_sequence') | ||
->field($this->createExpressionFromStringLiteral($schema), 'schema') | ||
->field('name') | ||
->field('seq', 'value') | ||
->render()[0]; | ||
}, $schemas)); | ||
|
||
$res = []; | ||
foreach ($this->query($listSequencesSql)->fetchAllAssociative() as $row) { | ||
$value = (int) $row['value']; | ||
if (!is_int($row['value']) && (string) $value !== $row['value']) { | ||
throw (new Exception('Unexpected SQLite sequence value')) | ||
->addMoreInfo('value', $row['value']); | ||
} | ||
|
||
$res[$row['schema']][$row['name']] = $value; | ||
} | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
/** | ||
* @param array<string, array<string, int>> $beforeRollbackSequences | ||
*/ | ||
protected function restoreSequencesIfDecremented(array $beforeRollbackSequences): void | ||
{ | ||
$afterRollbackSequences = $this->listSequences(); | ||
|
||
foreach ($beforeRollbackSequences as $schema => $beforeRollbackSequences2) { | ||
foreach ($beforeRollbackSequences2 as $table => $beforeRollbackValue) { | ||
$afterRollbackValue = $afterRollbackSequences[$schema][$table] ?? null; | ||
if ($afterRollbackValue >= $beforeRollbackValue) { | ||
continue; | ||
} | ||
|
||
if ($afterRollbackValue === null) { // https://sqlite.org/forum/info/3e7cc380f0a159c6 | ||
$query = (new Query()) | ||
->mode('insert') | ||
->set('name', $this->createExpressionFromStringLiteral($table)); | ||
} else { | ||
$query = (new Query()) | ||
->mode('update') | ||
->where('name', $this->createExpressionFromStringLiteral($table)); | ||
} | ||
$query->table($schema . '.sqlite_sequence'); | ||
$query->set('seq', $this->createExpressionFromStringLiteral((string) $beforeRollbackValue)); | ||
|
||
$this->exec($query->render()[0]); | ||
} | ||
} | ||
} | ||
|
||
#[\Override] | ||
public function exec(string $sql): int | ||
{ | ||
$isRollback = str_starts_with(strtoupper(ltrim($sql)), 'ROLLBACK '); | ||
|
||
if ($isRollback) { | ||
$beforeRollbackSequences = $this->listSequences(); | ||
} | ||
|
||
$res = parent::exec($sql); | ||
|
||
if ($isRollback) { | ||
$this->restoreSequencesIfDecremented($beforeRollbackSequences); | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
#[\Override] | ||
public function rollBack() | ||
{ | ||
$beforeRollbackSequences = $this->listSequences(); | ||
|
||
$res = parent::rollBack(); | ||
|
||
$this->restoreSequencesIfDecremented($beforeRollbackSequences); | ||
|
||
return $res; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Persistence/Sql/Sqlite/PreserveAutoincrementOnRollbackMiddleware.php
This file contains 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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Persistence\Sql\Sqlite; | ||
|
||
use Doctrine\DBAL\Driver; | ||
use Doctrine\DBAL\Driver\Connection; | ||
use Doctrine\DBAL\Driver\Middleware; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; | ||
|
||
class PreserveAutoincrementOnRollbackMiddleware implements Middleware | ||
{ | ||
#[\Override] | ||
public function wrap(Driver $driver): Driver | ||
{ | ||
return new class($driver) extends AbstractDriverMiddleware { | ||
#[\Override] | ||
public function connect( | ||
#[\SensitiveParameter] | ||
array $params | ||
): Connection { | ||
return new PreserveAutoincrementOnRollbackConnectionMiddleware(parent::connect($params)); | ||
} | ||
}; | ||
} | ||
} |
This file contains 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