| @@ 14-81 (lines=68) @@ | ||
| 11 | }; |
|
| 12 | use Illuminate\Console\Command; |
|
| 13 | ||
| 14 | class DbCreateCommand extends Command |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * The name and signature of the console command. |
|
| 18 | * |
|
| 19 | * @var string |
|
| 20 | */ |
|
| 21 | protected $signature = 'db:create |
|
| 22 | {--dry-run} |
|
| 23 | {--database= : The database name to test} |
|
| 24 | '; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * The console command description. |
|
| 28 | * |
|
| 29 | * @var string |
|
| 30 | */ |
|
| 31 | protected $description = 'Creates the database.'; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * Create a new command instance. |
|
| 35 | * |
|
| 36 | * @return void |
|
| 37 | */ |
|
| 38 | public function __construct() |
|
| 39 | { |
|
| 40 | parent::__construct(); |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Execute the console command. |
|
| 45 | * |
|
| 46 | * @return mixed |
|
| 47 | */ |
|
| 48 | public function handle(GrammarFactory $grammars) |
|
| 49 | { |
|
| 50 | $dryRun = (bool) $this->option('dry-run'); |
|
| 51 | ||
| 52 | if ($dryRun) { |
|
| 53 | $this->info('[DRY] Running in dry-run.'); |
|
| 54 | } |
|
| 55 | ||
| 56 | $connection = config('database.default'); |
|
| 57 | $configs = config(sprintf('database.connections.%s', $connection)); |
|
| 58 | $configs['database'] = $this->option('database') ?: $configs['database']; |
|
| 59 | ||
| 60 | $builder = new Builder( |
|
| 61 | $this->makeConnector($configs, $dryRun), |
|
| 62 | $grammars |
|
| 63 | ); |
|
| 64 | ||
| 65 | if ($builder->createDatabase($configs) === false) { |
|
| 66 | $this->error('Could not create the database.'); |
|
| 67 | return 1; |
|
| 68 | } |
|
| 69 | ||
| 70 | $this->info(sprintf('Database "%s" created successfully.', $configs['database'])); |
|
| 71 | } |
|
| 72 | ||
| 73 | private function makeConnector(array $configs, bool $dryRun = false): Connector |
|
| 74 | { |
|
| 75 | if ($dryRun === true) { |
|
| 76 | return new DryRunConnector($this->output); |
|
| 77 | } |
|
| 78 | ||
| 79 | return PDOConnector::make($configs); |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||
| @@ 14-81 (lines=68) @@ | ||
| 11 | }; |
|
| 12 | use Illuminate\Console\Command; |
|
| 13 | ||
| 14 | class DbDropCommand extends Command |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * The name and signature of the console command. |
|
| 18 | * |
|
| 19 | * @var string |
|
| 20 | */ |
|
| 21 | protected $signature = 'db:drop |
|
| 22 | {--dry-run} |
|
| 23 | {--database= : The database name} |
|
| 24 | '; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * The console command description. |
|
| 28 | * |
|
| 29 | * @var string |
|
| 30 | */ |
|
| 31 | protected $description = 'Drops the database.'; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * Create a new command instance. |
|
| 35 | * |
|
| 36 | * @return void |
|
| 37 | */ |
|
| 38 | public function __construct() |
|
| 39 | { |
|
| 40 | parent::__construct(); |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Execute the console command. |
|
| 45 | * |
|
| 46 | * @return mixed |
|
| 47 | */ |
|
| 48 | public function handle(GrammarFactory $grammars) |
|
| 49 | { |
|
| 50 | $dryRun = (bool) $this->option('dry-run'); |
|
| 51 | ||
| 52 | if ($dryRun) { |
|
| 53 | $this->info('[DRY] Running in dry-run.'); |
|
| 54 | } |
|
| 55 | ||
| 56 | $connection = config('database.default'); |
|
| 57 | $configs = config(sprintf('database.connections.%s', $connection)); |
|
| 58 | $configs['database'] = $this->option('database') ?: $configs['database']; |
|
| 59 | ||
| 60 | $builder = new Builder( |
|
| 61 | $this->makeConnector($configs, $dryRun), |
|
| 62 | $grammars |
|
| 63 | ); |
|
| 64 | ||
| 65 | if ($builder->dropDatabase($configs) === false) { |
|
| 66 | $this->error('Could not drop the database.'); |
|
| 67 | return 1; |
|
| 68 | } |
|
| 69 | ||
| 70 | $this->info(sprintf('Database "%s" drop successfully.', $configs['database'])); |
|
| 71 | } |
|
| 72 | ||
| 73 | private function makeConnector(array $configs, bool $dryRun = false): Connector |
|
| 74 | { |
|
| 75 | if ($dryRun === true) { |
|
| 76 | return new DryRunConnector($this->output); |
|
| 77 | } |
|
| 78 | ||
| 79 | return PDOConnector::make($configs); |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||