/** * run migrations * * @param array $migrations The table of migration files * @param Phigrate_BaseMigration $targetMethod The migration class * * @return void */ protected function _runMigrations($migrations, $targetMethod) { $this->_logger->debug(__METHOD__ . ' Start'); $lastVersion = -1; $objMigrations = array(); foreach ($migrations as $file) { $fullPath = $this->_migrationDir . '/' . $file['file']; $this->_logger->debug('file: ' . var_export($file, true)); if (!is_file($fullPath) || !is_readable($fullPath)) { continue; } $this->_logger->debug('include file: ' . $file['file']); include_once $fullPath; require_once 'Phigrate/Util/Naming.php'; $class = Phigrate_Util_Naming::classFromMigrationFile($file['file']); /** @param Phigrate_Migration_Base $obj */ $obj = new $class($this->_adapter); $refl = new ReflectionObject($obj); if (!$refl->hasMethod($targetMethod)) { $msg = $class . ' does not have (' . $targetMethod . ') method defined!'; $this->_logger->warn($msg); require_once 'Phigrate/Exception/MissingMigrationMethod.php'; throw new Phigrate_Exception_MissingMigrationMethod($msg); } $objMigrations[] = array('obj' => $obj, 'file' => $file); } try { //start transaction $this->_adapter->startTransaction(); $this->_logger->info('Start transaction called'); foreach ($objMigrations as $migration) { $start = microtime(true); try { $migration['obj']->{$targetMethod}(); } catch (Exception $e) { //wrap the caught exception in our own $msg = $migration['file']['class'] . ' - ' . $e->getMessage(); $this->_logger->err($msg); $this->_logger->debug('force: ' . var_export($this->_force, true)); // Migrations forcée ? if ($this->_force !== true) { throw new Phigrate_Exception($msg, $e->getCode(), $e); } $msgForce = $e->getMessage(); } $end = microtime(true); $diff = $this->_diffTimer($start, $end); $this->_return .= sprintf("========= %s ======== (%.2f)\n", $migration['file']['class'], $diff); // Ajout du message d'erreur en cas de forçage if ($this->_force && isset($msgForce)) { $this->_return .= "[1;31mError:[0m {$msgForce}\n\n"; unset($msgForce); } //successfully ran migration, update our version and commit $this->_migratorUtil->resolveCurrentVersion($migration['file']['version'], $targetMethod); $lastVersion = $migration['file']['version']; $this->_logger->info('last_version: ' . $lastVersion); } $this->_adapter->commitTransaction(); $this->_logger->info('Commit transaction called'); } catch (\Exception $e) { $this->_adapter->rollbackTransaction(); $this->_logger->info('Rollback transaction called'); throw $e; } //update the schema info $this->_logger->debug(__METHOD__ . ' End'); return array('last_version' => $lastVersion); }
/** * run migrations * * @param array $migrations The table of migration files * @param Phigrate_BaseMigration $targetMethod The migration class * * @return void */ protected function _runMigrations($migrations, $targetMethod) { $this->_logger->debug(__METHOD__ . ' Start'); $lastVersion = -1; $this->_return .= $this->_adapter->startTransaction() . "\n\n"; foreach ($migrations as $file) { $fullPath = $this->_migrationDir . '/' . $file['file']; $this->_logger->debug('file: ' . var_export($file, true)); if (!is_file($fullPath) || !is_readable($fullPath)) { continue; } $this->_logger->debug('include file: ' . $file['file']); include_once $fullPath; require_once 'Phigrate/Util/Naming.php'; $class = Phigrate_Util_Naming::classFromMigrationFile($file['file']); /** @param Phigrate_Migration_Base $obj */ $obj = new $class($this->_adapter); $refl = new ReflectionObject($obj); if (!$refl->hasMethod($targetMethod)) { $msg = $class . ' does not have (' . $targetMethod . ') method defined!'; $this->_logger->warn($msg); require_once 'Phigrate/Exception/MissingMigrationMethod.php'; throw new Phigrate_Exception_MissingMigrationMethod($msg); } $start = microtime(true); try { $obj->{$targetMethod}(); //successfully ran migration, update our version and commit $this->_migratorUtil->resolveCurrentVersion($file['version'], $targetMethod); } catch (\Exception $e) { //wrap the caught exception in our own $msg = $file['class'] . ' - ' . $e->getMessage(); $this->_logger->err($msg); throw new Phigrate_Exception($msg, $e->getCode(), $e); } $end = microtime(true); $diff = $this->_diffTimer($start, $end); $this->_return .= sprintf("-- ========= %s ======== (%.2f)\n", $file['class'], $diff); $this->_return .= $this->_adapter->getSql(); $this->_adapter->initSql(); $lastVersion = $file['version']; $this->_logger->info('last_version: ' . $lastVersion); } //foreach $this->_return .= $this->_adapter->commitTransaction(); //update the schema info $this->_logger->debug(__METHOD__ . ' End'); return array('last_version' => $lastVersion); }
/** */ public function testClassFromMigrationFile() { $fileName = '20090122193325_AddNewTable.php'; $expected = 'AddNewTable'; $actual = Phigrate_Util_Naming::classFromMigrationFile($fileName); $this->assertEquals($expected, $actual); $this->assertFalse(Phigrate_Util_Naming::classFromMigrationFile('AddNewTable.php')); $this->assertFalse(Phigrate_Util_Naming::classFromMigrationFile('20090122193325.php')); $klass = '001_CreateUsers.php'; $this->assertEquals('CreateUsers', Phigrate_Util_Naming::classFromMigrationFile($klass)); $klass = '120_AddIndexToPeopleTable.php'; $this->assertEquals('AddIndexToPeopleTable', Phigrate_Util_Naming::classFromMigrationFile($klass)); }