全局增加config参数,用于指定数据库配置项名称,可根据config参数独立执行不同数据库的迁移命令。

This commit is contained in:
tale 2017-03-27 15:26:40 +08:00 committed by 云无心
parent 2281f0899e
commit b111afc07a
8 changed files with 37 additions and 13 deletions

View File

@ -6,16 +6,18 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration; namespace think\migration;
use InvalidArgumentException; use InvalidArgumentException;
use Phinx\Db\Adapter\AdapterFactory; use Phinx\Db\Adapter\AdapterFactory;
use think\Config; use think\Config;
use think\Db;
abstract class Command extends \think\console\Command abstract class Command extends \think\console\Command
{ {
protected $config = 'database';
public function getAdapter() public function getAdapter()
{ {
@ -42,7 +44,8 @@ abstract class Command extends \think\console\Command
*/ */
protected function getDbConfig() protected function getDbConfig()
{ {
$config = Config::get('database'); $config = Db::connect($this->config)->getConfig();
if ($config['deploy'] == 0) { if ($config['deploy'] == 0) {
$dbConfig = [ $dbConfig = [
'adapter' => $config['type'], 'adapter' => $config['type'],
@ -52,7 +55,7 @@ abstract class Command extends \think\console\Command
'pass' => $config['password'], 'pass' => $config['password'],
'port' => $config['hostport'], 'port' => $config['hostport'],
'charset' => $config['charset'], 'charset' => $config['charset'],
'table_prefix' => $config['prefix'] 'table_prefix' => $config['prefix'],
]; ];
} else { } else {
$dbConfig = [ $dbConfig = [
@ -63,7 +66,7 @@ abstract class Command extends \think\console\Command
'pass' => explode(',', $config['password'])[0], 'pass' => explode(',', $config['password'])[0],
'port' => explode(',', $config['hostport'])[0], 'port' => explode(',', $config['hostport'])[0],
'charset' => explode(',', $config['charset'])[0], 'charset' => explode(',', $config['charset'])[0],
'table_prefix' => explode(',', $config['prefix'])[0] 'table_prefix' => explode(',', $config['prefix'])[0],
]; ];
} }

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration; namespace think\migration;

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration; namespace think\migration;

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration\command; namespace think\migration\command;
@ -16,6 +16,9 @@ use Phinx\Db\Adapter\ProxyAdapter;
use Phinx\Migration\AbstractMigration; use Phinx\Migration\AbstractMigration;
use Phinx\Migration\MigrationInterface; use Phinx\Migration\MigrationInterface;
use Phinx\Util\Util; use Phinx\Util\Util;
use think\console\Input;
use think\console\input\Option as InputOption;
use think\console\Output;
use think\migration\Command; use think\migration\Command;
use think\migration\Migrator; use think\migration\Migrator;
@ -26,9 +29,27 @@ abstract class Migrate extends Command
*/ */
protected $migrations; protected $migrations;
public function __construct($name = null)
{
parent::__construct($name);
$this->addOption('--config', null, InputOption::VALUE_REQUIRED, 'The database config name', 'database');
}
/**
* 初始化
* @param Input $input An InputInterface instance
* @param Output $output An OutputInterface instance
*/
protected function initialize(Input $input, Output $output)
{
$this->config = $input->getOption('config');
}
protected function getPath() protected function getPath()
{ {
return $this->getConfig('path', ROOT_PATH . 'database') . DS . 'migrations'; return $this->getConfig('path', ROOT_PATH . 'database') . DS . 'migrations' . ($this->config !== 'database' ? DS . $this->config : '');
} }
protected function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP) protected function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP)
@ -75,7 +96,7 @@ abstract class Migrate extends Command
// Record it in the database // Record it in the database
$this->getAdapter() $this->getAdapter()
->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time())); ->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
$end = microtime(true); $end = microtime(true);

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration\command; namespace think\migration\command;

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration\command\migrate; namespace think\migration\command\migrate;

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration\db; namespace think\migration\db;

View File

@ -6,7 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>  // | Author: yunwuxin <448901948@qq.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\migration\db; namespace think\migration\db;