Skip to content

Commit

Permalink
Merge pull request #5 from igormukhingmailcom/master
Browse files Browse the repository at this point in the history
Added: New option `dry-run`
  • Loading branch information
h-wang committed Sep 29, 2015
2 parents e9492f7 + 9cc58a3 commit 9cbe300
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion src/Command/LoadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
38 changes: 36 additions & 2 deletions src/Persister/PdoPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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();
}
}
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
}
7 changes: 6 additions & 1 deletion src/TableRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Haigha;

use RuntimeException;
use Exception;

class TableRecord
{
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tests/Persister/PdoPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Haigha\Tests\Persister;

use Haigha\Persister\PdoPersister;
use Symfony\Component\Console\Output\BufferedOutput;

/**
* Test class for PdoPersister
Expand All @@ -12,15 +13,16 @@
class PdoPersisterTest extends \PHPUnit_Framework_TestCase
{
private $persister;
private $output;

public function setUp()
{
$pdo = $this->getMockBuilder('PDO')
->disableOriginalConstructor()
->getMock()
;

$this->persister = new PdoPersister($pdo);
$this->output = new BufferedOutput();
$this->persister = new PdoPersister($pdo, $this->output);
}

public function testBuildSql()
Expand Down

0 comments on commit 9cbe300

Please sign in to comment.