Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Modify schema update to now simply delete the whole db #197

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
13 changes: 6 additions & 7 deletions src/Knp/FriendlyContexts/Context/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Knp\FriendlyContexts\Context;

use Behat\Gherkin\Node\TableNode;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\PropertyAccess\PropertyAccess;
Expand Down Expand Up @@ -189,14 +190,12 @@ public function beforeScenario($event)
$this->storeTags($event);

if ($this->hasTags([ 'reset-schema', '~not-reset-schema' ])) {
foreach ($this->getEntityManagers() as $entityManager) {
$metadata = $this->getMetadata($entityManager);
$purger = new ORMPurger();
$purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this PR, we could keep it like that. But it could be interesting to add a new PR to enable PURGE_MODE_TRUNCATE depending on database system, it's more performant :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed performance issue using truncate with mysql. I guess it depends on the schema ! But well, it should be at least configurable. You're right.


if (!empty($metadata)) {
$tool = new SchemaTool($entityManager);
$tool->dropSchema($metadata);
$tool->createSchema($metadata);
}
foreach ($this->getEntityManagers() as $entityManager) {
$purger->setEntityManager($entityManager);
$purger->purge();
Copy link
Contributor

Choose a reason for hiding this comment

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

This purger requires to build schema manually before running tests. To prevent this, you can reset schema once, maybe something like:

/**
 * @var bool
 */
private $databaseReady = false;

// ...

if (!$this->databaseReady) {
    $metadata = $entityManager->getMetadataFactory()->getAllMetadata();
    $tool = new SchemaTool($this->entityManager);
    $tool->dropSchema($metadata);
    $tool->createSchema($metadata);
    $this->databaseReady = true;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand it's a bcbreak. But IMO the good thing to do is to build schema in a different task than purging the database.

It's possible the database is already good when you run your tests on your computer. In this case rebuilding the database is not desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please note also that your change is a bc break too as it suggest the database is build only once, and if the test needs a real schema reset the second time, it will not be executed.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK so in that case, please remove useless use Doctrine\ORM\Tools\SchemaTool; 😉

}
}

Expand Down