Skip to content

Commit

Permalink
Merge pull request #1727 from MasterOdin/dry_run_initial
Browse files Browse the repository at this point in the history
Do not throw exception for missing phinxlog for dry run
  • Loading branch information
dereuromark committed Apr 8, 2020
2 parents 6bac1f5 + 66ad46a commit d748e18
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,17 @@ public function getVersionLog()
throw new RuntimeException('Invalid version_order configuration option');
}

$rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->quoteTableName($this->getSchemaTableName()), $orderBy));
// This will throw an exception if doing a --dry-run without any migrations as phinxlog
// does not exist, so in that case, we can just expect to trivially return empty set
try {
$rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->quoteTableName($this->getSchemaTableName()), $orderBy));
} catch (PDOException $e) {
if (!$this->isDryRunEnabled()) {
throw $e;
}
$rows = [];
}

foreach ($rows as $version) {
$result[$version['version']] = $version;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Phinx/Db/Adapter/PdoAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Phinx\Db\Adapter;

use PDOException;
use PHPUnit\Framework\TestCase;

class PdoAdapterTestPDOMock extends \PDO
Expand Down Expand Up @@ -158,6 +159,38 @@ public function testGetVersionLogInvalidVersionOrderKO()
$adapter->getVersionLog();
}

public function testGetVersionLongDryRun()
{
$adapter = $this->getMockForAbstractClass(
'\Phinx\Db\Adapter\PdoAdapter',
[['version_order' => \Phinx\Config\Config::VERSION_ORDER_CREATION_TIME]],
'',
true,
true,
true,
['isDryRunEnabled', 'fetchAll', 'getSchemaTableName', 'quoteTableName']
);

$schemaTableName = 'log';

$adapter->expects($this->once())
->method('isDryRunEnabled')
->will($this->returnValue(true));
$adapter->expects($this->once())
->method('getSchemaTableName')
->will($this->returnValue($schemaTableName));
$adapter->expects($this->once())
->method('quoteTableName')
->with($schemaTableName)
->will($this->returnValue("'$schemaTableName'"));
$adapter->expects($this->once())
->method('fetchAll')
->with("SELECT * FROM '$schemaTableName' ORDER BY version ASC")
->will($this->throwException(new PDOException()));

$this->assertEquals([], $adapter->getVersionLog());
}

/**
* Tests that execute() can be called on the adapter, and that the SQL is passed through to the PDO.
*/
Expand Down

0 comments on commit d748e18

Please sign in to comment.