/** * Applies given $migration to database * * @param AbstractMigration $migration * @throws \Exception */ public function apply(AbstractMigration $migration) { $sql = 'REPLACE s_schema_version (version, start_date, name) VALUES (:version, :date, :name)'; $stmt = $this->connection->prepare($sql); $stmt->execute(array(':version' => $migration->getVersion(), ':date' => date('Y-m-d H:i:s'), ':name' => $migration->getLabel())); try { $migration->up(); $sqls = $migration->getSql(); foreach ($sqls as $sql) { $this->connection->exec($sql); } } catch (\Exception $e) { $updateVersionSql = 'UPDATE s_schema_version SET error_msg = :msg WHERE version = :version'; $stmt = $this->connection->prepare($updateVersionSql); $stmt->execute(array(':version' => $migration->getVersion(), ':msg' => $e->getMessage())); throw new \Exception("Could not apply migration: " . $e->getMessage()); } $sql = 'UPDATE s_schema_version SET complete_date = :date WHERE version = :version'; $stmt = $this->connection->prepare($sql); $stmt->execute(array(':version' => $migration->getVersion(), ':date' => date('Y-m-d H:i:s'))); }
public function addMigration(AbstractMigration $migration) { $this->migrations[$migration->getVersion()] = $migration; ksort($this->migrations); }