Skip to content

Commit

Permalink
Merge pull request #19 from byjg/4.0.2
Browse files Browse the repository at this point in the history
Release 4.0.2
  • Loading branch information
byjg authored Dec 17, 2018
2 parents 8e3086d + 8314e89 commit 7f1dfba
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 39 deletions.
11 changes: 3 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ services:
before_install:
- sudo service mysql stop || echo "mysql not stopped"
- sudo service postgresql stop || echo "postgresql not stopped"
- npm i @usdocker/usdocker @usdocker/postgres @usdocker/mysql @usdocker/mssql
- node_modules/.bin/usdocker --refresh
# - node_modules/.bin/usdocker mssql up
- node_modules/.bin/usdocker postgres up
- node_modules/.bin/usdocker mysql up
# - docker run --name mssql-container --rm -e ACCEPT_EULA=Y -e SA_PASSWORD=Pa55word -p 1433:1433 -d mcr.microsoft.com/mssql/server
- docker run --name postgres-container --rm -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:9-alpine
- docker run --name mysql-container --rm -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:5.7

install:
- composer install
- node_modules/.bin/usdocker postgres status
- node_modules/.bin/usdocker mysql status
- node_modules/.bin/usdocker mssql status

script:
- vendor/bin/phpunit
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ vendor/bin/phpunit tests/SqlServerDatabaseTest.php
### MySql

```bash
npm i @usdocker/usdocker @usdocker/mysql
./node_modules/.bin/usdocker --refresh --home /tmp
./node_modules/.bin/usdocker mysql up --home /tmp
docker run --name mysql-container --rm -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:5.7

docker run -it --rm \
--link mysql-container \
Expand All @@ -377,9 +375,7 @@ docker run -it --rm \
### Postgresql

```bash
npm i @usdocker/usdocker @usdocker/postgres
./node_modules/.bin/usdocker --refresh --home /tmp
./node_modules/.bin/usdocker postgres up --home /tmp
docker run --name postgres-container --rm -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:9-alpine

docker run -it --rm \
--link postgres-container \
Expand All @@ -392,9 +388,7 @@ docker run -it --rm \
### Microsoft SqlServer

```bash
npm i @usdocker/usdocker @usdocker/mssql
./node_modules/.bin/usdocker --refresh --home /tmp
./node_modules/.bin/usdocker mssql up --home /tmp
docker run --name mssql-container --rm -e ACCEPT_EULA=Y -e SA_PASSWORD=Pa55word -p 1433:1433 -d mcr.microsoft.com/mssql/server

docker run -it --rm \
--link mssql-container \
Expand Down
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
2 changes: 1 addition & 1 deletion example/sql_server/test_sqlserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* This user need to have grant for DDL commands;
*/

$uri = new \ByJG\Util\Uri('dblib://sa:Pa$$word!@mssql-container/migratedatabase');
$uri = new \ByJG\Util\Uri('dblib://sa:Pa55word@mssql-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);
$migration->registerDatabase('dblib', \ByJG\DbMigration\Database\DblibDatabase::class);
Expand Down
15 changes: 10 additions & 5 deletions src/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
Expand Down Expand Up @@ -101,8 +102,12 @@ protected function checkExistsVersion()
{
// Get the version to check if exists
$versionInfo = $this->getVersion();
if (empty($versionInfo['version'])) {
$this->getDbDriver()->execute("insert into " . $this->getMigrationTable() . " values(0, 'unknow')");
if ($versionInfo['version'] === false) {
$this->getDbDriver()->execute(sprintf(
"insert into %s values(0, '%s')",
$this->getMigrationTable(),
Migration::VERSION_STATUS_UNKNOWN)
);
}
}

Expand All @@ -111,9 +116,9 @@ protected function checkExistsVersion()
*/
public function updateVersionTable()
{
$currentVersion = $this->getDbDriver()->getScalar('select version from ' . $this->getMigrationTable());
$this->getDbDriver()->execute('drop table ' . $this->getMigrationTable());
$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, 'unknow');
$this->setVersion($currentVersion, Migration::VERSION_STATUS_UNKNOWN);
}
}
10 changes: 4 additions & 6 deletions src/Database/PgsqlDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class PgsqlDatabase extends AbstractDatabase
public static function prepareEnvironment(UriInterface $uri)
{
$database = preg_replace('~^/~', '', $uri->getPath());
$dbDriver = self::getDbDriverWithoutDatabase($uri);
self::createDatabaseIfNotExists($dbDriver, $database);
$dbDriver = static::getDbDriverWithoutDatabase($uri);
static::createDatabaseIfNotExists($dbDriver, $database);
}

protected static function getDbDriverWithoutDatabase(UriInterface $uri)
{
$customUri = new Uri($uri->__toString());
return Factory::getDbRelationalInstance($customUri->withPath('/')->__toString());
return Factory::getDbRelationalInstance($customUri->withPath('/postgres')->__toString());
}

/**
Expand All @@ -41,13 +41,11 @@ protected static function createDatabaseIfNotExists($dbDriver, $database)
public function createDatabase()
{
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
self::createDatabaseIfNotExists($this->getDbDriver(), $database);
static::createDatabaseIfNotExists($this->getDbDriver(), $database);
}

public function dropDatabase()
{
// $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());

$iterator = $this->getDbDriver()->getIterator(
"select 'drop table if exists \"' || tablename || '\" cascade;' command from pg_tables where schemaname = 'public';"
);
Expand Down
12 changes: 8 additions & 4 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,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 @@ -196,7 +200,7 @@ public function reset($upVersion = null)
$this->getDbCommand()->executeSql(file_get_contents($this->getBaseSql()));
}

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

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

if (strpos($versionInfo['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 @@ -274,9 +278,9 @@ protected function migrate($upVersion, $increment, $force)
call_user_func_array($this->callableProgress, ['migrate', $currentVersion]);
}

$this->getDbCommand()->setVersion($currentVersion, '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, 'complete');
$this->getDbCommand()->setVersion($currentVersion, Migration::VERSION_STATUS_COMPLETE);
$currentVersion = $currentVersion + $increment;
}
}
Expand Down
105 changes: 101 additions & 4 deletions tests/BaseDatabase.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
<?php

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 @@ -37,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 @@ -45,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 @@ -53,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 @@ -77,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('complete', $status);
$this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);

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

Expand All @@ -109,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('complete', $status);
$this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);

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

Expand All @@ -141,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('complete', $status);
$this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);

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

Expand All @@ -168,4 +228,41 @@ protected function assertVersion2()

$this->migrate->getDbDriver()->getIterator('select * from roles');
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @expectedException \ByJG\DbMigration\Exception\DatabaseNotVersionedException
*/
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' => Migration::VERSION_STATUS_UNKNOWN
]
], $records);

// Check Bug (cannot create twice)
$this->migrate->createVersion();
$records = $this->migrate->getDbDriver()->getIterator("select * from " . $this->migrationTable)->toArray();
$this->assertEquals([
[
'version' => '0',
'status' => Migration::VERSION_STATUS_UNKNOWN
]
], $records);
}
}
2 changes: 1 addition & 1 deletion tests/SqlServerDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class SqlServerDatabaseTest extends BaseDatabase
{
protected $uri = 'dblib://sa:Pa$$word!@mssql-container/migratedatabase';
protected $uri = 'dblib://sa:Pa55word@mssql-container/migratedatabase';

/**
* @var \ByJG\DbMigration\Migration
Expand Down

0 comments on commit 7f1dfba

Please sign in to comment.