From 4977f6c270f083237eff1df00113dc36779bd3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 18 Jun 2023 22:06:06 +0200 Subject: [PATCH] Reset autoincrements on SQLite with our ResetAutoIncrementPurger too and make it default for fixtures load --- .github/workflows/tests.yml | 2 +- config/services.yaml | 11 ++++++++++- src/Doctrine/Purger/ResetAutoIncrementORMPurger.php | 11 +++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 76137f02..99955059 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -109,7 +109,7 @@ jobs: run: php bin/console --env test doctrine:migrations:migrate -n - name: Load fixtures - run: php bin/console --env test doctrine:fixtures:load -n --purger reset_autoincrement_purger + run: php bin/console --env test doctrine:fixtures:load -n - name: Run PHPunit and generate coverage run: ./bin/phpunit --coverage-clover=coverage.xml diff --git a/config/services.yaml b/config/services.yaml index 26c35cba..6522b99b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -259,7 +259,16 @@ services: tags: - { name: 'doctrine.fixtures.purger_factory', alias: 'reset_autoincrement_purger' } - # We are needing this service inside of a migration, where only the container is injected. So we need to define it as public, to access it from the container. + # Decorate the doctrine fixtures load command to use our custom purger by default + doctrine.fixtures_load_command.custom: + decorates: doctrine.fixtures_load_command + class: Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand + arguments: + - '@doctrine.fixtures.loader' + - '@doctrine' + - { default: '@App\Doctrine\Purger\ResetAutoIncrementPurgerFactory' } + + # We are needing this service inside a migration, where only the container is injected. So we need to define it as public, to access it from the container. App\Services\UserSystem\PermissionPresetsHelper: public: true diff --git a/src/Doctrine/Purger/ResetAutoIncrementORMPurger.php b/src/Doctrine/Purger/ResetAutoIncrementORMPurger.php index db717c3f..84ac806e 100644 --- a/src/Doctrine/Purger/ResetAutoIncrementORMPurger.php +++ b/src/Doctrine/Purger/ResetAutoIncrementORMPurger.php @@ -27,6 +27,7 @@ use Doctrine\Common\DataFixtures\Purger\PurgerInterface; use Doctrine\Common\DataFixtures\Sorter\TopologicalSorter; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Schema\Identifier; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; @@ -188,7 +189,7 @@ class ResetAutoIncrementORMPurger implements PurgerInterface, ORMPurgerInterface } //Reseting autoincrement is only supported on MySQL platforms - if ($platform instanceof AbstractMySQLPlatform) { + if ($platform instanceof AbstractMySQLPlatform || $platform instanceof SqlitePlatform) { $connection->beginTransaction(); $connection->executeQuery($this->getResetAutoIncrementSQL($tbl, $platform)); } @@ -204,7 +205,13 @@ class ResetAutoIncrementORMPurger implements PurgerInterface, ORMPurgerInterface { $tableIdentifier = new Identifier($tableName); - return 'ALTER TABLE '. $tableIdentifier->getQuotedName($platform) .' AUTO_INCREMENT = 1;'; + if ($platform instanceof AbstractMySQLPlatform) { + return 'ALTER TABLE '.$tableIdentifier->getQuotedName($platform).' AUTO_INCREMENT = 1;'; + } + + if ($platform instanceof SqlitePlatform) { + return 'DELETE FROM `sqlite_sequence` WHERE name = \''.$tableIdentifier->getQuotedName($platform).'\';'; + } } /**