/** * Applies given $migration to database * * @param AbstractMigration $migration * @param string $modus * @throws \Exception */ public function apply(AbstractMigration $migration, $modus = AbstractMigration::MODUS_INSTALL) { if (!($suffix = $this->getTableSuffix())) { return parent::apply($migration, $modus); } // if $sql = 'REPLACE `s_schema_version_' . $suffix . '` (version, start_date, name) VALUES (:version, :date, :name)'; $stmt = $this->connection->prepare($sql); $stmt->execute([':version' => $migration->getVersion(), ':date' => date('Y-m-d H:i:s'), ':name' => $migration->getLabel()]); try { $migration->up($modus); $sqls = $migration->getSql(); foreach ($sqls as $sql) { $this->connection->exec($sql); } } catch (\Exception $e) { $updateVersionSql = 'UPDATE `s_schema_version_' . $suffix . '` SET error_msg = :msg WHERE version = :version'; $stmt = $this->connection->prepare($updateVersionSql); $stmt->execute([':version' => $migration->getVersion(), ':msg' => $e->getMessage()]); throw new \Exception("Could not apply migration: " . $e->getMessage()); } $sql = 'UPDATE `s_schema_version_' . $suffix . '` SET complete_date = :date WHERE version = :version'; $stmt = $this->connection->prepare($sql); $stmt->execute([':version' => $migration->getVersion(), ':date' => date('Y-m-d H:i:s')]); }
/** * @param int $offset * @param int $totalCount * @return ErrorResult|FinishResult|ValidResult */ public function run($offset, $totalCount = null) { if ($offset == 0) { $this->migrationManager->createSchemaTable(); } $currentVersion = $this->migrationManager->getCurrentVersion(); if (!$totalCount) { $totalCount = count($this->migrationManager->getMigrationsForVersion($currentVersion)); } $migration = $this->migrationManager->getNextMigrationForVersion($currentVersion); if (null === $migration) { return new FinishResult($offset, $totalCount); } try { $this->migrationManager->apply($migration); } catch (\Exception $e) { $reflection = new \ReflectionClass(get_class($migration)); $classFile = $reflection->getFileName(); return new ErrorResult($e->getMessage(), $e, array('deltaFile' => $classFile, 'deltaVersion' => $migration->getVersion(), 'deltaLabel' => $migration->getLabel())); } return new ValidResult($offset + 1, $totalCount, array('deltaVersion' => $migration->getVersion(), 'deltaLabel' => $migration->getLabel())); }