-
Notifications
You must be signed in to change notification settings - Fork 180
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
Separate db tool and db backup/restore services for version 2.x #398
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
02e3518
Separate db tool and db backup/restore services for version 2.x
fb40d39
code style fix
alekseytupichenkov c92e7e4
Merge branches '2.x' and 'db_tools_from_2.x' of github.com:alekseytup…
260d01c
Changes from pr #393
5923bed
fixes and MysqlCustomDatabaseBackup boost
66e4390
fix when append fixtures
831b101
cache schema too, it's more predictable
817c8ee
performance update
1a71772
fix typo
6379ac1
refactoring
c355de3
removed cache_sqlite_db from config
7d2d90e
fix mongodb service name
966681b
remove MysqlExampleDatabaseBackup
87d9f1a
Travis trigger
b919a90
small refactoring and fixes
3cde299
Added instruction to upgrade from 1.x to 2.0
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="liip_functional_test.services.fixtures_loader_factory" class="Liip\FunctionalTestBundle\Services\FixturesLoaderFactory" public="true"> | ||
<argument type="service" id="service_container" /> | ||
</service> | ||
|
||
<service id="liip_functional_test.services_database_backup.sqlite" class="Liip\FunctionalTestBundle\Services\DatabaseBackup\SqliteDatabaseBackup" public="true"> | ||
<argument type="service" id="service_container" /> | ||
<argument type="service" id="liip_functional_test.services.fixtures_loader_factory" /> | ||
</service> | ||
|
||
<service id="liip_functional_test.services_database_tools.orm_database_tool" class="Liip\FunctionalTestBundle\Services\DatabaseTools\ORMDatabaseTool" public="false"> | ||
<argument type="service" id="service_container" /> | ||
<argument type="service" id="liip_functional_test.services.fixtures_loader_factory" /> | ||
</service> | ||
<service id="liip_functional_test.services_database_tools.orm_sqlite_database_tool" class="Liip\FunctionalTestBundle\Services\DatabaseTools\ORMSqliteDatabaseTool" public="false"> | ||
<argument type="service" id="service_container" /> | ||
<argument type="service" id="liip_functional_test.services.fixtures_loader_factory" /> | ||
</service> | ||
<service id="liip_functional_test.services_database_tools.mongodb_database_tool" class="Liip\FunctionalTestBundle\Services\DatabaseTools\MongoDBDatabaseTool" public="false"> | ||
<argument type="service" id="service_container" /> | ||
<argument type="service" id="liip_functional_test.services.fixtures_loader_factory" /> | ||
</service> | ||
<service id="liip_functional_test.services_database_tools.phpcr_database_tool" class="Liip\FunctionalTestBundle\Services\DatabaseTools\PHPCRDatabaseTool" public="false"> | ||
<argument type="service" id="service_container" /> | ||
<argument type="service" id="liip_functional_test.services.fixtures_loader_factory" /> | ||
</service> | ||
<service id="liip_functional_test.services.database_tool_collection" class="Liip\FunctionalTestBundle\Services\DatabaseToolCollection" public="true"> | ||
<argument type="service" id="service_container" /> | ||
<argument type="service" id="liip_functional_test.services.fixtures_loader_factory" /> | ||
<call method="add"> | ||
<argument type="service" id="liip_functional_test.services_database_tools.orm_database_tool" /> | ||
</call> | ||
<call method="add"> | ||
<argument type="service" id="liip_functional_test.services_database_tools.orm_sqlite_database_tool" /> | ||
</call> | ||
<call method="add"> | ||
<argument type="service" id="liip_functional_test.services_database_tools.mongodb_database_tool" /> | ||
</call> | ||
<call method="add"> | ||
<argument type="service" id="liip_functional_test.services_database_tools.phpcr_database_tool" /> | ||
</call> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Liip/FunctionalTestBundle | ||
* | ||
* (c) Lukas Kahwe Smith <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Liip\FunctionalTestBundle\Services\DatabaseBackup; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Liip\FunctionalTestBundle\Services\FixturesLoaderFactory; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* @author Aleksey Tupichenkov <[email protected]> | ||
*/ | ||
abstract class AbstractDatabaseBackup implements DatabaseBackupInterface | ||
{ | ||
protected $container; | ||
|
||
protected $fixturesLoaderFactory; | ||
|
||
/** | ||
* @var Connection | ||
*/ | ||
protected $connection; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $metadatas; | ||
|
||
/** | ||
* The fixture classnames. | ||
* | ||
* @var array | ||
*/ | ||
protected $classNames; | ||
|
||
public function __construct(ContainerInterface $container, FixturesLoaderFactory $fixturesLoaderFactory) | ||
{ | ||
$this->container = $container; | ||
$this->fixturesLoaderFactory = $fixturesLoaderFactory; | ||
} | ||
|
||
public function init(Connection $connection, array $metadatas, array $classNames): void | ||
{ | ||
$this->connection = $connection; | ||
$this->metadatas = $metadatas; | ||
$this->classNames = $classNames; | ||
} | ||
|
||
/** | ||
* Determine if the Fixtures that define a database backup have been | ||
* modified since the backup was made. | ||
* | ||
* @param string $backup The fixture backup database file path | ||
* | ||
* @return bool TRUE if the backup was made since the modifications to the | ||
* fixtures; FALSE otherwise | ||
*/ | ||
protected function isBackupUpToDate(string $backup): bool | ||
{ | ||
$backupLastModifiedDateTime = \DateTime::createFromFormat('U', filemtime($backup)); | ||
|
||
$loader = $this->fixturesLoaderFactory->getFixtureLoader($this->classNames); | ||
|
||
// Use loader in order to fetch all the dependencies fixtures. | ||
foreach ($loader->getFixtures() as $className) { | ||
$fixtureLastModifiedDateTime = $this->getFixtureLastModified($className); | ||
if ($backupLastModifiedDateTime < $fixtureLastModifiedDateTime) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* This function finds the time when the data blocks of a class definition | ||
* file were being written to, that is, the time when the content of the | ||
* file was changed. | ||
* | ||
* @param string $class The fully qualified class name of the fixture class to | ||
* check modification date on | ||
* | ||
* @return \DateTime|null | ||
*/ | ||
protected function getFixtureLastModified($class): ?\DateTime | ||
{ | ||
$lastModifiedDateTime = null; | ||
|
||
$reflClass = new \ReflectionClass($class); | ||
$classFileName = $reflClass->getFileName(); | ||
|
||
if (file_exists($classFileName)) { | ||
$lastModifiedDateTime = new \DateTime(); | ||
$lastModifiedDateTime->setTimestamp(filemtime($classFileName)); | ||
} | ||
|
||
return $lastModifiedDateTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Liip/FunctionalTestBundle | ||
* | ||
* (c) Lukas Kahwe Smith <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Liip\FunctionalTestBundle\Services\DatabaseBackup; | ||
|
||
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor; | ||
use Doctrine\DBAL\Connection; | ||
|
||
/** | ||
* @author Aleksey Tupichenkov <[email protected]> | ||
*/ | ||
interface DatabaseBackupInterface | ||
{ | ||
public function init(Connection $connection, array $metadatas, array $classNames): void; | ||
|
||
public function getBackupFilePath(): string; | ||
|
||
public function isBackupActual(): bool; | ||
|
||
public function backup(AbstractExecutor $executor): void; | ||
|
||
public function restore(AbstractExecutor $executor): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Liip/FunctionalTestBundle | ||
* | ||
* (c) Lukas Kahwe Smith <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Liip\FunctionalTestBundle\Services\DatabaseBackup; | ||
|
||
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor; | ||
|
||
/** | ||
* @author Aleksey Tupichenkov <[email protected]> | ||
*/ | ||
class SqliteDatabaseBackup extends AbstractDatabaseBackup implements DatabaseBackupInterface | ||
{ | ||
public function getBackupFilePath(): string | ||
{ | ||
return $this->container->getParameter('kernel.cache_dir').'/test_sqlite_'.md5(serialize($this->metadatas).serialize($this->classNames)).'.db'; | ||
} | ||
|
||
private function getDatabaseName(): string | ||
{ | ||
$params = $this->connection->getParams(); | ||
if (isset($params['master'])) { | ||
$params = $params['master']; | ||
} | ||
|
||
$name = $params['path'] ?? ($params['dbname'] ?? false); | ||
if (!$name) { | ||
throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); | ||
} | ||
|
||
return $name; | ||
} | ||
|
||
public function isBackupActual(): bool | ||
{ | ||
$backupDBFileName = $this->getBackupFilePath(); | ||
$backupReferenceFileName = $backupDBFileName.'.ser'; | ||
|
||
return file_exists($backupDBFileName) && file_exists($backupReferenceFileName) && $this->isBackupUpToDate($backupDBFileName); | ||
} | ||
|
||
public function backup(AbstractExecutor $executor): void | ||
{ | ||
$executor->getReferenceRepository()->save($this->getBackupFilePath()); | ||
copy($this->getDatabaseName(), $this->getBackupFilePath()); | ||
} | ||
|
||
public function restore(AbstractExecutor $executor): void | ||
{ | ||
copy($this->getBackupFilePath(), $this->getDatabaseName()); | ||
$executor->getReferenceRepository()->load($this->getBackupFilePath()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we then remove
cache_sqlite_db
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll remove it.
#398 (comment)