Repository navigation
[TASK] Skip unchanged PostgreSQL tables during database reset #758
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,197 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TYPO3\TestingFramework\Tests\Functional; | ||
|
|
||
| /* | ||
| * 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! | ||
| */ | ||
|
|
||
| use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use TYPO3\CMS\Core\Database\ConnectionPool; | ||
| use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; | ||
| use TYPO3\TestingFramework\Core\Testbase; | ||
|
|
||
| final class PostgreSqlDatabaseResetTest 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 review summary: nothing in this repository executes |
||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
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. Please drop the manual platform check in #[Group('not-mariadb')]
#[Group('not-mysql')]
#[Group('not-sqlite')]
final class PostgreSqlDatabaseResetTest extends FunctionalTestCaseOnce a functional suite gets added to this repository, it should follow that same convention, so tests here look and behave like Core tests. As a side effect this also avoids the current cost: With the check gone, the |
||
| if (!$this->get(ConnectionPool::class)->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME)->getDatabasePlatform() instanceof PostgreSQLPlatform) { | ||
| self::markTestSkipped('PostgreSQL database reset behavior'); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function resetLeavesUnusedTablesUntouched(): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_unused (value INT)'); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_used (value INT)'); | ||
| try { | ||
| $connection->executeStatement('INSERT INTO tx_reset_used VALUES (1)'); | ||
| $unusedFile = $connection->executeQuery("SELECT pg_relation_filenode('tx_reset_unused')")->fetchOne(); | ||
| $usedFile = $connection->executeQuery("SELECT pg_relation_filenode('tx_reset_used')")->fetchOne(); | ||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame($unusedFile, $connection->executeQuery("SELECT pg_relation_filenode('tx_reset_unused')")->fetchOne()); | ||
| self::assertNotSame($usedFile, $connection->executeQuery("SELECT pg_relation_filenode('tx_reset_used')")->fetchOne()); | ||
| self::assertSame(0, $connection->count('*', 'tx_reset_used', [])); | ||
| } finally { | ||
| $connection->executeStatement('DROP TABLE tx_reset_unused, tx_reset_used'); | ||
| } | ||
| } | ||
|
|
||
| public static function sequenceStateDataProvider(): iterable | ||
| { | ||
| yield 'unused sequence' => ['uid', [], 1]; | ||
| yield 'populated table' => ['uid', ['INSERT INTO tx_reset_sequence DEFAULT VALUES'], 1]; | ||
| yield 'deleted row with consumed ID' => ['uid', ['INSERT INTO tx_reset_sequence DEFAULT VALUES', 'DELETE FROM tx_reset_sequence'], 1]; | ||
| yield 'explicit ID without sequence advance' => ['uid', ['INSERT INTO tx_reset_sequence (uid) VALUES (400)'], 1]; | ||
| yield 'changed uncalled sequence' => ['uid', ["SELECT setval('tx_reset_sequence_uid_seq', 42, false)"], 1]; | ||
| yield 'custom column and start' => ['record_id', ['ALTER SEQUENCE tx_reset_sequence_record_id_seq START WITH 13'], 13]; | ||
| yield 'descending sequence' => ['uid', ['ALTER SEQUENCE tx_reset_sequence_uid_seq MINVALUE -10 MAXVALUE 10 INCREMENT BY -1 START WITH -2 RESTART WITH -2', "SELECT nextval('tx_reset_sequence_uid_seq')"], -2]; | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DataProvider('sequenceStateDataProvider')] | ||
| public function resetRestoresSequenceStart(string $column, array $statements, int $expectedId): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_sequence (' . $connection->quoteIdentifier($column) . ' SERIAL PRIMARY KEY)'); | ||
| try { | ||
| foreach ($statements as $statement) { | ||
| $connection->executeStatement($statement); | ||
| } | ||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame(0, $connection->count('*', 'tx_reset_sequence', [])); | ||
| self::assertSame($expectedId, $connection->executeQuery('INSERT INTO tx_reset_sequence DEFAULT VALUES RETURNING ' . $connection->quoteIdentifier($column))->fetchOne()); | ||
| } finally { | ||
| $connection->executeStatement('DROP TABLE tx_reset_sequence'); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function resetRestoresMultipleIdentityColumns(): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_identity (id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 5), other BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 9))'); | ||
| try { | ||
| $connection->executeStatement('INSERT INTO tx_reset_identity DEFAULT VALUES'); | ||
| $connection->executeStatement('DELETE FROM tx_reset_identity'); | ||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame(['id' => 5, 'other' => 9], $connection->executeQuery('INSERT INTO tx_reset_identity DEFAULT VALUES RETURNING id, other')->fetchAssociative()); | ||
| } finally { | ||
| $connection->executeStatement('DROP TABLE tx_reset_identity'); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function resetRestoresSequencesWithAnotherCurrentSchema(): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $searchPath = $connection->executeQuery('SHOW search_path')->fetchOne(); | ||
| $connection->executeStatement('CREATE SCHEMA tx_reset_schema'); | ||
| $connection->executeStatement('CREATE TABLE public.tx_reset_public (uid SERIAL PRIMARY KEY)'); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_schema.tx_reset_other (uid SERIAL PRIMARY KEY)'); | ||
| try { | ||
| $connection->executeStatement('INSERT INTO public.tx_reset_public DEFAULT VALUES'); | ||
| $connection->executeStatement('DELETE FROM public.tx_reset_public'); | ||
| $connection->executeStatement('INSERT INTO tx_reset_schema.tx_reset_other DEFAULT VALUES'); | ||
| $connection->executeStatement('DELETE FROM tx_reset_schema.tx_reset_other'); | ||
| $connection->executeStatement('SET search_path TO tx_reset_schema, public'); | ||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame(1, $connection->executeQuery('INSERT INTO public.tx_reset_public DEFAULT VALUES RETURNING uid')->fetchOne()); | ||
| self::assertSame(1, $connection->executeQuery('INSERT INTO tx_reset_schema.tx_reset_other DEFAULT VALUES RETURNING uid')->fetchOne()); | ||
| } finally { | ||
| $connection->executeQuery("SELECT set_config('search_path', ?, false)", [$searchPath]); | ||
| $connection->executeStatement('DROP TABLE public.tx_reset_public'); | ||
| $connection->executeStatement('DROP SCHEMA tx_reset_schema CASCADE'); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function resetChecksMoreThanOneBatchOfTables(): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $tables = []; | ||
| try { | ||
| for ($index = 0; $index <= 100; ++$index) { | ||
| $table = sprintf('tx_reset_batch_%03d', $index); | ||
| $connection->executeStatement('CREATE TABLE ' . $table . ' (value INT)'); | ||
| $tables[] = $table; | ||
| } | ||
| $connection->executeStatement('INSERT INTO tx_reset_batch_100 VALUES (1)'); | ||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame(0, $connection->count('*', 'tx_reset_batch_100', [])); | ||
| } finally { | ||
| if ($tables !== []) { | ||
| $connection->executeStatement('DROP TABLE ' . implode(', ', $tables)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function resetFiresTruncateTriggersOnEmptyTables(): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_trigger (uid INT)'); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_audit (message TEXT)'); | ||
| $connection->executeStatement("CREATE FUNCTION tx_reset_log() RETURNS TRIGGER AS $$ BEGIN INSERT INTO tx_reset_audit VALUES ('fired'); RETURN NULL; END $$ LANGUAGE plpgsql"); | ||
| try { | ||
| $connection->executeStatement('CREATE TRIGGER tx_reset_truncated AFTER TRUNCATE ON tx_reset_trigger FOR EACH STATEMENT EXECUTE FUNCTION tx_reset_log()'); | ||
|
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.
|
||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame(['fired'], $connection->select(['message'], 'tx_reset_audit', [])->fetchFirstColumn()); | ||
| } finally { | ||
| $connection->executeStatement('DROP TABLE tx_reset_trigger, tx_reset_audit'); | ||
| $connection->executeStatement('DROP FUNCTION tx_reset_log()'); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function resetPreservesForeignKeyCleanupAndResetsUntouchedTables(): void | ||
| { | ||
| $connection = $this->getConnectionPool()->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_parent (uid SERIAL PRIMARY KEY)'); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_child (uid SERIAL PRIMARY KEY, pid INT REFERENCES tx_reset_parent(uid))'); | ||
| $connection->executeStatement('CREATE TABLE tx_reset_unused (value INT)'); | ||
| try { | ||
| $connection->executeStatement('INSERT INTO tx_reset_parent DEFAULT VALUES'); | ||
| $connection->executeStatement('INSERT INTO tx_reset_child (pid) VALUES (1)'); | ||
| $unusedFile = $connection->executeQuery("SELECT pg_relation_filenode('tx_reset_unused')")->fetchOne(); | ||
|
|
||
| (new Testbase())->initializeTestDatabaseAndTruncateTables(); | ||
|
|
||
| self::assertSame(0, $connection->count('*', 'tx_reset_parent', [])); | ||
| self::assertSame(0, $connection->count('*', 'tx_reset_child', [])); | ||
| self::assertNotSame($unusedFile, $connection->executeQuery("SELECT pg_relation_filenode('tx_reset_unused')")->fetchOne()); | ||
| self::assertSame(1, $connection->executeQuery('INSERT INTO tx_reset_parent DEFAULT VALUES RETURNING uid')->fetchOne()); | ||
| self::assertSame(1, $connection->executeQuery('INSERT INTO tx_reset_child (pid) VALUES (1) RETURNING uid')->fetchOne()); | ||
| } finally { | ||
| $connection->executeStatement('DROP TABLE tx_reset_child, tx_reset_parent, tx_reset_unused'); | ||
| } | ||
| } | ||
| } | ||
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.
Nit: this key has to match the format
listTableNames()returns (PostgreSQLSchemaManager::selectTableNames()+_getPortableTableDefinition():quote_ident(table_name), raw schema name, prefix only outsidecurrent_schema()). It does today, but if the two ever drift apart the sequence checks drop out silently and an empty table with a consumed sequence is no longer reset. Could you add a short comment saying the key mirrors the DBAL format?