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