Skip to content

Commit

Permalink
Adds possibility to dry run
Browse files Browse the repository at this point in the history
  • Loading branch information
newcron committed Jan 5, 2016
1 parent 5b637ad commit 04bf1bb
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ time you run the migration tool, you'll need to *Initialize* it (generating that
```php
$pdo = new PDO("mysql:host=yourdbhost;database=yourdb", "youruser", "yourpass");

(new \dbmigrate\Initialize($pdo))->createInstalledMigrationsTable();
call_user_func(new \dbmigrate\Initialize($pdo));
```

##### Running Migrations (on every deployment)
Expand All @@ -35,7 +35,7 @@ If multiple migrations have to be installed at once, the order in which they are
```php
$pdo = new PDO("mysql:host=yourdbhost;database=yourdb", "youruser", "yourpass");

(new \dbmigrate\Migrate($pdo, new \SplFileInfo("/path/to/your/sql/folder")))->runMissingMigrations();
call_user_func(new \dbmigrate\Migrate($pdo, new \SplFileInfo("/path/to/your/sql/folder")));
```


Expand Down
2 changes: 1 addition & 1 deletion src/php/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(\PDO $pdo)
}


public function createInstalledMigrationsTable()
public function __invoke()
{
$this->migrationTablePresenceConstraint->assertTableMissing();
$this->runMigration->run(new SqlFile(new \SplFileInfo(__DIR__ . "/../sql/init.sql")));
Expand Down
11 changes: 4 additions & 7 deletions src/php/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace dbmigrate;


use dbmigrate\application\MigrationDirectoryValidator;
use dbmigrate\application\MigrationException;
use dbmigrate\application\MigrationFileScanner;
use dbmigrate\application\Planner;
Expand All @@ -30,18 +31,14 @@ public function __construct(\PDO $pdo, \SplFileInfo $sqlDirectory)
if($pdo === null) {
throw new \InvalidArgumentException("PDO may not be null");
}
if($sqlDirectory===null) {
throw new \InvalidArgumentException("sqlDirectory may not be null");
}

if(!$sqlDirectory->isDir() || !is_readable($sqlDirectory->getPathname())) {
throw new \InvalidArgumentException("sqlDirectory ".$sqlDirectory->getPathname()." does not exist or is not readable");
}
(new MigrationDirectoryValidator())->assertValidMigrationFileDirectory($sqlDirectory);

$this->pdo = $pdo;
$this->sqlDirectory = $sqlDirectory;
}

public function runMissingMigrations() {
public function __invoke() {
$fileScanner = new MigrationFileScanner($this->sqlDirectory);
$loadMigrations = new LoadMigrations($this->pdo);
$migrationsToInstall = (new Planner($fileScanner, $loadMigrations))->findMigrationsToInstall();
Expand Down
42 changes: 42 additions & 0 deletions src/php/MigrateDryRun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php


namespace dbmigrate;


use dbmigrate\application\MigrationDirectoryValidator;
use dbmigrate\application\MigrationFileScanner;
use dbmigrate\application\Planner;
use dbmigrate\application\sql\LoadMigrations;
use dbmigrate\application\sql\SqlFile;

class MigrateDryRun
{
/** @var \PDO */
private $pdo;

/** @var \SplFileInfo */
private $sqlDirectory;

public function __construct(\PDO $pdo, \SplFileInfo $sqlDirectory)
{
if ($pdo === null) {
throw new \InvalidArgumentException("PDO may not be null");
}

(new MigrationDirectoryValidator())->assertValidMigrationFileDirectory($sqlDirectory);

$this->pdo = $pdo;
$this->sqlDirectory = $sqlDirectory;
}

/** @return SqlFile[] */
public function __invoke()
{
$fileScanner = new MigrationFileScanner($this->sqlDirectory);
$loadMigrations = new LoadMigrations($this->pdo);

return (new Planner($fileScanner, $loadMigrations))->findMigrationsToInstall();

}
}
20 changes: 20 additions & 0 deletions src/php/application/MigrationDirectoryValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace dbmigrate\application;


class MigrationDirectoryValidator
{
public function assertValidMigrationFileDirectory(\SplFileInfo $sqlDirectory)
{
if($sqlDirectory===null) {
throw new \InvalidArgumentException("sqlDirectory may not be null");
}

if(!$sqlDirectory->isDir() || !is_readable($sqlDirectory->getPathname())) {
throw new \InvalidArgumentException("sqlDirectory ".$sqlDirectory->getPathname()." does not exist or is not readable");
}

}
}
4 changes: 1 addition & 3 deletions src/php/application/MigrationFileScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class MigrationFileScanner
*/
public function __construct(\SplFileInfo $scanDir)
{
if (!$scanDir->isDir() || !is_readable($scanDir->getPathname())) {
throw new \InvalidArgumentException("Passed dir " . $scanDir->getPathname() . " is not a dir or not readable");
}
(new MigrationDirectoryValidator())->assertValidMigrationFileDirectory($scanDir);
$this->scanDir = $scanDir;
}

Expand Down
4 changes: 2 additions & 2 deletions test/php/InitializeIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class InitializeIntegrationTest extends PHPUnit_Framework_TestCase
public function testInitializeCreatesExactlyOneTable()
{
$pdo = $this->aNewSchema();
(new Initialize($pdo))->createInstalledMigrationsTable();
call_user_func(new Initialize($pdo));

$this->assertEquals(1, $pdo->query("show tables;")->rowCount());
}

public function testInitializeCreatesInfoTable()
{
$pdo = $this->aNewSchema();
(new Initialize($pdo))->createInstalledMigrationsTable();
call_user_func(new Initialize($pdo));

$this->assertEquals("installed_migrations", $pdo->query("show tables;")->fetchColumn(0));
}
Expand Down
33 changes: 33 additions & 0 deletions test/php/MigrateDryRunIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace dbmigrate;


use dbmigrate\application\MigrationException;
use dbmigrate\application\schema\Migration;
use dbmigrate\application\sql\LoadMigrations;
use dbmigrate\application\sql\SqlFile;
use dbmigrate\testutil\IntegrationDatabase;

class MigrateDryRunIntegrationTest extends \PHPUnit_Framework_TestCase
{
use IntegrationDatabase;


public function testInitializeFromEmptySchema()
{
$pdo = $this->aNewInitializedSchema();

$sqlDirectory = new \SplFileInfo(__DIR__ . "/../testsets/nummericsorting");

$files = call_user_func(new MigrateDryRun($pdo, $sqlDirectory));

$this->assertEquals([
new SqlFile(new \SplFileInfo($sqlDirectory->getPathname()."/v3_test.sql")),
new SqlFile(new \SplFileInfo($sqlDirectory->getPathname()."/v10_test.sql"))
], $files);
}


}
8 changes: 4 additions & 4 deletions test/php/MigrateIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testInitializeFromEmptySchema()
{
$pdo = $this->aNewInitializedSchema();

(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/nummericsorting")))->runMissingMigrations();
call_user_func(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/nummericsorting")));

$migrations = (new LoadMigrations($pdo))->allInstalledMigrations();
$this->assertEquals("v3_test.sql", $migrations[0]->getFilename());
Expand All @@ -30,8 +30,8 @@ public function testInitializeIncremental()

$pdo = $this->aNewInitializedSchema();

(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/incremental/step1")))->runMissingMigrations();
(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/incremental/step2")))->runMissingMigrations();
call_user_func(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/incremental/step1")));
call_user_func(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/incremental/step2")));

$migrations = (new LoadMigrations($pdo))->allInstalledMigrations();
$this->assertEquals("v3_test.sql", $migrations[0]->getFilename());
Expand All @@ -44,7 +44,7 @@ public function testMarkBrokenSqlFileAsFailed()
$pdo = $this->aNewInitializedSchema();

try {
(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/failing")))->runMissingMigrations();
call_user_func(new Migrate($pdo, new \SplFileInfo(__DIR__ . "/../testsets/failing")));
$this->fail("should throw an exception");
} catch(MigrationException $e) {
// expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testValidationPassesOnExistingTable()
{
$pdo = $this->aNewSchema();

(new Initialize($pdo))->createInstalledMigrationsTable();
(new Initialize($pdo))->__invoke();

(new MigrationTablePresenceConstraint($pdo))->assertTablePresent();
}
Expand Down
2 changes: 1 addition & 1 deletion test/php/testutil/IntegrationDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function aNewSchema() {
public function aNewInitializedSchema() {
$pdo = $this->aNewSchema();

(new Initialize($pdo))->createInstalledMigrationsTable();
(new Initialize($pdo))->__invoke();

return $pdo;
}
Expand Down

0 comments on commit 04bf1bb

Please sign in to comment.