/** * check db state in last migration, if state is empty * save current db state to migration * @param $module * @return bool */ public function checkState($module) { $lastMigration = $this->getLastMigration($module); $dbAdapter = Zend_Db_Table::getDefaultAdapter(); $dbState = $this->getLastDbState($module); if (!$dbState) { $db = new Core_Db_Database(); $dbAdapter->update($this->_options['migrationsSchemaTable'], array('state' => $db->toString()), array($dbAdapter->quoteInto('migration=?', $lastMigration), $dbAdapter->quoteInto('module=?', $module))); return true; } return false; }
/** * Method upgrade all migration or migrations to selected * * @param string $module Module name * @param string $to Migration name or label * @throws Core_Exception * @return void */ public function up($module = null, $to = null) { $lastMigration = $this->getLastMigration($module); $lastMigration = $lastMigration['migration']; if ($fullMigrationName = $this->getMigrationFullName($to, $module)) { $to = $fullMigrationName; } if ($to) { if (!self::isMigration($to)) { throw new Core_Exception("Migration name `{$to}` is not valid"); } elseif ($lastMigration == $to) { throw new Core_Exception("Migration `{$to}` is current"); } elseif ($lastMigration > $to) { throw new Core_Exception("Migration `{$to}` is older than current " . "migration `{$lastMigration}`"); } } $exists = $this->getExistsMigrations($module); $loaded = $this->getLoadedMigrations($module); $ready = array_diff($exists, $loaded); if (sizeof($ready) == 0) { if ($module) { array_push($this->_messages, $module . ': no migrations to upgrade.'); } else { array_push($this->_messages, 'No migrations to upgrade.'); } return; } sort($ready); if ($to && !in_array($to, $exists)) { throw new Core_Exception("Migration `{$to}` not exists"); } @set_time_limit(0); foreach ($ready as $migration) { if ($migration < $lastMigration) { throw new Core_Exception("Migration `{$migration}` is conflicted"); } try { $includePath = $this->getMigrationsDirectoryPath($module) . '/' . $migration . '.php'; include_once $includePath; $moduleAddon = null !== $module ? ucfirst($module) . '_' : ''; $migrationClass = $moduleAddon . 'Migration_' . $migration; /** @var Core_Migration_Abstract $migrationObject */ $migrationObject = new $migrationClass(); $migrationObject->setMigrationMananger($this); if (!$this->_transactionFlag) { $migrationObject->getDbAdapter()->beginTransaction(); $this->_transactionFlag = true; try { $migrationObject->up(); $migrationObject->getDbAdapter()->commit(); } catch (Exception $e) { $migrationObject->getDbAdapter()->rollBack(); throw new Core_Exception($e->getMessage()); } $this->_transactionFlag = false; } else { $migrationObject->up(); } if ($module) { array_push($this->_messages, $module . ": upgrade to revision `{$migration}`"); } else { array_push($this->_messages, "Upgrade to revision `{$migration}`"); } $this->_pushMigration($module, $migration); // add db state to migration $db = new Core_Db_Database(array('blacklist' => $this->_options['migrationsSchemaTable'])); $dbAdapter = Zend_Db_Table::getDefaultAdapter(); $dbAdapter->update($this->_options['migrationsSchemaTable'], array('state' => $db->toString()), array($dbAdapter->quoteInto('migration=?', $migration), $dbAdapter->quoteInto('module=?', $module))); } catch (Exception $e) { throw new Core_Exception("Migration `{$migration}` return exception:\n" . $e->getMessage()); } if ($to && $migration == $to) { break; } } }