Replace DatabaseHandler with Doctrine DBAL connection

This commit is contained in:
Damian Zabawa
2020-04-03 13:16:21 +02:00
parent 74c3ef4687
commit b8c78ce2b7
8 changed files with 73 additions and 69 deletions

View File

@@ -2,7 +2,7 @@
namespace Kaliop\eZMigrationBundle\Core\Executor;
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
use Doctrine\DBAL\Connection;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface;
@@ -10,10 +10,8 @@ class SQLExecutor extends AbstractExecutor
{
use IgnorableStepExecutorTrait;
/**
* @var DatabaseHandler $connection
*/
protected $dbHandler;
/** @var \Doctrine\DBAL\Connection */
protected $connection;
protected $supportedStepTypes = array('sql');
@@ -21,12 +19,12 @@ class SQLExecutor extends AbstractExecutor
protected $referenceResolver;
/**
* @param DatabaseHandler $dbHandler
* @param \Doctrine\DBAL\Connection $connection
* @param ReferenceBagInterface $referenceResolver
*/
public function __construct(DatabaseHandler $dbHandler, ReferenceBagInterface $referenceResolver)
public function __construct(Connection $connection, ReferenceBagInterface $referenceResolver)
{
$this->dbHandler = $dbHandler;
$this->connection = $connection;
$this->referenceResolver = $referenceResolver;
}
@@ -41,7 +39,7 @@ class SQLExecutor extends AbstractExecutor
$this->skipStepIfNeeded($step);
$conn = $this->dbHandler->getConnection();
$conn = $this->connection;
// @see http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/platforms.html
$dbType = strtolower(preg_replace('/([0-9]+|Platform)/', '', $conn->getDatabasePlatform()->getName()));

View File

@@ -2,6 +2,7 @@
namespace Kaliop\eZMigrationBundle\Core\StorageHandler\Database;
use Doctrine\DBAL\FetchMode;
use Kaliop\eZMigrationBundle\API\ContextStorageHandlerInterface;
use Doctrine\DBAL\Schema\Schema;
@@ -13,14 +14,15 @@ class Context extends TableStorage implements ContextStorageHandlerInterface
{
$this->createTableIfNeeded();
/** @var \eZ\Publish\Core\Persistence\Database\SelectQuery $q */
$q = $this->dbHandler->createSelectQuery();
/** @var \Doctrine\DBAL\Query\QueryBuilder $q */
$q = $this->connection->createQueryBuilder();
$q->select($this->fieldList)
->from($this->tableName)
->where($q->expr->eq('migration', $q->bindValue($migrationName)));
$stmt = $q->prepare();
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
->where($q->expr()->eq('migration', $q->createPositionalParameter($migrationName)));
$stmt = $q->execute();
$result = $stmt->fetch(FetchMode::ASSOCIATIVE);
if (is_array($result) && !empty($result)) {
return $this->stringToContext($result['context']);
@@ -54,7 +56,7 @@ class Context extends TableStorage implements ContextStorageHandlerInterface
$conn->beginTransaction();
$stmt = $conn->executeQuery($sql, array($migrationName));
$existingMigrationData = $stmt->fetch(\PDO::FETCH_ASSOC);
$existingMigrationData = $stmt->fetch(FetchMode::ASSOCIATIVE);
if (is_array($existingMigrationData)) {
// context exists
@@ -106,7 +108,7 @@ class Context extends TableStorage implements ContextStorageHandlerInterface
public function createTable()
{
/** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $sm */
$sm = $this->dbHandler->getConnection()->getSchemaManager();
$sm = $this->getConnection()->getSchemaManager();
$dbPlatform = $sm->getDatabasePlatform();
$schema = new Schema();
@@ -118,7 +120,7 @@ class Context extends TableStorage implements ContextStorageHandlerInterface
$t->setPrimaryKey(array('migration'));
foreach ($schema->toSql($dbPlatform) as $sql) {
$this->dbHandler->exec($sql);
$this->connection->exec($sql);
}
}

View File

@@ -2,10 +2,11 @@
namespace Kaliop\eZMigrationBundle\Core\StorageHandler\Database;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\Schema;
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
use eZ\Publish\Core\Persistence\Database\SelectQuery;
use Kaliop\eZMigrationBundle\API\StorageHandlerInterface;
use Kaliop\eZMigrationBundle\API\Collection\MigrationCollection;
use Kaliop\eZMigrationBundle\API\Value\Migration as APIMigration;
@@ -23,14 +24,14 @@ class Migration extends TableStorage implements StorageHandlerInterface
protected $fieldList = 'migration, md5, path, execution_date, status, execution_error';
/**
* @param DatabaseHandler $dbHandler
* @param \Doctrine\DBAL\Connection $connection
* @param string $tableNameParameter
* @param ConfigResolverInterface $configResolver
* @throws \Exception
*/
public function __construct(DatabaseHandler $dbHandler, $tableNameParameter = 'kaliop_migrations', ConfigResolverInterface $configResolver = null)
public function __construct(Connection $connection, $tableNameParameter = 'kaliop_migrations', ConfigResolverInterface $configResolver = null)
{
parent::__construct($dbHandler, $tableNameParameter, $configResolver);
parent::__construct($connection, $tableNameParameter, $configResolver);
}
/**
@@ -64,14 +65,17 @@ class Migration extends TableStorage implements StorageHandlerInterface
{
$this->createTableIfNeeded();
/** @var \eZ\Publish\Core\Persistence\Database\SelectQuery $q */
$q = $this->dbHandler->createSelectQuery();
/** @var \Doctrine\DBAL\Query\QueryBuilder $q */
$q = $this->connection->createQueryBuilder();
$q->select($this->fieldList)
->from($this->tableName)
->orderBy('migration', SelectQuery::ASC);
->orderBy('migration', 'ASC');
if ($status !== null) {
$q->where($q->expr->eq('status', $q->bindValue($status)));
$q->where($q->expr()->eq('status', $q->createPositionalParameter($status)));
}
if ($limit > 0 || $offset > 0) {
if ($limit <= 0) {
$limit = null;
@@ -79,10 +83,11 @@ class Migration extends TableStorage implements StorageHandlerInterface
if ($offset == 0) {
$offset = null;
}
$q->limit($limit, $offset);
$q->setMaxResults($limit);
$q->setFirstResult($offset);
}
$stmt = $q->prepare();
$stmt->execute();
$stmt = $q->execute();
$results = $stmt->fetchAll();
$migrations = array();
@@ -101,14 +106,15 @@ class Migration extends TableStorage implements StorageHandlerInterface
{
$this->createTableIfNeeded();
/** @var \eZ\Publish\Core\Persistence\Database\SelectQuery $q */
$q = $this->dbHandler->createSelectQuery();
/** @var \Doctrine\DBAL\Query\QueryBuilder $q */
$q = $this->connection->createQueryBuilder();
$q->select($this->fieldList)
->from($this->tableName)
->where($q->expr->eq('migration', $q->bindValue($migrationName)));
$stmt = $q->prepare();
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
->where($q->expr()->eq('migration', $q->createPositionalParameter($migrationName)));
$stmt = $q->execute();
$result = $stmt->fetch(FetchMode::ASSOCIATIVE);
if (is_array($result) && !empty($result)) {
return $this->arrayToMigration($result);
@@ -196,7 +202,7 @@ class Migration extends TableStorage implements StorageHandlerInterface
$conn->beginTransaction();
$stmt = $conn->executeQuery($sql, array($migration->name));
$existingMigrationData = $stmt->fetch(\PDO::FETCH_ASSOC);
$existingMigrationData = $stmt->fetch(FetchMode::ASSOCIATIVE);
// fail if it was not executing
@@ -277,7 +283,7 @@ class Migration extends TableStorage implements StorageHandlerInterface
$conn->beginTransaction();
$stmt = $conn->executeQuery($sql, array($migrationDefinition->name));
$existingMigrationData = $stmt->fetch(\PDO::FETCH_ASSOC);
$existingMigrationData = $stmt->fetch(FetchMode::ASSOCIATIVE);
if (is_array($existingMigrationData)) {
// migration exists
@@ -359,7 +365,7 @@ class Migration extends TableStorage implements StorageHandlerInterface
$conn->beginTransaction();
$stmt = $conn->executeQuery($sql, array($migration->name));
$existingMigrationData = $stmt->fetch(\PDO::FETCH_ASSOC);
$existingMigrationData = $stmt->fetch(FetchMode::ASSOCIATIVE);
if (!is_array($existingMigrationData)) {
// commit immediately, to release the lock and avoid deadlocks
@@ -428,7 +434,7 @@ class Migration extends TableStorage implements StorageHandlerInterface
//$t->addIndex(array('path'));
foreach ($schema->toSql($dbPlatform) as $sql) {
$this->dbHandler->exec($sql);
$this->connection->exec($sql);
}
}

View File

@@ -2,7 +2,7 @@
namespace Kaliop\eZMigrationBundle\Core\StorageHandler\Database;
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
use Doctrine\DBAL\Connection;
use Kaliop\eZMigrationBundle\API\ConfigResolverInterface;
abstract class TableStorage
@@ -15,10 +15,8 @@ abstract class TableStorage
*/
protected $tableName;
/**
* @var DatabaseHandler $dbHandler
*/
protected $dbHandler;
/** @var \Doctrine\DBAL\Connection */
protected $connection;
/**
* Flag to indicate that the migration table has been created
@@ -28,25 +26,25 @@ abstract class TableStorage
protected $tableExists = false;
/**
* @param DatabaseHandler $dbHandler
* @param \Doctrine\DBAL\Connection $connection
* @param string $tableNameParameter name of table when $configResolver is null, name of parameter otherwise
* @param ConfigResolverInterface $configResolver
* @throws \Exception
*/
public function __construct(DatabaseHandler $dbHandler, $tableNameParameter, ConfigResolverInterface $configResolver = null)
public function __construct(Connection $connection, $tableNameParameter, ConfigResolverInterface $configResolver = null)
{
$this->dbHandler = $dbHandler;
$this->connection = $connection;
$this->tableName = $configResolver ? $configResolver->getParameter($tableNameParameter) : $tableNameParameter;
}
abstract function createTable();
/**
* @return mixed
* @return \Doctrine\DBAL\Connection
*/
protected function getConnection()
{
return $this->dbHandler->getConnection();
return $this->connection;
}
/**
@@ -58,7 +56,7 @@ abstract class TableStorage
protected function tableExist($tableName)
{
/** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $sm */
$sm = $this->dbHandler->getConnection()->getSchemaManager();
$sm = $this->getConnection()->getSchemaManager();
foreach ($sm->listTables() as $table) {
if ($table->getName() == $tableName) {
return true;
@@ -99,7 +97,7 @@ abstract class TableStorage
protected function drop()
{
if ($this->tableExist($this->tableName)) {
$this->dbHandler->exec('DROP TABLE ' . $this->tableName);
$this->connection->exec('DROP TABLE ' . $this->tableName);
}
}
@@ -109,7 +107,7 @@ abstract class TableStorage
public function truncate()
{
if ($this->tableExist($this->tableName)) {
$this->dbHandler->exec('TRUNCATE ' . $this->tableName);
$this->connection->exec('TRUNCATE ' . $this->tableName);
}
}
}
}

View File

@@ -4,12 +4,14 @@ use Kaliop\eZMigrationBundle\API\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Kaliop\eZMigrationBundle\API\Value\Migration;
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
use \eZ\Publish\Core\Persistence\Database\SelectQuery;
class MigrateV1ToV2 implements MigrationInterface
{
private $container;
private $dbHandler;
/** @var \Doctrine\DBAL\Connection */
private $connection;
private $activeBundles;
private $legacyTableName;
private $legacyMigrationsDir;
@@ -32,7 +34,7 @@ class MigrateV1ToV2 implements MigrationInterface
$this->legacyMigrationsDir = $this->container->get('ez_migration_bundle.helper.config.resolver')->getParameter('kaliop_bundle_migration.version_directory');
$migrationStorageService = $this->container->get('ez_migration_bundle.storage_handler');
$this->dbHandler = $this->container->get('ezpublish.connection');
$this->connection = $this->container->get('ezpublish.api.storage_engine.legacy.connection');
$this->activeBundles = array();
foreach ($this->container->get('kernel')->getBundles() as $bundle)
@@ -102,7 +104,7 @@ class MigrateV1ToV2 implements MigrationInterface
private function tableExist($tableName)
{
/** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $sm */
$sm = $this->dbHandler->getConnection()->getSchemaManager();
$sm = $this->connection->getSchemaManager();
foreach ($sm->listTables() as $table) {
if ($table->getName() == $tableName) {
return true;
@@ -114,13 +116,13 @@ class MigrateV1ToV2 implements MigrationInterface
private function loadLegacyMigrations()
{
/** @var \eZ\Publish\Core\Persistence\Database\SelectQuery $q */
$q = $this->dbHandler->createSelectQuery();
/** @var \Doctrine\DBAL\Query\QueryBuilder $q */
$q = $this->connection->createQueryBuilder();
$q->select('version, bundle')
->from($this->legacyTableName)
->orderBy('version', SelectQuery::ASC);
$stmt = $q->prepare();
$stmt->execute();
->orderBy('version', 'ASC');
$stmt = $q->execute();
$results = $stmt->fetchAll();
return $results;
@@ -128,7 +130,7 @@ class MigrateV1ToV2 implements MigrationInterface
private function deleteLegacyMigration($version, $bundle)
{
$this->dbHandler->getConnection()->delete($this->legacyTableName, array('version' => $version, 'bundle' => $bundle));
$this->connection->delete($this->legacyTableName, array('version' => $version, 'bundle' => $bundle));
}
/**

View File

@@ -197,7 +197,7 @@ services:
ez_migration_bundle.storage_handler.database:
class: '%ez_migration_bundle.storage_handler.database.class%'
arguments:
- '@ezpublish.connection'
- "@ezpublish.api.storage_engine.legacy.connection"
- 'ez_migration_bundle.table_name'
- '@ez_migration_bundle.helper.config.resolver'
@@ -207,7 +207,7 @@ services:
ez_migration_bundle.context_storage_handler.database:
class: '%ez_migration_bundle.context_storage_handler.database.class%'
arguments:
- '@ezpublish.connection'
- "@ezpublish.api.storage_engine.legacy.connection"
- 'ez_migration_bundle.context_table_name'
- '@ez_migration_bundle.helper.config.resolver'
@@ -318,7 +318,7 @@ services:
ez_migration_bundle.executor.sql:
class: '%ez_migration_bundle.executor.sql.class%'
arguments:
- '@ezpublish.connection'
- "@ezpublish.api.storage_engine.legacy.connection"
- '@ez_migration_bundle.reference_resolver.customreference'
calls:
- ['setReferenceMatcher', ['@ez_migration_bundle.reference_matcher']]

View File

@@ -5,7 +5,6 @@ use Kaliop\eZMigrationBundle\API\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Kaliop\eZMigrationBundle\API\Value\Migration;
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
use \eZ\Publish\Core\Persistence\Database\SelectQuery;
class noclassname implements MigrationInterface
{

View File

@@ -5,7 +5,6 @@ use Kaliop\eZMigrationBundle\API\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Kaliop\eZMigrationBundle\API\Value\Migration;
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
use \eZ\Publish\Core\Persistence\Database\SelectQuery;
class HarmlessClass implements MigrationInterface
{