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

BUGFIX: Dont reset escr tables or ignored tables in general #37

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions Classes/FlowEntitiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Neos\Behat;

use Behat\Hook\BeforeScenario;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception as DoctrineException;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManagerInterface;
use Neos\Flow\Persistence\Doctrine\Service;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Persistence\Doctrine\Service as FlowDoctrineService;
use Neos\Flow\Persistence\PersistenceManagerInterface;

/**
* Tag your test with @ flowEntities to enable support for flow entities
* Tag your test with [at]flowEntities to enable support for flow entities
mhsdesign marked this conversation as resolved.
Show resolved Hide resolved
*
* @api
*/
Expand Down Expand Up @@ -40,14 +41,15 @@ final public function truncateAndSetupFlowEntities(): void
$this->truncateTables($entityManager);
} else {
try {
$doctrineService = $this->getObject(Service::class);
$doctrineService = $this->getObject(FlowDoctrineService::class);

$doctrineService->executeMigrations();
$needsTruncate = true;
} catch (DBALException $exception) {
} catch (DoctrineException $exception) {
// Do an initial teardown to drop the schema cleanly
$this->getObject(PersistenceManagerInterface::class)->tearDown();

$doctrineService = $this->getObject(Service::class);
$doctrineService = $this->getObject(FlowDoctrineService::class);
$doctrineService->executeMigrations();
$needsTruncate = false;
} catch (\PDOException $exception) {
Expand Down Expand Up @@ -82,9 +84,34 @@ private function truncateTables(EntityManagerInterface $entityManager): void
{
$connection = $entityManager->getConnection();

$tables = array_filter(self::$databaseSchema->getTables(), function ($table) {
return $table->getName() !== 'flow_doctrine_migrationstatus';
/**
* We respect flows option "ignoredTables" to preserve certain tables while resetting the database.
* In our case we interpret everything in "ignoredTables" as not managed by doctrine.
* And this trait should only clear tables for @flowEntities.
* An important use case is keeping the Neos ESCR tables `cr_*.` alive for speeding up tests.
*
* Docs for original idea of "ignoredTables": {@link https://flowframework.readthedocs.io/en/9.0/TheDefinitiveGuide/PartIII/Persistence.html#ignoring-tables}
*
* Implementation copied from {@link https://github.com/neos/flow-development-collection/blob/ed6a26603f682966816c71840524c7da6ed919a5/Neos.Flow/Classes/Command/DoctrineCommandController.php#L468-L474}
*/
$ignoredTables = $this->getObject(ConfigurationManager::class)
->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Flow.persistence.doctrine.migrations.ignoredTables') ?? [];
$filterExpression = null;
$ignoredTables = array_keys(array_filter($ignoredTables));
if ($ignoredTables !== []) {
$filterExpression = sprintf('/^(?!%s$).*$/xs', implode('$|', $ignoredTables));
}

$tables = array_filter(self::$databaseSchema->getTables(), function ($table) use ($filterExpression) {
if ($table->getName() === FlowDoctrineService::DOCTRINE_MIGRATIONSTABLENAME) {
return false;
}
if ($filterExpression === null) {
return true;
}
return preg_match($filterExpression, $table->getName()) === 1;
});

switch ($connection->getDatabasePlatform()->getName()) {
case 'mysql':
$sql = 'SET FOREIGN_KEY_CHECKS=0;';
Expand Down