Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Phpmig/Api/PhpmigApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,51 @@ public function getVersion()
}
return 0;
}

/**
* Check every migration, load and install it if missing.
*/
public function migrateMissing()
{
$adapter = !empty($this->container['phpmig.adapter']) ? $this->container['phpmig.adapter'] : null;

if ($adapter == null) {

throw new RuntimeException("The container must contain a phpmig.adapter key!");
}

if (!$adapter->hasSchema()) {

$this->container['phpmig.adapter']->createSchema();
}

foreach ($this->getMissingMigrations() as $migration) {
$this->container['phpmig.migrator']->up($migration);
}
}
/**
* Load all migrations which are not yet installed
*
* @return array An array of Phpmig\Migration\Migration objects to process
*/
public function getMissingMigrations()
{
$versions = $this->container['phpmig.adapter']->fetchAll();

$migrations = array();

foreach ($this->migrations as $path) {
preg_match('/^[0-9]+/', basename($path), $matches);
if (!array_key_exists(0, $matches)) {
continue;
}

$version = $matches[0];
if (!in_array($version, $versions)) {
$migrations[] = $path;
}
}

return $this->loadMigrations($migrations);
}
}