1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awssat\SyncMigration; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Spatie\Regex\Regex; |
7
|
|
|
use Illuminate\Database\Console\Migrations\BaseCommand; |
|
|
|
|
8
|
|
|
use Illuminate\Database\Migrations\Migrator; |
|
|
|
|
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Illuminate\Support\Facades\Schema as LaravelSchema; |
11
|
|
|
|
12
|
|
|
class SyncMigrateCommand extends BaseCommand |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The migrator instance. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Database\Migrations\Migrator |
18
|
|
|
*/ |
19
|
|
|
protected $migrator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The name and signature of the console command. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $signature = 'migrate:sync'; |
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Developer migrations syncing tool'; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
protected $schemas; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new migration command instance. |
39
|
|
|
* |
40
|
|
|
* @param \Illuminate\Database\Migrations\Migrator $migrator |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Migrator $migrator) |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
$this->schemas = Collection::make(); |
48
|
|
|
$this->migrator = $migrator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public function handle() |
55
|
|
|
{ |
56
|
|
|
$files = $this->migrator->getMigrationFiles($this->getMigrationPaths()); |
57
|
|
|
|
58
|
|
|
foreach ($files as $file) { |
59
|
|
|
$this->processMigration(file_get_contents($file)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->abondedTables()->each(function ($table) { |
63
|
|
|
$this->error("Schemas doesn't have table <fg=black;bg=white> {$table} </> seems you have delete it"); |
64
|
|
|
|
65
|
|
|
$this->confirm("Do you want to drop <fg=black;bg=white> {$table} </> ?", |
66
|
|
|
true) && LaravelSchema::dropIfExists($table); |
67
|
|
|
})->isEmpty() && $this->syncedOrNot(); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function syncedOrNot() |
72
|
|
|
{ |
73
|
|
|
return !$this->schemas->pluck('synced')->contains(true) && $this->info('Nothing to sync.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function tables() |
77
|
|
|
{ |
78
|
|
|
return Collection::make(DB::select('SHOW TABLES'))->map(function ($table) { |
79
|
|
|
return array_values((array)$table); |
80
|
|
|
})->flatten(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function processMigration($content) |
84
|
|
|
{ |
85
|
|
|
$schemas = $this->getAllSchemas($content); |
86
|
|
|
|
87
|
|
|
foreach ($schemas as $schema) { |
88
|
|
|
$schema = new Schema($schema, $this); |
89
|
|
|
$schema->process(); |
90
|
|
|
$this->schemas->push($schema); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getAllSchemas($content) |
95
|
|
|
{ |
96
|
|
|
return Regex::matchAll('/Schema::create\s*\((.*)\,.*{(.*)}\);/sU', $content)->results(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
protected function abondedTables() |
103
|
|
|
{ |
104
|
|
|
return $this->tables()->diff($this->schemasTables()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function schemasTables() |
108
|
|
|
{ |
109
|
|
|
return $this->schemas->pluck('name')->push('migrations')->map(function ($name) { |
110
|
|
|
return DB::getTablePrefix() . $name; |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths