Пример #1
0
 /**
  * Executes the migration of all the App's database info. First tries to install every
  * migration inside the /migrations directory. Then executes the code inside the migration
  * methods of the AtuinConfig file. 
  * 
  * @return bool
  * @throws yii\base\Exception
  * @throws yii\db\Exception
  */
 protected function executeMigration()
 {
     $transaction = Yii::$app->db->beginTransaction();
     try {
         /**
          * Migration based in the Yii2 Migration System (only applies for up and down since 
          * it doesn't have support for updates).
          */
         if ($this->preMethod != 'update') {
             // First we will try to search for migration files at the Apps' migration subdirectory.
             $baseDirectoryList = [];
             $baseDirectoryList[] = $this->module->getModuleDirectory();
             if (method_exists($this->module, 'getComposerPackageData')) {
                 $baseDirectoryList[] = Yii::$app->getVendorPath() . '/' . $this->module->getComposerPackageData(TRUE);
             }
             foreach ($baseDirectoryList as $baseDirectory) {
                 $filesystem = new Filesystem(new Adapter($baseDirectory));
                 if ($filesystem->has($this->module->getMigrationsDirectory()) && FactoryCommandHelper::migration()->check()) {
                     FactoryCommandHelper::migration()->execute($baseDirectory . '/' . $this->module->getMigrationsDirectory(), $this->preMethod);
                 }
             }
         }
         /**
          * Migration based in the AtuinConfig file, will be called in every action, including updates.
          */
         $methodParams = NULL;
         // Declare the parameters passed into the method
         if ($this->preMethod == 'update') {
             $methodParams = $this->app->version;
         }
         // calling the method
         // since we are calling yii2 Migration AND has a lot of echos, let's capture the string output
         ob_start();
         $execution = call_user_func([$this->installationObject, $this->preMethod . 'Migration'], $methodParams);
         $outputText = ob_get_contents();
         if ($execution === FALSE) {
             $transaction->rollBack();
             return FALSE;
         }
         $transaction->commit();
         ob_end_clean();
     } catch (\Exception $e) {
         $transaction->rollBack();
         ob_end_clean();
         throw new yii\base\Exception($e->getMessage());
     }
 }