Migrations: run initial schema file...
...when no migrations have ever been applied
This commit is contained in:
parent
c3ac57ede0
commit
afe0bfb373
|
@ -27,7 +27,9 @@ class Migration
|
||||||
public function apply(Db $connection)
|
public function apply(Db $connection)
|
||||||
{
|
{
|
||||||
$db = $connection->getDbAdapter();
|
$db = $connection->getDbAdapter();
|
||||||
$queries = preg_split('/[\n\s\t]*\;[\n\s\t]*/s', $this->sql, -1, PREG_SPLIT_NO_EMPTY);
|
|
||||||
|
// TODO: this is fagile and depends on accordingly written schema files:
|
||||||
|
$queries = preg_split('/[\n\s\t]*\;[\n\s\t]+/s', $this->sql, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
|
|
||||||
if (empty($queries)) {
|
if (empty($queries)) {
|
||||||
throw new IcingaException(
|
throw new IcingaException(
|
||||||
|
@ -37,21 +39,17 @@ class Migration
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$db->beginTransaction();
|
|
||||||
|
|
||||||
foreach ($queries as $query) {
|
foreach ($queries as $query) {
|
||||||
$db->exec($query);
|
$db->exec($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
$db->commit();
|
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
|
||||||
$db->rollback();
|
|
||||||
throw new IcingaException(
|
throw new IcingaException(
|
||||||
'Migration %d failed: %s',
|
'Migration %d failed (%s) while running %s',
|
||||||
$this->version,
|
$this->version,
|
||||||
$e->getMessage()
|
$e->getMessage(),
|
||||||
|
$query
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,11 @@ class Migrations
|
||||||
|
|
||||||
public function listPendingMigrations()
|
public function listPendingMigrations()
|
||||||
{
|
{
|
||||||
|
$lastMigration = $this->getLastMigrationNumber();
|
||||||
|
if ($lastMigration === 0) {
|
||||||
|
return array(0);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->listMigrationsAfter($this->getLastMigrationNumber());
|
return $this->listMigrationsAfter($this->getLastMigrationNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,11 +106,15 @@ class Migrations
|
||||||
|
|
||||||
public function loadMigrationFile($version)
|
public function loadMigrationFile($version)
|
||||||
{
|
{
|
||||||
|
if ($version === 0) {
|
||||||
|
$filename = $this->getFullSchemaFile();
|
||||||
|
} else {
|
||||||
$filename = sprintf(
|
$filename = sprintf(
|
||||||
'%s/upgrade_%d.sql',
|
'%s/upgrade_%d.sql',
|
||||||
$this->getMigrationsDir(),
|
$this->getMigrationsDir(),
|
||||||
$version
|
$version
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return file_get_contents($filename);
|
return file_get_contents($filename);
|
||||||
}
|
}
|
||||||
|
@ -133,4 +142,12 @@ class Migrations
|
||||||
|
|
||||||
return $this->migrationsDir;
|
return $this->migrationsDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getFullSchemaFile()
|
||||||
|
{
|
||||||
|
return dirname(dirname(dirname(__DIR__)))
|
||||||
|
. '/schema/'
|
||||||
|
. $this->connection->getDbType()
|
||||||
|
. '.sql';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue