Skip to content

Commit

Permalink
#18 - Add constants for the migration status
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Dec 17, 2018
1 parent 21e92c2 commit 8314e89
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"prefer-stable": true,
"require": {
"byjg/anydataset-db": "4.0.*",
"byjg/uri": "^1.0"
"byjg/uri": "^1.0",
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": ">=5.7"
Expand Down
9 changes: 3 additions & 6 deletions src/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
use ByJG\AnyDataset\Db\Factory;
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
use ByJG\DbMigration\Exception\OldVersionSchemaException;
use ByJG\DbMigration\Migration;
use Psr\Http\Message\UriInterface;

abstract class AbstractDatabase implements DatabaseInterface
{
const VERSION_STATUS_UNKNOWN = "unknown";
const VERSION_STATUS_PARTIAL = "partial";
const VERSION_STATUS_COMPLETE = "complete";

/**
* @var DbDriverInterface
*/
Expand Down Expand Up @@ -109,7 +106,7 @@ protected function checkExistsVersion()
$this->getDbDriver()->execute(sprintf(
"insert into %s values(0, '%s')",
$this->getMigrationTable(),
static::VERSION_STATUS_UNKNOWN)
Migration::VERSION_STATUS_UNKNOWN)
);
}
}
Expand All @@ -122,6 +119,6 @@ public function updateVersionTable()
$currentVersion = $this->getDbDriver()->getScalar(sprintf('select version from %s', $this->getMigrationTable()));
$this->getDbDriver()->execute(sprintf('drop table %s', $this->getMigrationTable()));
$this->createVersion();
$this->setVersion($currentVersion, static::VERSION_STATUS_UNKNOWN);
$this->setVersion($currentVersion, Migration::VERSION_STATUS_UNKNOWN);
}
}
13 changes: 8 additions & 5 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ByJG\DbMigration;

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\DbMigration\Database\AbstractDatabase;
use ByJG\DbMigration\Database\DatabaseInterface;
use ByJG\DbMigration\Exception\DatabaseDoesNotRegistered;
use ByJG\DbMigration\Exception\DatabaseIsIncompleteException;
Expand All @@ -12,6 +11,10 @@

class Migration
{
const VERSION_STATUS_UNKNOWN = "unknown";
const VERSION_STATUS_PARTIAL = "partial";
const VERSION_STATUS_COMPLETE = "complete";

/**
* @var UriInterface
*/
Expand Down Expand Up @@ -197,7 +200,7 @@ public function reset($upVersion = null)
$this->getDbCommand()->executeSql(file_get_contents($this->getBaseSql()));
}

$this->getDbCommand()->setVersion(0, AbstractDatabase::VERSION_STATUS_COMPLETE);
$this->getDbCommand()->setVersion(0, Migration::VERSION_STATUS_COMPLETE);
$this->up($upVersion);
}

Expand Down Expand Up @@ -264,7 +267,7 @@ protected function migrate($upVersion, $increment, $force)
$versionInfo = $this->getCurrentVersion();
$currentVersion = intval($versionInfo['version']) + $increment;

if (strpos($versionInfo['status'], AbstractDatabase::VERSION_STATUS_PARTIAL) !== false && !$force) {
if (strpos($versionInfo['status'], Migration::VERSION_STATUS_PARTIAL) !== false && !$force) {
throw new DatabaseIsIncompleteException('Database was not fully updated. Use --force for migrate.');
}

Expand All @@ -275,9 +278,9 @@ protected function migrate($upVersion, $increment, $force)
call_user_func_array($this->callableProgress, ['migrate', $currentVersion]);
}

$this->getDbCommand()->setVersion($currentVersion, AbstractDatabase::VERSION_STATUS_PARTIAL . ' ' . ($increment>0 ? 'up' : 'down'));
$this->getDbCommand()->setVersion($currentVersion, Migration::VERSION_STATUS_PARTIAL . ' ' . ($increment>0 ? 'up' : 'down'));
$this->getDbCommand()->executeSql(file_get_contents($file));
$this->getDbCommand()->setVersion($currentVersion, AbstractDatabase::VERSION_STATUS_COMPLETE);
$this->getDbCommand()->setVersion($currentVersion, Migration::VERSION_STATUS_COMPLETE);
$currentVersion = $currentVersion + $increment;
}
}
Expand Down
76 changes: 69 additions & 7 deletions tests/BaseDatabase.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
<?php

use ByJG\DbMigration\Database\AbstractDatabase;
use ByJG\DbMigration\Migration;

abstract class BaseDatabase extends \PHPUnit\Framework\TestCase
{
protected $uri = null;

/**
* @var \ByJG\DbMigration\Migration
* @var Migration
*/
protected $migrate = null;

protected $migrationTable = "migration_version";

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
*/
public function setUp()
{
// create Migrate object in the parent!!!

$this->migrate->prepareEnvironment();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
*/
public function tearDown()
{
$this->migrate->getDbCommand()->dropDatabase();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function testVersion0()
{
$this->migrate->reset(0);
$this->assertVersion0();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function testUpVersion1()
{
$this->migrate->reset(0);
Expand All @@ -39,6 +61,14 @@ public function testUpVersion1()
$this->assertVersion1();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function testUpVersion2()
{
$this->migrate->reset(0);
Expand All @@ -47,6 +77,14 @@ public function testUpVersion2()
$this->assertVersion2();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function testDownVersion1()
{
$this->migrate->reset();
Expand All @@ -55,6 +93,14 @@ public function testDownVersion1()
$this->assertVersion1();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function testDownVersion0()
{
$this->migrate->reset();
Expand All @@ -79,12 +125,16 @@ protected function getExpectedUsersVersion1()
];
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
protected function assertVersion0()
{
$version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
$this->assertEquals(0, $version);
$status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
$this->assertEquals(AbstractDatabase::VERSION_STATUS_COMPLETE, $status);
$this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);

$iterator = $this->migrate->getDbDriver()->getIterator('select * from users');

Expand All @@ -111,12 +161,16 @@ protected function assertVersion0()
}
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
protected function assertVersion1()
{
$version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
$this->assertEquals(1, $version);
$status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
$this->assertEquals(AbstractDatabase::VERSION_STATUS_COMPLETE, $status);
$this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);

$iterator = $this->migrate->getDbDriver()->getIterator('select * from users');

Expand All @@ -143,12 +197,16 @@ protected function assertVersion1()
}
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
protected function assertVersion2()
{
$version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
$this->assertEquals(2, $version);
$status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
$this->assertEquals(AbstractDatabase::VERSION_STATUS_COMPLETE, $status);
$this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);

$iterator = $this->migrate->getDbDriver()->getIterator('select * from users');

Expand Down Expand Up @@ -182,14 +240,18 @@ public function testGetCurrentVersionIsEmpty()
$this->migrate->getCurrentVersion();
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function testCreateVersion()
{
$this->migrate->createVersion();
$records = $this->migrate->getDbDriver()->getIterator("select * from " . $this->migrationTable)->toArray();
$this->assertEquals([
[
'version' => '0',
'status' => AbstractDatabase::VERSION_STATUS_UNKNOWN
'status' => Migration::VERSION_STATUS_UNKNOWN
]
], $records);

Expand All @@ -199,7 +261,7 @@ public function testCreateVersion()
$this->assertEquals([
[
'version' => '0',
'status' => AbstractDatabase::VERSION_STATUS_UNKNOWN
'status' => Migration::VERSION_STATUS_UNKNOWN
]
], $records);
}
Expand Down

0 comments on commit 8314e89

Please sign in to comment.