2016-10-13 3 views
0

私はPhinxを使用して、複数のサーバ上の100種類のアプリケーション間で移行を実行しています。すべてのアプリケーションが同じ移行を実行する必要があります。カスタマイズされたPhinx Symfonyコマンドと複数のアプリケーションブートストラップ

これを実行するには、ブートストラッププロセスを実行するために必要なすべての設定とその他の情報(applicationIdに基づいて実行されている)を認識している、中央サーバ上のappインスタンスがあります。

このセントラルインスタンス(adminappと呼ぶ)はコマンドを実行し、STDINを通じてapplicationIdsを受け取り、アプリケーションをブートストラップして移行コマンドを実行するループを実行します。

<?php 
namespace Command\Db; 

use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 
use App\Command\AppCommand; 

class MigrateBulkCommand extends AppCommand 
{ 

    protected function configure() 
    { 
     $this 
      ->setName('command:blah') 
      ->setDescription('Executes SQL migrations accross multiple applications. Expects ApplicationIDs to be passed as new line delimited string on STDIN.') 
     ; 
    } 

    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     $stdin = $this->getStdin(); 
     if ($stdin === false) { 
      throw new \RuntimeException("Bulk migration command requires applicationIds to be passed to STDIN."); 
     } 
     $applicationIds = explode("\n", $stdin); 

     foreach($applicationIds as $applicationId) { 
      try { 
       $this->bootstrap($applicationId); 
      } catch (\Exception $e) { 
       $output->writeln(sprintf("<error>Bootstrap process failed for applicationId `%s`</error>", $applicationId)); 
      } 
      $command = new \Phinx\Console\Command\Migrate(); 
      $migrationInput = new \Symfony\Component\Console\Input\ArrayInput([ 

      ]); 
      $returnCode = $command->run($migrationInput, $output); 
      $output->writeln(sprinf("<info>Migrations for applicationId `%s` executed successfully.</info>", $applicationId)); 
     } 
    } 

} 

今、Phinxは設定がconfigファイルの形で存在することを期待しています。私がやろうとしているのは、DB接続リソース(PDO)を再利用し、db nameと共にPhyxコマンドPhinx\Console\Command\Migrateをオンザフライで渡すことです。

Phinxのドキュメントでは、これはPHP設定ファイルのオプションであることがわかりましたが、私はその場でこれを行う方法を見つけることができません(Phinx\Console\Command\Migrateクラスの初期化中)。

Phinxドキュメントが示唆:

require 'app/init.php'; 

global $app; 
$pdo = $app->getDatabase()->getPdo(); 

return array('environments' => 
     array(
      'default_database' => 'development', 
      'development' => array(
      'name' => 'devdb', 
      'connection' => $pdo 
      ) 
     ) 
     ); 

\Phinx\Console\Command\Migrate

答えて

0

にPDO接続リソースとDB名を渡すことが恐ろしいのハッキングなしの方法は、あります私はPhinx Configクラス\Phinx\Config\Configを拡張し、方法fromArrayを作成することになりました。

$command = new \Phinx\Console\Command\Migrate(); 
$command->setConfig(\MyNamespace\Config::fromArray(
    [ 
     'paths' => [ 
      'migrations' => APPLICATION_PATH . "/../db/migrations", 
      'seeds' => APPLICATION_PATH . "/../db/seeds" 
     ], 
     'environments' => [ 
      'default_database' => 'production', 
      'production' => [ 
       'name' => $db->get('dbname'), 
       'adapter' => 'mysql', 
       'host' => $db->get('host'), 
       'port' => $db->get('port'), 
       'user' => $db->get('username'), 
       'pass' => $db->get('password'), 
      ] 
     ] 
    ] 
)); 
関連する問題