Skip to content

Commit 994ea4f

Browse files
authored
Merge pull request #1 from aptvision/master
Add support for Postgres databases and other SQL databases
2 parents ada8adc + 29cb6fe commit 994ea4f

File tree

8 files changed

+53
-15
lines changed

8 files changed

+53
-15
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# dbmigrate - Database Migration Tool for MySQL
1+
# dbmigrate - Database Migration Tool
22

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

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

11+
dbmigrate currently supports these database engines:
12+
13+
* MySQL
14+
* PostgreSQL
15+
1116
### How does it work
1217

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

74+
All the sql files in the "/path/to/your/sql/folder" directory will be read and run against the
75+
database. Each file will be run inside a single transaction, if anything within that file
76+
fails then all commands in that file will be rolled back.
77+
6978
##### Dry-Running Migrations
7079

7180
If you just want to know if your new migration *would* be going to be installed, you can perform a

src/php/Initialize.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,42 @@
77
use dbmigrate\application\sql\MigrationTablePresenceConstraint;
88
use dbmigrate\application\sql\RunMigration;
99
use dbmigrate\application\sql\SqlFile;
10+
use PDO;
1011

1112
class Initialize
1213
{
14+
/**
15+
* @var string
16+
*/
17+
private $databaseEngine;
18+
19+
/**
20+
* @var MigrationTablePresenceConstraint
21+
*/
1322
private $migrationTablePresenceConstraint;
1423

1524
/** @var RunMigration */
1625
private $runMigration;
1726

1827
public function __construct(\PDO $pdo)
1928
{
29+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30+
31+
$this->databaseEngine = strtolower($pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
2032
$this->runMigration = new RunMigration($pdo);
2133
$this->migrationTablePresenceConstraint = new MigrationTablePresenceConstraint($pdo);
2234
}
2335

2436

2537
public function __invoke()
2638
{
39+
$initScript = __DIR__ . "/../sql/init-{$this->databaseEngine}.sql";
40+
if (!file_exists($initScript)) {
41+
throw new \Exception("Unsupported database engine: {$this->databaseEngine}. Create a src/sql/init-{$this->databaseEngine}.sql script to support it");
42+
}
43+
2744
$this->migrationTablePresenceConstraint->assertTableMissing();
28-
$this->runMigration->run(new SqlFile(new \SplFileInfo(__DIR__ . "/../sql/init.sql")));
45+
$this->runMigration->run(new SqlFile(new \SplFileInfo($initScript)));
2946
$this->migrationTablePresenceConstraint->assertTablePresent();
3047
}
31-
}
48+
}

src/php/Migrate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use dbmigrate\application\sql\LoadMigrations;
1313
use dbmigrate\application\sql\LogMigration;
1414
use dbmigrate\application\sql\RunMigration;
15+
use PDO;
1516

1617
class Migrate
1718
{
@@ -28,6 +29,7 @@ class Migrate
2829
*/
2930
public function __construct(\PDO $pdo, \SplFileInfo $sqlDirectory)
3031
{
32+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
3133

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

src/php/MigrateDryRun.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use dbmigrate\application\Planner;
1010
use dbmigrate\application\sql\LoadMigrations;
1111
use dbmigrate\application\sql\SqlFile;
12+
use PDO;
1213

1314
class MigrateDryRun
1415
{
@@ -20,6 +21,8 @@ class MigrateDryRun
2021

2122
public function __construct(\PDO $pdo, \SplFileInfo $sqlDirectory)
2223
{
24+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
25+
2326
(new MigrationDirectoryValidator())->assertValidMigrationFileDirectory($sqlDirectory);
2427

2528
$this->pdo = $pdo;

src/php/application/sql/MigrationTablePresenceConstraint.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ public function assertTableMissing()
3939
*/
4040
private function countRows()
4141
{
42-
43-
$statement = $this->pdo->query("show tables like \"installed_migrations\";");
44-
try {
45-
return $statement->rowCount();
46-
} finally {
47-
$statement->closeCursor();
48-
}
42+
return $this->pdo
43+
->query("select count(*) from information_schema.tables where table_name = 'installed_migrations';")
44+
->fetchColumn();
4945
}
5046
}

src/php/application/sql/RunMigration.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ public function __construct(\PDO $pdo)
2727

2828
public function run(SqlFile $file)
2929
{
30-
$statement = $this->pdo->prepare($file->getContents());
30+
$this->pdo->beginTransaction();
31+
3132
try {
32-
$result = $statement->execute();
33+
$result = $this->pdo->query($file->getContents());
3334
if ($result === false) {
35+
$this->pdo->rollBack();
3436
throw new \Exception("Query Returned " . var_export($this->pdo->errorInfo(), true));
3537
}
3638
} catch (\Exception $e) {
37-
throw new MigrationException("Running SQL File " . $file->getFile()->getPathname() . " failed.", $e);
38-
} finally {
39-
$statement->closeCursor();
39+
$this->pdo->rollBack();
40+
throw new MigrationException("Error running SQL File " . $file->getFile()->getPathname() . " failed: ".$e->getMessage(), $e);
4041
}
42+
43+
$this->pdo->commit();
4144
}
4245

4346
}
File renamed without changes.

src/sql/init-pgsql.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
create table installed_migrations (
2+
id SERIAL,
3+
installation_time timestamp DEFAULT current_timestamp,
4+
migration_file_name varchar(255) NOT NULL,
5+
migration_file_checksum varchar(32) NOT NULL,
6+
success boolean,
7+
PRIMARY KEY (id)
8+
)

0 commit comments

Comments
 (0)