Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Implemented "down" without "force" flag #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion src/ZfSimpleMigrations/Controller/MigrateController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function applyAction()
$force = $this->getRequest()->getParam('force');
$down = $this->getRequest()->getParam('down');
$fake = $this->getRequest()->getParam('fake');
$name = $this->getRequest()->getParam('name');

if (is_null($version) && $force) {
return "Can't force migration apply without migration version explicitly set.";
Expand Down
15 changes: 9 additions & 6 deletions src/ZfSimpleMigrations/Library/Migration.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ public function getCurrentVersion()
*/
public function migrate($version = null, $force = false, $down = false, $fake = false)
{
$migrations = $this->getMigrationClasses($force);
$migrations = $this->getMigrationClasses($force, $down);

if (!is_null($version) && !$this->hasMigrationVersions($migrations, $version)) {
throw new MigrationException(sprintf('Migration version %s is not found!', $version));
}

$currentMigrationVersion = $this->migrationVersionTable->getCurrentVersion();
if (!is_null($version) && $version == $currentMigrationVersion && !$force) {
if (!is_null($version) && $version == $currentMigrationVersion && !$force && !$down) {
throw new MigrationException(sprintf('Migration version %s is current version!', $version));
}

if ($version && $force) {
if ($version && ($down || $force)) {
foreach ($migrations as $migration) {
if ($migration['version'] == $version) {
// if existing migration is forced to apply - delete its information from migrated
Expand Down Expand Up @@ -208,19 +208,22 @@ public function getMaxMigrationVersion(\ArrayIterator $migrations)
}

/**
* @param bool $all
* @param bool $force
* @param bool $down
* @return \ArrayIterator
*/
public function getMigrationClasses($all = false)
public function getMigrationClasses($force = false, $down = false)
{
$classes = new \ArrayIterator();

$iterator = new \GlobIterator(sprintf('%s/Version*.php', $this->migrationsDir), \FilesystemIterator::KEY_AS_FILENAME);

foreach ($iterator as $item) {
/** @var $item \SplFileInfo */
if (preg_match('/(Version(\d+))\.php/', $item->getFilename(), $matches)) {
$applied = $this->migrationVersionTable->applied($matches[2]);
if ($all || !$applied) {

if ($force || !$applied || $down) {
$className = $this->migrationsNamespace . '\\' . $matches[1];

if (!class_exists($className))
Expand Down
Empty file modified src/ZfSimpleMigrations/Model/MigrationVersionTable.php
100644 → 100755
Empty file.