Skip to content

Commit

Permalink
Merge pull request #1 from aptvision/master
Browse files Browse the repository at this point in the history
Add support for Postgres databases and other SQL databases
  • Loading branch information
newcron authored Dec 21, 2017
2 parents ada8adc + 29cb6fe commit 994ea4f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 15 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dbmigrate - Database Migration Tool for MySQL
# dbmigrate - Database Migration Tool

dbmigrate is a simple utility to automate changes to your database (schema and contents). This tool is typically
helpful for automating code deployments or in scenarios, where it is necessary to audit database changes. It's an alternative
Expand All @@ -8,6 +8,11 @@ in your hands and like the simplicity of versioned SQL files, then this tool mig

dbmigrate is heavily inspired from [Flyway](http://flywaydb.org/).

dbmigrate currently supports these database engines:

* MySQL
* PostgreSQL

### How does it work

All your migration SQL files will be stored in one directory (let's call it the *migrations directory*).
Expand Down Expand Up @@ -66,6 +71,10 @@ $pdo = new PDO("mysql:host=yourdbhost;database=yourdb", "youruser", "yourpass");
call_user_func(new \dbmigrate\Migrate($pdo, new \SplFileInfo("/path/to/your/sql/folder")));
```

All the sql files in the "/path/to/your/sql/folder" directory will be read and run against the
database. Each file will be run inside a single transaction, if anything within that file
fails then all commands in that file will be rolled back.

##### Dry-Running Migrations

If you just want to know if your new migration *would* be going to be installed, you can perform a
Expand Down
21 changes: 19 additions & 2 deletions src/php/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,42 @@
use dbmigrate\application\sql\MigrationTablePresenceConstraint;
use dbmigrate\application\sql\RunMigration;
use dbmigrate\application\sql\SqlFile;
use PDO;

class Initialize
{
/**
* @var string
*/
private $databaseEngine;

/**
* @var MigrationTablePresenceConstraint
*/
private $migrationTablePresenceConstraint;

/** @var RunMigration */
private $runMigration;

public function __construct(\PDO $pdo)
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$this->databaseEngine = strtolower($pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
$this->runMigration = new RunMigration($pdo);
$this->migrationTablePresenceConstraint = new MigrationTablePresenceConstraint($pdo);
}


public function __invoke()
{
$initScript = __DIR__ . "/../sql/init-{$this->databaseEngine}.sql";
if (!file_exists($initScript)) {
throw new \Exception("Unsupported database engine: {$this->databaseEngine}. Create a src/sql/init-{$this->databaseEngine}.sql script to support it");
}

$this->migrationTablePresenceConstraint->assertTableMissing();
$this->runMigration->run(new SqlFile(new \SplFileInfo(__DIR__ . "/../sql/init.sql")));
$this->runMigration->run(new SqlFile(new \SplFileInfo($initScript)));
$this->migrationTablePresenceConstraint->assertTablePresent();
}
}
}
2 changes: 2 additions & 0 deletions src/php/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use dbmigrate\application\sql\LoadMigrations;
use dbmigrate\application\sql\LogMigration;
use dbmigrate\application\sql\RunMigration;
use PDO;

class Migrate
{
Expand All @@ -28,6 +29,7 @@ class Migrate
*/
public function __construct(\PDO $pdo, \SplFileInfo $sqlDirectory)
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

Expand Down
3 changes: 3 additions & 0 deletions src/php/MigrateDryRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use dbmigrate\application\Planner;
use dbmigrate\application\sql\LoadMigrations;
use dbmigrate\application\sql\SqlFile;
use PDO;

class MigrateDryRun
{
Expand All @@ -20,6 +21,8 @@ class MigrateDryRun

public function __construct(\PDO $pdo, \SplFileInfo $sqlDirectory)
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

$this->pdo = $pdo;
Expand Down
10 changes: 3 additions & 7 deletions src/php/application/sql/MigrationTablePresenceConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ public function assertTableMissing()
*/
private function countRows()
{

$statement = $this->pdo->query("show tables like \"installed_migrations\";");
try {
return $statement->rowCount();
} finally {
$statement->closeCursor();
}
return $this->pdo
->query("select count(*) from information_schema.tables where table_name = 'installed_migrations';")
->fetchColumn();
}
}
13 changes: 8 additions & 5 deletions src/php/application/sql/RunMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ public function __construct(\PDO $pdo)

public function run(SqlFile $file)
{
$statement = $this->pdo->prepare($file->getContents());
$this->pdo->beginTransaction();

try {
$result = $statement->execute();
$result = $this->pdo->query($file->getContents());
if ($result === false) {
$this->pdo->rollBack();
throw new \Exception("Query Returned " . var_export($this->pdo->errorInfo(), true));
}
} catch (\Exception $e) {
throw new MigrationException("Running SQL File " . $file->getFile()->getPathname() . " failed.", $e);
} finally {
$statement->closeCursor();
$this->pdo->rollBack();
throw new MigrationException("Error running SQL File " . $file->getFile()->getPathname() . " failed: ".$e->getMessage(), $e);
}

$this->pdo->commit();
}

}
File renamed without changes.
8 changes: 8 additions & 0 deletions src/sql/init-pgsql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table installed_migrations (
id SERIAL,
installation_time timestamp DEFAULT current_timestamp,
migration_file_name varchar(255) NOT NULL,
migration_file_checksum varchar(32) NOT NULL,
success boolean,
PRIMARY KEY (id)
)

0 comments on commit 994ea4f

Please sign in to comment.