|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Squadron\Base\Helpers\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
|
6
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class DatabaseSchema |
|
10
|
|
|
{ |
|
11
|
|
|
public static function create(string $tableName, ?\Closure $tableStructure = null, |
|
12
|
|
|
?array $references = null, bool $useTimestamps = false, bool $useSorting = false): void |
|
13
|
|
|
{ |
|
14
|
|
|
Schema::create($tableName, function (Blueprint $table) use ($tableStructure, $references, $useTimestamps, $useSorting) { |
|
15
|
|
|
$table->uuid('uuid'); |
|
16
|
|
|
$table->primary('uuid'); |
|
17
|
|
|
|
|
18
|
|
|
if ($tableStructure !== null) |
|
19
|
|
|
{ |
|
20
|
|
|
$tableStructure($table); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if ($references !== null) |
|
24
|
|
|
{ |
|
25
|
|
|
foreach ($references as $referenceKey => $reference) |
|
26
|
|
|
{ |
|
27
|
|
|
$referenceSettings = []; |
|
28
|
|
|
|
|
29
|
|
|
if (! is_numeric($referenceKey) && is_array($reference)) |
|
30
|
|
|
{ |
|
31
|
|
|
$referenceSettings = $reference; |
|
32
|
|
|
$reference = $referenceKey; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$columnName = sprintf('%sUuid', Str::camel($reference)); |
|
36
|
|
|
$onUpdate = $referenceSettings['onUpdate'] ?? 'cascade'; |
|
37
|
|
|
$onDelete = $referenceSettings['onDelete'] ?? 'cascade'; |
|
38
|
|
|
|
|
39
|
|
|
$column = $table->uuid($columnName); |
|
40
|
|
|
|
|
41
|
|
|
if ($onUpdate === 'set null' || $onDelete === 'set null') |
|
42
|
|
|
{ |
|
43
|
|
|
$column->nullable(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$table->foreign($columnName)->references('uuid')->on($reference)->onUpdate($onUpdate)->onDelete($onDelete); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($useSorting) |
|
51
|
|
|
{ |
|
52
|
|
|
$table->unsignedMediumInteger('sortOrder')->default(0); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($useTimestamps) |
|
56
|
|
|
{ |
|
57
|
|
|
$table->timestamp('createdAt')->nullable(); |
|
58
|
|
|
$table->timestamp('updatedAt')->nullable(); |
|
59
|
|
|
} |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function createPivot(string $name1, string $name2, ?string $table1 = null, ?string $table2 = null): void |
|
64
|
|
|
{ |
|
65
|
|
|
$pivotName = sprintf('%s_%s', $name1, $name2); |
|
66
|
|
|
|
|
67
|
|
|
$primaryKeys = [ |
|
68
|
|
|
sprintf('%sUuid', $name1), |
|
69
|
|
|
sprintf('%sUuid', $name2), |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
$tables = [ |
|
73
|
|
|
$table1 ?? $name1, |
|
74
|
|
|
$table2 ?? $name2, |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
Schema::create($pivotName, function (Blueprint $table) use ($primaryKeys, $tables) { |
|
78
|
|
|
$table->uuid($primaryKeys[0]); |
|
79
|
|
|
$table->uuid($primaryKeys[1]); |
|
80
|
|
|
|
|
81
|
|
|
$table->primary($primaryKeys, 'pivot_primary'); |
|
82
|
|
|
|
|
83
|
|
|
$table->foreign($primaryKeys[0])->references('uuid')->on($tables[0])->onDelete('cascade')->onUpdate('cascade'); |
|
84
|
|
|
$table->foreign($primaryKeys[1])->references('uuid')->on($tables[1])->onDelete('cascade')->onUpdate('cascade'); |
|
85
|
|
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|