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