1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\ContentMigrations\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Console\Migrations\MigrateCommand as Command; |
6
|
|
|
|
7
|
|
|
class MigrateCommand extends Command |
8
|
|
|
{ |
9
|
|
|
protected $signature = 'content-migrate {--database= : The database connection to use} |
10
|
|
|
{--schema-path= : The path to a schema dump file} |
11
|
|
|
{--pretend : Dump the SQL queries that would be run}'; |
12
|
|
|
|
13
|
|
|
protected $description = 'Run the content migrations'; |
14
|
|
|
|
15
|
|
|
public function handle() |
16
|
|
|
{ |
17
|
|
|
// Needed for Laravel 6 support. Laravel 7 and 8 has 'usingConnection' method on Migrator |
18
|
|
|
if (method_exists($this->migrator, 'usingConnection')) { |
19
|
|
|
$this->migrator->usingConnection($this->option('database'), function () { |
20
|
|
|
$this->prepareAndRun(); |
21
|
|
|
}); |
22
|
|
|
} else { |
23
|
|
|
// @codeCoverageIgnoreStart |
24
|
|
|
$this->prepareAndRun(); |
25
|
|
|
// @codeCoverageIgnoreEnd |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return 0; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function prepareAndRun(): void |
32
|
|
|
{ |
33
|
|
|
$this->prepareDatabase(); |
34
|
|
|
|
35
|
|
|
$this->migrator->setOutput($this->output)->run($this->getMigrationPaths()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function prepareDatabase() |
39
|
|
|
{ |
40
|
|
|
if (! $this->migrator->repositoryExists()) { |
41
|
|
|
$this->call('content-migrate:install', array_filter([ |
42
|
|
|
'--database' => $this->option('database'), |
43
|
|
|
])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (method_exists($this->migrator, 'hasRunAnyMigrations') && |
47
|
|
|
! $this->migrator->hasRunAnyMigrations() && |
48
|
|
|
! $this->option('pretend')) { |
49
|
|
|
$this->loadSchemaState(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getMigrationPath() |
54
|
|
|
{ |
55
|
|
|
return $this->laravel->databasePath() . DIRECTORY_SEPARATOR . 'content-migrations'; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|