1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Dynamicdatabaseconfig\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Ikechukwukalu\Dynamicdatabaseconfig\Trait\DatabaseConfig; |
7
|
|
|
|
8
|
|
|
class EnvDatabaseConfigMigrateCommand extends Command |
9
|
|
|
{ |
10
|
|
|
use DatabaseConfig; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'env:migrate |
18
|
|
|
{database : The type of database} |
19
|
|
|
{name : The name of the new database connection} |
20
|
|
|
{postfix : The postfix for the database configuration} |
21
|
|
|
{--P|--path= : The path where the database migration files are kept} |
22
|
|
|
{--seed : Running seeders} |
23
|
|
|
{--seeder= : Running a single seeder class} |
24
|
|
|
{--refresh : Refreshing all migration} |
25
|
|
|
{--rollback : Reverting migrations} |
26
|
|
|
{--fresh : Re-run all migrations afresh}'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Migrate databases from env'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
*/ |
38
|
|
|
public function handle(): void |
39
|
|
|
{ |
40
|
|
|
$path = config('dynamicdatabaseconfig.default_path'); |
41
|
|
|
$database = $this->argument('database'); |
42
|
|
|
$name = $this->argument('name'); |
43
|
|
|
$postFix = $this->argument('postfix'); |
44
|
|
|
|
45
|
|
|
if ($this->option('path')) { |
46
|
|
|
$path = $this->option('path'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->components->info("Running migrations for {$name} and path={$path}"); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$newConfig = $this->setNewEnvConfig($database, $postFix); |
|
|
|
|
52
|
|
|
$this->addNewConfig($database, $name, $newConfig); |
|
|
|
|
53
|
|
|
$this->createDatabase($database, $newConfig['database']); |
|
|
|
|
54
|
|
|
$this->execMigrationCommands($name, $path); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function execMigrationCommands(string $name, null|string $path = '') |
58
|
|
|
{ |
59
|
|
|
if ($this->option('rollback')) { |
60
|
|
|
$this->call('migrate:rollback', ['--database' => $name, '--path' => $path]); |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->option('refresh')) { |
65
|
|
|
$this->call('migrate:refresh', ['--database' => $name, '--path' => $path]); |
66
|
|
|
} elseif ($this->option('fresh')) { |
67
|
|
|
$this->call('migrate:fresh', ['--database' => $name, '--path' => $path]); |
68
|
|
|
} else { |
69
|
|
|
$this->call('migrate', ['--database' => $name, '--path' => $path]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->option('seed')) { |
73
|
|
|
$this->call('db:seed'); |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($seeder = $this->option('seeder')) { |
78
|
|
|
$this->call('db:seed', ['--class' => $seeder]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|