Skip to content

Commit

Permalink
introduce query value object to wrap sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Jan 18, 2020
1 parent 93927c3 commit 5ee2b89
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 159 deletions.
8 changes: 4 additions & 4 deletions lib/Doctrine/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\SkipMigration;
use Doctrine\Migrations\Query\Query;
use Psr\Log\LoggerInterface;
use function func_get_args;

/**
* The AbstractMigration class is for end users to extend from when creating migrations. Extend this class
Expand All @@ -32,7 +32,7 @@ abstract class AbstractMigration
/** @var LoggerInterface */
private $logger;

/** @var mixed[] */
/** @var Query[] */
private $plannedSql = [];

public function __construct(Connection $connection, LoggerInterface $logger)
Expand Down Expand Up @@ -122,11 +122,11 @@ protected function addSql(
array $params = [],
array $types = []
) : void {
$this->plannedSql[] = func_get_args();
$this->plannedSql[] = new Query($sql, $params, $types);
}

/**
* @return mixed[]
* @return Query[]
*/
public function getSql() : array
{
Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/Migrations/DbalMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tools\BytesFormatter;
use Doctrine\Migrations\Version\Executor;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function __construct(
}

/**
* @return string[][]
* @return array<string, Query[]>
*/
private function executeMigrations(
MigrationPlanList $migrationsPlan,
Expand Down Expand Up @@ -85,7 +86,7 @@ private function executeMigrations(
}

/**
* @return array<string, string[]>
* @return array<string, Query[]>
*/
private function executePlan(MigrationPlanList $migrationsPlan, MigratorConfiguration $migratorConfiguration) : array
{
Expand All @@ -110,7 +111,7 @@ private function executePlan(MigrationPlanList $migrationsPlan, MigratorConfigur
}

/**
* @param string[][] $sql
* @param array<string, Query[]> $sql
*/
private function endMigrations(
StopwatchEvent $stopwatchEvent,
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/Migrations/FileQueryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Migrations\Generator\FileBuilder;
use Doctrine\Migrations\Query\Query;
use Psr\Log\LoggerInterface;
use function file_put_contents;
use function is_dir;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function __construct(
}

/**
* @param mixed[] $queriesByVersion
* @param array<string,Query[]> $queriesByVersion
*/
public function write(
string $path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Migrations\Query\Query;
use function sprintf;

/**
Expand All @@ -15,7 +16,7 @@
*/
final class ConcatenationFileBuilder implements FileBuilder
{
/** @param string[][] $queriesByVersion */
/** @param array<string,Query[]> $queriesByVersion */
public function buildMigrationFile(
array $queriesByVersion,
string $direction,
Expand All @@ -25,12 +26,10 @@ public function buildMigrationFile(
$string = sprintf("-- Doctrine Migration File Generated on %s\n", $now->format('Y-m-d H:i:s'));

foreach ($queriesByVersion as $version => $queries) {
$version = (string) $version;

$string .= "\n-- Version " . $version . "\n";

foreach ($queries as $query) {
$string .= $query . ";\n";
$string .= $query->getStatement() . ";\n";
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/Migrations/Generator/FileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Migrations\Generator;

use DateTimeInterface;
use Doctrine\Migrations\Query\Query;

/**
* The ConcatenationFileBuilder class is responsible for building a migration SQL file from an array of queries per version.
Expand All @@ -13,6 +14,6 @@
*/
interface FileBuilder
{
/** @param string[][] $queriesByVersion */
/** @param array<string,Query[]> $queriesByVersion */
public function buildMigrationFile(array $queriesByVersion, string $direction, ?DateTimeInterface $now = null) : string;
}
5 changes: 3 additions & 2 deletions lib/Doctrine/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
namespace Doctrine\Migrations;

use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Query\Query;

/**
* The DbalMigrator class is responsible for generating and executing the SQL for a migration.
* The Migrator interface is responsible for generating and executing the SQL for a migration.
*
* @internal
*/
interface Migrator
{
/**
* @return array<string, string[]> A list of SQL statements executed, grouped by migration version
* @return array<string, Query[]> A list of SQL statements executed, grouped by migration version
*/
public function migrate(MigrationPlanList $migrationsPlan, MigratorConfiguration $migratorConfiguration) : array;
}
53 changes: 53 additions & 0 deletions lib/Doctrine/Migrations/Query/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Query;

/**
* The Query wraps the sql query, parameters and types.
*/
final class Query
{
/** @var string */
private $statement;

/** @var mixed[] */
private $parameters;

/** @var mixed[] */
private $types;

/**
* @param mixed[] $parameters
* @param mixed[] $types
*/
public function __construct(string $statement, array $parameters = [], array $types = [])
{
$this->statement = $statement;
$this->parameters = $parameters;
$this->types = $types;
}

public function __toString() : string
{
return $this->statement;
}

public function getStatement() : string
{
return $this->statement;
}

/** @return mixed[] */
public function getParameters() : array
{
return $this->parameters;
}

/** @return mixed[] */
public function getTypes() : array
{
return $this->types;
}
}
4 changes: 3 additions & 1 deletion lib/Doctrine/Migrations/QueryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\Migrations;

use Doctrine\Migrations\Query\Query;

/**
* The QueryWriter defines the interface used for writing migration SQL queries to a file on disk.
*
Expand All @@ -12,7 +14,7 @@
interface QueryWriter
{
/**
* @param string[][] $queriesByVersion
* @param array<string,Query[]> $queriesByVersion
*/
public function write(
string $path,
Expand Down
84 changes: 20 additions & 64 deletions lib/Doctrine/Migrations/Version/DbalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Migrations\MigratorConfiguration;
use Doctrine\Migrations\ParameterFormatter;
use Doctrine\Migrations\Provider\SchemaDiffProvider;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Stopwatch;
use Doctrine\Migrations\Tools\BytesFormatter;
use Psr\Log\LoggerInterface;
Expand All @@ -42,15 +43,9 @@ final class DbalExecutor implements Executor
/** @var Stopwatch */
private $stopwatch;

/** @var array<int, string> */
/** @var Query[] */
private $sql = [];

/** @var mixed[] */
private $params = [];

/** @var mixed[] */
private $types = [];

/** @var MetadataStorage */
private $metadataStorage;

Expand Down Expand Up @@ -79,42 +74,16 @@ public function __construct(
}

/**
* @return string[]
* @return Query[]
*/
public function getSql() : array
{
return $this->sql;
}

/**
* @return mixed[]
*/
public function getParams() : array
public function addSql(Query $sqlQuery) : void
{
return $this->params;
}

/**
* @return mixed[]
*/
public function getTypes() : array
{
return $this->types;
}

/**
* @param mixed[] $params
* @param mixed[] $types
*/
public function addSql(string $sql, array $params = [], array $types = []) : void
{
$this->sql[] = $sql;

if (count($params) === 0) {
return;
}

$this->addQueryParams($params, $types);
$this->sql[] = $sqlQuery;
}

public function execute(
Expand All @@ -132,7 +101,7 @@ public function execute(
$configuration
);

$result->setSql($this->sql, $this->params, $this->types);
$result->setSql($this->sql);
} catch (SkipMigration $e) {
$result->setSkipped(true);

Expand All @@ -152,9 +121,7 @@ private function startMigration(
MigrationPlan $plan,
MigratorConfiguration $configuration
) : void {
$this->sql = [];
$this->params = [];
$this->types = [];
$this->sql = [];

$this->dispatcher->dispatchVersionEvent(
Events::onMigrationsVersionExecuting,
Expand Down Expand Up @@ -196,20 +163,20 @@ private function executeMigration(

$migration->$direction($toSchema);

foreach ($migration->getSql() as $sqlData) {
$this->addSql(...$sqlData);
foreach ($migration->getSql() as $sqlQuery) {
$this->addSql($sqlQuery);
}

foreach ($this->schemaProvider->getSqlDiffToMigrate($fromSchema, $toSchema) as $sql) {
$this->addSql($sql);
$this->addSql(new Query($sql));
}

if (count($this->sql) !== 0) {
if (! $configuration->isDryRun()) {
$this->executeResult($configuration);
} else {
foreach ($this->sql as $idx => $query) {
$this->outputSqlQuery($idx, $query);
foreach ($this->sql as $query) {
$this->outputSqlQuery($query);
}
}
} else {
Expand Down Expand Up @@ -329,12 +296,12 @@ private function executeResult(MigratorConfiguration $configuration) : void
foreach ($this->sql as $key => $query) {
$stopwatchEvent = $this->stopwatch->start('query');

$this->outputSqlQuery($key, $query);
$this->outputSqlQuery($query);

if (! isset($this->params[$key])) {
$this->connection->executeQuery($query);
if (count($query->getParameters()) === 0) {
$this->connection->executeQuery($query->getStatement());
} else {
$this->connection->executeQuery($query, $this->params[$key], $this->types[$key]);
$this->connection->executeQuery($query->getStatement(), $query->getParameters(), $query->getTypes());
}

$stopwatchEvent->stop();
Expand All @@ -349,26 +316,15 @@ private function executeResult(MigratorConfiguration $configuration) : void
}
}

/**
* @param mixed[]|int $params
* @param mixed[]|int $types
*/
private function addQueryParams($params, $types) : void
{
$index = count($this->sql) - 1;
$this->params[$index] = $params;
$this->types[$index] = $types;
}

private function outputSqlQuery(int $idx, string $query) : void
private function outputSqlQuery(Query $query) : void
{
$params = $this->parameterFormatter->formatParameters(
$this->params[$idx] ?? [],
$this->types[$idx] ?? []
$query->getParameters(),
$query->getTypes()
);

$this->logger->debug('{query} {params}', [
'query' => $query,
'query' => $query->getStatement(),
'params' => $params,
]);
}
Expand Down
Loading

0 comments on commit 5ee2b89

Please sign in to comment.