From 01d2ba507251990ec49e4ee91bd5ef2c538063d8 Mon Sep 17 00:00:00 2001 From: Adrian Nachev Date: Wed, 23 Mar 2016 14:07:29 +0000 Subject: [PATCH] Changed behaviour so that the "down" method will execute without the need for the "--force" flag to be set Removed an unused variable --- .../Controller/MigrateController.php | 1 - src/ZfSimpleMigrations/Library/Migration.php | 15 +++++++++------ .../Model/MigrationVersionTable.php | 0 3 files changed, 9 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/ZfSimpleMigrations/Controller/MigrateController.php mode change 100644 => 100755 src/ZfSimpleMigrations/Library/Migration.php mode change 100644 => 100755 src/ZfSimpleMigrations/Model/MigrationVersionTable.php diff --git a/src/ZfSimpleMigrations/Controller/MigrateController.php b/src/ZfSimpleMigrations/Controller/MigrateController.php old mode 100644 new mode 100755 index 3ce6dae..83a763b --- a/src/ZfSimpleMigrations/Controller/MigrateController.php +++ b/src/ZfSimpleMigrations/Controller/MigrateController.php @@ -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."; diff --git a/src/ZfSimpleMigrations/Library/Migration.php b/src/ZfSimpleMigrations/Library/Migration.php old mode 100644 new mode 100755 index 5809e63..9ed0d42 --- a/src/ZfSimpleMigrations/Library/Migration.php +++ b/src/ZfSimpleMigrations/Library/Migration.php @@ -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 @@ -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)) diff --git a/src/ZfSimpleMigrations/Model/MigrationVersionTable.php b/src/ZfSimpleMigrations/Model/MigrationVersionTable.php old mode 100644 new mode 100755