Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增db connection 可选 #22

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 = [
Expand Down
8 changes: 6 additions & 2 deletions src/command/migrate/Breakpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>breakpoint</info> 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.
Expand All @@ -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.');
Expand Down
10 changes: 7 additions & 3 deletions src/command/migrate/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>migrate:rollback</info> command reverts the last migration, or optionally up to a specific version

Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/command/migrate/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>migrate:run</info> command runs all available migrations, optionally up to a specific version

Expand All @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/command/migrate/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>migrate:status</info> command prints a list of all migrations, along with their current status

Expand All @@ -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('<info>using format</info> ' . $format);
Expand Down
6 changes: 5 additions & 1 deletion src/command/seed/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>seed:run</info> command runs all available or individual seeders

Expand All @@ -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);
Expand Down