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

Simplified logic for baseline cache sqlite file, easier parallel execution support #1

Merged
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
98 changes: 70 additions & 28 deletions src/Zorbus/Behat/Context/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\Tools\SchemaTool;
use Behat\Gherkin\Node\TableNode;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Exception;
use PDOException;
use RuntimeException;
use Exception;

class DoctrineContext implements Context, SnippetAcceptingContext
{
private static $database = null;
/**
* @var Connection
*/
private $connection;

/**
* @var EntityManager
*/
private $entityManager;

public function __construct(Connection $connection, EntityManager $entityManager)
/**
* @var string
*/
private $cacheDir;

/**
* @param Connection $connection
* @param EntityManager $entityManager
* @param string|null $cacheDir
*/
public function __construct(Connection $connection, EntityManager $entityManager, $cacheDir = null)
{
$this->connection = $connection;
$this->entityManager = $entityManager;
$this->cacheDir = $cacheDir;
}

/**
Expand Down Expand Up @@ -186,28 +203,28 @@ public function theFixtureIsAppended($file)

private function buildDatabase()
{
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$path = $this->connection->getDatabase();
$copy = $path.'.copy';
if ($this->cacheDir && $this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$cachePath = $this->createSqliteCachePath($this->connection->getDatabase());

if (null !== self::$database) {
if (copy($copy, $path)) {
return;
}
}
}
if (file_exists($cachePath)) {
copy($cachePath, $this->connection->getDatabase());

$this->connection->getSchemaManager()->dropAndCreateDatabase($this->connection->getDatabase());
$this->selectDatabase();
$queries = $this->dumpStructure();
foreach ($queries as $query) {
$this->connection->query($query);
}
return;
} else {
$this->dropCreateAndSchemaUp();

$cacheDir = dirname($cachePath);

if (!is_dir($cacheDir)) {
if (!@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
throw new RuntimeException('Failed to create SQLite cache directory: '.$cacheDir);
}
}

if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
if (copy($path, $copy)) {
self::$database = 'sqlite';
copy($this->connection->getDatabase(), $cachePath);
}
} else {
$this->dropCreateAndSchemaUp();
}
}

Expand All @@ -228,7 +245,7 @@ private function dropDatabase()
/**
* Runs the symfony equivalent command doctrine:schema:update --dump-sql
*
* @return string
* @return string[]
*/
private function dumpStructure()
{
Expand Down Expand Up @@ -275,4 +292,29 @@ private function selectDatabase()
} catch (Exception $e) {
}
}

/**
* @param string $databasePath
*
* @return string
*/
private function createSqliteCachePath($databasePath)
{
return $this->cacheDir.DIRECTORY_SEPARATOR.(new \SplFileInfo($databasePath))->getFilename().'.cache';
}

/**
* Drops, creates the database and builds the schema.
*/
private function dropCreateAndSchemaUp()
{
$this->connection->getSchemaManager()->dropAndCreateDatabase($this->connection->getDatabase());
$this->selectDatabase();

$queries = $this->dumpStructure();

foreach ($queries as $query) {
$this->connection->exec($query);
}
}
}