Repository navigation
[TASK] Reuse sequence metadata in database snapshots #757
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the TYPO3 CMS project. | ||
| * | ||
| * It is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License, either version 2 | ||
| * of the License, or any later version. | ||
| * | ||
| * For the full copyright and license information, please read the | ||
| * LICENSE.txt file that was distributed with this source code. | ||
| * | ||
| * The TYPO3 project - inspiring people to share! | ||
| */ | ||
|
|
||
| namespace TYPO3\TestingFramework\Tests\Functional; | ||
|
|
||
| use Doctrine\DBAL\Schema\Table; | ||
| use Doctrine\DBAL\Types\Types; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use TYPO3\CMS\Core\Database\ConnectionPool; | ||
| use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Snapshot\DatabaseAccessor; | ||
| use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; | ||
|
|
||
| final class DatabaseAccessorSequenceTest extends FunctionalTestCase | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the summary: nothing in this repository executes |
||
| { | ||
| public static function autoIncrementColumnsDataProvider(): iterable | ||
| { | ||
| yield 'uid' => ['uid', 'uid']; | ||
| yield 'custom column' => ['record_id', 'record_id']; | ||
| yield 'quoted mixed-case column' => ['entryId', '"entryId"']; | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DataProvider('autoIncrementColumnsDataProvider')] | ||
| public function snapshotRestoresNextGeneratedId(string $columnName, string $schemaColumnName): void | ||
| { | ||
| $connection = $this->get(ConnectionPool::class)->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $schemaManager = $connection->createSchemaManager(); | ||
| $table = new Table('tx_testingframework_sequence'); | ||
| $table->addColumn($schemaColumnName, Types::INTEGER, ['autoincrement' => true]); | ||
| $table->addColumn('title', Types::STRING, ['length' => 255]); | ||
| $table->setPrimaryKey([$schemaColumnName]); | ||
| $schemaManager->createTable($table); | ||
|
|
||
| try { | ||
| $connection->insert($table->getName(), [$columnName => 7, 'title' => 'first']); | ||
| $connection->insert($table->getName(), [$columnName => 11, 'title' => 'last']); | ||
| $subject = new DatabaseAccessor($connection); | ||
| $snapshot = json_decode(json_encode($subject->export(), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
|
||
| for ($iteration = 0; $iteration < 2; ++$iteration) { | ||
| $connection->truncate($table->getName()); | ||
| $subject->import($snapshot); | ||
| $connection->insert($table->getName(), ['title' => 'generated']); | ||
|
|
||
| self::assertSame(12, (int)$connection->select([$columnName], $table->getName(), ['title' => 'generated'])->fetchOne()); | ||
| self::assertSame(3, $connection->count('*', $table->getName(), [])); | ||
| } | ||
| } finally { | ||
| $schemaManager->dropTable($table->getName()); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function snapshotRestoresTableWithoutAutoIncrementColumn(): void | ||
| { | ||
| $connection = $this->get(ConnectionPool::class)->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $schemaManager = $connection->createSchemaManager(); | ||
| $table = new Table('tx_testingframework_no_sequence'); | ||
| $table->addColumn('identifier', Types::STRING, ['length' => 255]); | ||
| $table->addColumn('title', Types::STRING, ['length' => 255]); | ||
| $table->setPrimaryKey(['identifier']); | ||
| $schemaManager->createTable($table); | ||
|
|
||
| try { | ||
| $connection->insert($table->getName(), ['identifier' => 'example', 'title' => 'preserved']); | ||
| $subject = new DatabaseAccessor($connection); | ||
| $snapshot = json_decode(json_encode($subject->export(), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); | ||
| $connection->truncate($table->getName()); | ||
|
|
||
| $subject->import($snapshot); | ||
|
|
||
| self::assertSame('preserved', $connection->select(['title'], $table->getName(), ['identifier' => 'example'])->fetchOne()); | ||
| } finally { | ||
| $schemaManager->dropTable($table->getName()); | ||
| } | ||
| } | ||
| } | ||
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.
On PostgreSQL
getAutoincrement()is onlytruefor identity columns:'autoincrement' => $tableColumn['attidentity'] === 'd'in DBAL 4.4 and inTYPO3\CMS\Core\Database\Schema\SchemaManager\PostgreSQLSchemaManager. ASERIALcolumn is not detected, soimportTable()skips the reset and the sequence keeps its old position.Reproduced on PostgreSQL 17:
The TYPO3 schema itself uses identity columns since DBAL 4, so the default tables are fine. But tests and extensions creating tables with raw SQL still use
SERIALa lot (the tests in #758 do), and onmainthis works.Suggestion that keeps the gain: on PostgreSQL resolve the owned sequence column once in
export()(which runs once per test case) with one catalog query for all tables, for examplepg_dependwithdeptype IN ('a', 'i')joined topg_attribute. That coversSERIALand identity columns, andimport()stays free of catalog lookups. Please add aSERIALdata set to the test as well.