예제 #1
0
파일: Export.php 프로젝트: azema/phigrate
 /**
  * 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);
 }
예제 #2
0
파일: Manager.php 프로젝트: azema/phigrate
 /**
  * load all tasks
  *
  * @return void
  * @throws Phigrate_Exception_Argument
  */
 private function _loadAllTasks()
 {
     $this->_logger->debug(__METHOD__ . ' Start');
     if (!isset($this->_tasksDir)) {
         $msg = 'Please: you must specify the directory of tasks!';
         $this->_logger->err($msg);
         throw new Phigrate_Exception_Argument($msg);
     }
     foreach ($this->_tasksDir as $index => $taskDir) {
         $namespaces = scandir($taskDir);
         $this->_logger->debug('Tasks dir[' . $index . ']: ' . $taskDir);
         //$this->_tasks = array();
         foreach ($namespaces as $namespace) {
             //skip over invalid files
             if ($namespace == '.' || $namespace == '..' || !is_dir($taskDir . '/' . $namespace)) {
                 continue;
             }
             $this->_logger->debug('Namespace: ' . $namespace);
             $files = scandir($taskDir . '/' . $namespace);
             foreach ($files as $file) {
                 $ext = substr($file, -4);
                 //skip over invalid files
                 if ($file == '.' || $file == '..' || $ext != '.php') {
                     continue;
                 }
                 $this->_logger->debug('include ' . $namespace . '/' . $file);
                 require_once $taskDir . '/' . $namespace . '/' . $file;
                 $class = Phigrate_Util_Naming::classFromFileName($taskDir . '/' . $namespace . '/' . $file);
                 $refl = new ReflectionClass($class);
                 if ($refl->isInstantiable()) {
                     $this->_logger->debug('className ' . $class);
                     $taskName = Phigrate_Util_Naming::taskFromClassName($class);
                     $this->_logger->debug('TaskName: ' . $taskName);
                     $taskObj = new $class($this->getAdapter());
                     $this->_logger->debug('obj: ' . get_class($taskObj));
                     $this->registerTask($taskName, $taskObj);
                 }
             }
         }
     }
     $this->_logger->debug(__METHOD__ . ' End');
 }
예제 #3
0
파일: Adapter.php 프로젝트: azema/phigrate
 /**
  * _getIndexName
  *
  * @param string       $tableName  The table name
  * @param string|array $columnName The column name(s)
  * @param string       $tableRef   The table ref name
  * @param string|array $columnRef  The column ref name(s)
  * @param array        $options    The options definition of the index
  *
  * @return string
  */
 private function _getConstrainteName($tableName, $columnName, $tableRef, $columnRef, $options = array())
 {
     //did the user specify an index name?
     if (is_array($options) && array_key_exists('name', $options)) {
         $constrainteName = $options['name'];
     } else {
         $constrainteName = Phigrate_Util_Naming::constrainteName($tableName, $columnName, $tableRef, $columnRef);
     }
     return $constrainteName;
 }
예제 #4
0
파일: Migrate.php 프로젝트: azema/phigrate
 /**
  * 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 .= "Error: {$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);
 }
예제 #5
0
 /**
  */
 public function testIndexName()
 {
     $column = 'first_name';
     $this->assertEquals('idx_users_first_name', Phigrate_Util_Naming::indexName('users', $column));
     $column = 'age';
     $this->assertEquals('idx_users_age', Phigrate_Util_Naming::indexName('users', $column));
     $column = array('listing_id', 'review_id');
     $this->assertEquals('idx_users_listing_id_and_review_id', Phigrate_Util_Naming::indexName('users', $column));
     $column = array('listing_id', 'review_id');
     $this->assertEquals('idx_users_addresses_listing_id_and_review_id', Phigrate_Util_Naming::indexName('users__addresses', $column));
     $column = 'listing__id';
     $this->assertEquals('idx_users_addresses_listing_id', Phigrate_Util_Naming::indexName('users__addresses', $column));
 }