Skip to content

Commit

Permalink
Merge pull request #919 from goetas/query-value-object
Browse files Browse the repository at this point in the history
Introduce query value object to wrap sql queries
  • Loading branch information
goetas authored Jan 20, 2020
2 parents ca826f6 + c024f00 commit dcd1d05
Show file tree
Hide file tree
Showing 21 changed files with 410 additions and 239 deletions.
8 changes: 4 additions & 4 deletions lib/Doctrine/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\MigrationException;
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 @@ -34,7 +34,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 @@ -142,11 +142,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;
}
22 changes: 22 additions & 0 deletions lib/Doctrine/Migrations/Query/Exception/InvalidArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Query\Exception;

use Doctrine\Migrations\Exception\MigrationException;
use InvalidArgumentException;
use function sprintf;

class InvalidArguments extends InvalidArgumentException implements MigrationException
{
public static function wrongTypesArgumentCount(string $statement, int $parameters, int $types) : self
{
return new self(sprintf(
'The number of types (%s) is higher than the number of passed parameters (%s) for the query "%s".',
$types,
$parameters,
$statement
));
}
}
60 changes: 60 additions & 0 deletions lib/Doctrine/Migrations/Query/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Query;

use Doctrine\Migrations\Query\Exception\InvalidArguments;
use function count;

/**
* 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 = [])
{
if (count($types) > count($parameters)) {
throw InvalidArguments::wrongTypesArgumentCount($statement, count($parameters), count($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
Loading

0 comments on commit dcd1d05

Please sign in to comment.