From bfe988be4203da42a337a4d60e3862b0380c67cb Mon Sep 17 00:00:00 2001 From: Igor Mukhin Date: Sat, 26 Sep 2015 18:51:26 +0300 Subject: [PATCH 1/2] Added: New option `dry-run` --- src/Command/LoadCommand.php | 9 ++++++- src/Persister/PdoPersister.php | 38 ++++++++++++++++++++++++++-- src/TableRecord.php | 7 ++++- tests/Persister/PdoPersisterTest.php | 6 +++-- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Command/LoadCommand.php b/src/Command/LoadCommand.php index 9b78a09..1fe430c 100644 --- a/src/Command/LoadCommand.php +++ b/src/Command/LoadCommand.php @@ -36,6 +36,13 @@ protected function configure() InputArgument::REQUIRED, 'Database connection details' ) + ->addOption( + 'dry-run', + 'd', + InputOption::VALUE_NONE, + 'Do not run any SQL query - just pass to output', + null + ) ->addArgument( 'autouuidfield', InputArgument::OPTIONAL, @@ -105,7 +112,7 @@ public function execute(InputInterface $input, OutputInterface $output) $dburl )); - $persister = new PdoPersister($pdo); + $persister = new PdoPersister($pdo, $output, $input->getOption('dry-run')); if (!is_null($input->getOption('append'))) { $persister->reset($objects); } diff --git a/src/Persister/PdoPersister.php b/src/Persister/PdoPersister.php index dda98a0..a63ef11 100644 --- a/src/Persister/PdoPersister.php +++ b/src/Persister/PdoPersister.php @@ -10,13 +10,17 @@ class PdoPersister implements PersisterInterface { private $pdo; + private $output; + private $dryRun; /** * @param PDO $pdo */ - public function __construct(PDO $pdo) + public function __construct(PDO $pdo, $output, $dryRun = false) { $this->pdo = $pdo; + $this->output = $output; + $this->dryRun = $dryRun; } /** @@ -26,7 +30,15 @@ public function reset($objects) { foreach ($objects as $object) { $tablename = $object->__meta('tablename'); - $statement = $this->pdo->prepare(sprintf("TRUNCATE `%s`", $tablename)); + $sql = sprintf("TRUNCATE `%s`", $tablename); + + if ($this->dryRun) { + $this->output->writeln(sprintf("Will be executed: %s", $sql)); + continue; + } + + $this->output->writeln(sprintf("Executing: %s", $sql)); + $statement = $this->pdo->prepare($sql); $statement->execute(); } } @@ -42,6 +54,19 @@ public function persist(array $objects) $sql = $this->buildSql($tablename, $fields); + if ($this->dryRun) { + $this->output->writeln(sprintf( + "Will be executed: %s", + $this->getExpectedSqlQuery($sql, $fields) + )); + continue; + } + + $this->output->writeln(sprintf( + "Executing: %s", + $this->getExpectedSqlQuery($sql, $fields) + )); + $statement = $this->pdo->prepare($sql); $res = $statement->execute($fields); @@ -96,4 +121,13 @@ private function implodeBindNames($fields) $fields_names = array_keys($fields); return ":" . implode($fields_names, ", :"); } + + public function getExpectedSqlQuery($sql, $fields) + { + foreach ($fields as $key=>$value) { + $key = preg_quote($key); + $sql = preg_replace("/:$key/", $value, $sql); + } + return $sql; + } } diff --git a/src/TableRecord.php b/src/TableRecord.php index 6c78bb9..c87d9b2 100644 --- a/src/TableRecord.php +++ b/src/TableRecord.php @@ -3,6 +3,7 @@ namespace Haigha; use RuntimeException; +use Exception; class TableRecord { @@ -22,7 +23,11 @@ public function __construct($tablename) */ public function __toString() { - return (string)$this->id; + try { + return (string)$this->id; + } catch (Exception $exception) { + return ''; + } } public function __call($key, $params) diff --git a/tests/Persister/PdoPersisterTest.php b/tests/Persister/PdoPersisterTest.php index 362fa68..39bab10 100644 --- a/tests/Persister/PdoPersisterTest.php +++ b/tests/Persister/PdoPersisterTest.php @@ -3,6 +3,7 @@ namespace Haigha\Tests\Persister; use Haigha\Persister\PdoPersister; +use Symfony\Component\Console\Output\BufferedOutput; /** * Test class for PdoPersister @@ -12,6 +13,7 @@ class PdoPersisterTest extends \PHPUnit_Framework_TestCase { private $persister; + private $output; public function setUp() { @@ -19,8 +21,8 @@ public function setUp() ->disableOriginalConstructor() ->getMock() ; - - $this->persister = new PdoPersister($pdo); + $this->output = new BufferedOutput(); + $this->persister = new PdoPersister($pdo, $this->output); } public function testBuildSql() From 9cc58a31c46543ed386abeb13b396b194c97af21 Mon Sep 17 00:00:00 2001 From: Igor Mukhin Date: Sat, 26 Sep 2015 20:56:17 +0300 Subject: [PATCH 2/2] Added: Notice to README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3cf3b53..3b5c2ec 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Haigha uses Alice to load fixture files, so the format is identical ([Details](h ```yaml table.group: group_random_users: + id: 1 # This is important for version ~2.0 name: Random users table.user: