From 0eb15e9cabb7157b0326f7c94c732de393cf4890 Mon Sep 17 00:00:00 2001 From: tangtz Date: Tue, 23 Oct 2018 15:47:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Edb=20connection=20=E5=8F=AF?= =?UTF-8?q?=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Command.php | 14 +++++++++++++- src/command/migrate/Breakpoint.php | 8 ++++++-- src/command/migrate/Rollback.php | 10 +++++++--- src/command/migrate/Run.php | 8 ++++++-- src/command/migrate/Status.php | 6 +++++- src/command/seed/Run.php | 6 +++++- 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/Command.php b/src/Command.php index 73313c4..90fb8ab 100644 --- a/src/Command.php +++ b/src/Command.php @@ -18,6 +18,18 @@ abstract class Command extends \think\console\Command { + protected $connection = ''; + + public function setConnection($connection = '') + { + $this->connection = $connection; + } + + public function getConnection() + { + return $this->connection; + } + public function getAdapter() { if (isset($this->adapter)) { @@ -43,7 +55,7 @@ public function getAdapter() */ protected function getDbConfig() { - $config = Db::connect()->getConfig(); + $config = Db::connect($this->getConnection())->getConfig(); if (0 == $config['deploy']) { $dbConfig = [ diff --git a/src/command/migrate/Breakpoint.php b/src/command/migrate/Breakpoint.php index 0010080..abda3db 100644 --- a/src/command/migrate/Breakpoint.php +++ b/src/command/migrate/Breakpoint.php @@ -24,6 +24,7 @@ protected function configure() ->setDescription('Manage breakpoints') ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to set or clear a breakpoint against') ->addOption('--remove-all', '-r', InputOption::VALUE_NONE, 'Remove all breakpoints') + ->addOption('--connection', '-c', InputOption::VALUE_REQUIRED, 'The database connection to migrate to') ->setHelp(<<breakpoint command allows you to set or clear a breakpoint against a specific target to inhibit rollbacks beyond a certain target. If no target is supplied then the most recent migration will be used. @@ -38,8 +39,11 @@ protected function configure() protected function execute(Input $input, Output $output) { - $version = $input->getOption('target'); - $removeAll = $input->getOption('remove-all'); + $version = $input->getOption('target'); + $removeAll = $input->getOption('remove-all'); + $connection = $input->getOption('connection'); + + $this->setConnection($connection); if ($version && $removeAll) { throw new \InvalidArgumentException('Cannot toggle a breakpoint and remove all breakpoints at the same time.'); diff --git a/src/command/migrate/Rollback.php b/src/command/migrate/Rollback.php index 07826a6..bb1a6fe 100644 --- a/src/command/migrate/Rollback.php +++ b/src/command/migrate/Rollback.php @@ -27,6 +27,7 @@ protected function configure() ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to rollback to') ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to rollback to') ->addOption('--force', '-f', InputOption::VALUE_NONE, 'Force rollback to ignore breakpoints') + ->addOption('--connection', '-c', InputOption::VALUE_REQUIRED, 'The database connection to migrate to') ->setHelp(<<migrate:rollback command reverts the last migration, or optionally up to a specific version @@ -48,9 +49,12 @@ protected function configure() */ protected function execute(Input $input, Output $output) { - $version = $input->getOption('target'); - $date = $input->getOption('date'); - $force = !!$input->getOption('force'); + $version = $input->getOption('target'); + $date = $input->getOption('date'); + $force = !!$input->getOption('force'); + $connection = $input->getOption('connection'); + + $this->setConnection($connection); // rollback the specified environment $start = microtime(true); diff --git a/src/command/migrate/Run.php b/src/command/migrate/Run.php index c0fbeec..89a0258 100644 --- a/src/command/migrate/Run.php +++ b/src/command/migrate/Run.php @@ -26,6 +26,7 @@ protected function configure() ->setDescription('Migrate the database') ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to') ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to') + ->addOption('--connection', '-c', InputOption::VALUE_REQUIRED, 'The database connection to migrate to') ->setHelp(<<migrate:run command runs all available migrations, optionally up to a specific version @@ -47,8 +48,11 @@ protected function configure() */ protected function execute(Input $input, Output $output) { - $version = $input->getOption('target'); - $date = $input->getOption('date'); + $version = $input->getOption('target'); + $date = $input->getOption('date'); + $connection = $input->getOption('connection'); + + $this->setConnection($connection); // run the migrations $start = microtime(true); diff --git a/src/command/migrate/Status.php b/src/command/migrate/Status.php index 5bad619..83095c2 100644 --- a/src/command/migrate/Status.php +++ b/src/command/migrate/Status.php @@ -24,6 +24,7 @@ protected function configure() $this->setName('migrate:status') ->setDescription('Show migration status') ->addOption('--format', '-f', InputOption::VALUE_REQUIRED, 'The output format: text or json. Defaults to text.') + ->addOption('--connection', '-c', InputOption::VALUE_REQUIRED, 'The database connection to migrate to') ->setHelp(<<migrate:status command prints a list of all migrations, along with their current status @@ -42,7 +43,10 @@ protected function configure() */ protected function execute(Input $input, Output $output) { - $format = $input->getOption('format'); + $format = $input->getOption('format'); + $connection = $input->getOption('connection'); + + $this->setConnection($connection); if (null !== $format) { $output->writeln('using format ' . $format); diff --git a/src/command/seed/Run.php b/src/command/seed/Run.php index ee7ced9..e403fe9 100644 --- a/src/command/seed/Run.php +++ b/src/command/seed/Run.php @@ -25,6 +25,7 @@ protected function configure() $this->setName('seed:run') ->setDescription('Run database seeders') ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED, 'What is the name of the seeder?') + ->addOption('--connection', '-c', InputOption::VALUE_REQUIRED, 'The database connection to migrate to') ->setHelp(<<seed:run command runs all available or individual seeders @@ -45,7 +46,10 @@ protected function configure() */ protected function execute(Input $input, Output $output) { - $seed = $input->getOption('seed'); + $seed = $input->getOption('seed'); + $connection = $input->getOption('connection'); + + $this->setConnection($connection); // run the seed(ers) $start = microtime(true);