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

Add ability to exclude tables from purge in ORM #225

Merged
merged 11 commits into from
Jul 5, 2016
25 changes: 19 additions & 6 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ class ORMPurger implements PurgerInterface
*/
private $purgeMode = self::PURGE_MODE_DELETE;

/**
* Table/view names to be excleded from purge
*
* @var string[]
*/
private $excluded;

/**
* Construct new purger instance.
*
* @param EntityManagerInterface $em EntityManagerInterface instance used for persistence.
* @param string[] $excluded array of table/view names to be excleded from purge
*/
public function __construct(EntityManagerInterface $em = null)
public function __construct(EntityManagerInterface $em = null, array $excluded = array())
{
$this->em = $em;
$this->excluded = $excluded;
}

/**
Expand Down Expand Up @@ -133,11 +142,15 @@ public function purge()
}

$connection = $this->em->getConnection();
$filterExpr = $connection->getConfiguration()->getFilterSchemaAssetsExpression();
$efe = empty($filterExpr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does efe mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty filter expression

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not abbreviate then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i changed the variable name and added tests for exclusion using the filter expression or the explicit exclusion list.

foreach($orderedTables as $tbl) {
if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$connection->executeUpdate('DELETE FROM ' . $tbl);
} else {
$connection->executeUpdate($platform->getTruncateTableSQL($tbl, true));
if(($efe||( !$efe && 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));
}
}
}
}
Expand Down Expand Up @@ -238,4 +251,4 @@ private function getJoinTableName($assoc, $class, $platform)

return $this->em->getConfiguration()->getQuoteStrategy()->getJoinTableName($assoc, $class, $platform);
}
}
}