Skip to content
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

Respect schema asset filters from DBAL 2.9+ #312

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 25 additions & 6 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use function array_search;
use function is_callable;
use function preg_match;

/**
* Class responsible for purging databases of data before reloading data fixtures.
Expand Down Expand Up @@ -126,13 +129,29 @@ public function purge()
$connection = $this->em->getConnection();
$filterExpr = $connection->getConfiguration()->getFilterSchemaAssetsExpression();
$emptyFilterExpression = empty($filterExpr);

$schemaAssetsFilter = method_exists($connection->getConfiguration(), 'getSchemaAssetsFilter') ? $connection->getConfiguration()->getSchemaAssetsFilter() : null;

foreach($orderedTables as $tbl) {
if(($emptyFilterExpression||preg_match($filterExpr, $tbl)) && array_search($tbl, $this->excluded) === false){
if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$connection->executeUpdate("DELETE FROM " . $tbl);
} else {
$connection->executeUpdate($platform->getTruncateTableSQL($tbl, true));
}
// If we have a filter expression, check it and skip if necessary
if (!$emptyFilterExpression && !preg_match($filterExpr, $tbl)) {
continue;
}

// If the table is excluded, skip it as well
if (array_search($tbl, $this->excluded) !== false) {
continue;
}

// Support schema asset filters as presented in
if (is_callable($schemaAssetsFilter) && !$schemaAssetsFilter($tbl)) {
continue;
}

if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$connection->executeUpdate("DELETE FROM " . $tbl);
} else {
$connection->executeUpdate($platform->getTruncateTableSQL($tbl, true));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\ORM\Tools\Setup;
use Doctrine\Tests\Common\DataFixtures\TestPurgeEntity\ExcludedEntity;
use Doctrine\Tests\Common\DataFixtures\TestPurgeEntity\IncludedEntity;
use function method_exists;

class ORMPurgerExcludeTest extends BaseTest
{
Expand Down Expand Up @@ -59,7 +60,7 @@ protected function loadTestData(){
* @param string|null $expression
* @param array $list
*/
public function executeTestPurge($expression, array $list){
public function executeTestPurge($expression, array $list, ?callable $filter = null){
$em = $this->loadTestData();
$excludedRepository = $em->getRepository(self::TEST_ENTITY_EXCLUDED);
$includedRepository = $em->getRepository(self::TEST_ENTITY_INCLUDED);
Expand All @@ -74,6 +75,14 @@ public function executeTestPurge($expression, array $list){
$configuration = $connection->getConfiguration();
$configuration->setFilterSchemaAssetsExpression($expression);

if ($filter !== null) {
if (!method_exists($configuration, 'setSchemaAssetsFilter')) {
$this->markTestSkipped('DBAL 2.9 or newer is required to test schema assets filters');
}

$configuration->setSchemaAssetsFilter($filter);
}

$purger = new ORMPurger($em,$list);
$purger->purge();

Expand All @@ -88,13 +97,20 @@ public function executeTestPurge($expression, array $list){
* Test for purge exclusion usig dbal filter expression regexp.
*/
public function testPurgeExcludeUsingFilterExpression(){
$this->executeTestPurge('~^(?!ExcludedEntity)~', []);
$this->executeTestPurge('~^(?!ExcludedEntity)~', [], null);
}

/**
* Test for purge exclusion usig explicit exclution list.
*/
public function testPurgeExcludeUsingList(){
$this->executeTestPurge(null, ['ExcludedEntity']);
$this->executeTestPurge(null, ['ExcludedEntity'], null);
}

public function testPurgeExcludeUsingFilterCallable() : void
{
$this->executeTestPurge(null, [], static function (string $table) : bool {
return preg_match('~^(?!ExcludedEntity)~', $table);
});
}
}