2016-10-13 85 views
0

我正在使用Phinx執行跨多個服務器上的應用程序的遷移。每個應用程序都應執行相同的遷移。自定義Phinx Symfony命令與多個應用程序引導

爲了做到這一點,中央服務器上有一個應用程序實例,它知道所有配置和執行引導過程所需的其他信息(這是基於applicationId完成的)。

該中央實例(我們稱之爲adminapp)執行命令並通過STDIN接收applicationId,然後執行引導應用程序並運行遷移命令的循環。

<?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希望它的配置以配置文件的形式出現。我試圖做的是重用DB連接資源(PDO),並將其與db名稱一起傳送給Phinx命令Phinx\Console\Command\Migrate

我在Phinx的文檔中看到這是一個PHP配置文件的選項,但是我找不到在飛行中執行此操作的方法(在Phinx\Console\Command\Migrate類初始化期間)。

Phinx DOC提示:

require 'app/init.php'; 

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

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

有沒有辦法,不可怕黑客以PDO連接資源和數據庫名稱傳遞給\Phinx\Console\Command\Migrate

回答

0

我結束了擴展Phinx配置類\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'), 
      ] 
     ] 
    ] 
)); 
相關問題