|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Migrations\Operation\ForeignKey; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Database\ForeignKeyInterface; |
|
8
|
|
|
use Cycle\Migrations\CapsuleInterface; |
|
9
|
|
|
use Cycle\Migrations\Exception\Operation\ForeignKeyException; |
|
10
|
|
|
use Cycle\Migrations\Operation\Traits\OptionsTrait; |
|
11
|
|
|
|
|
12
|
|
|
final class Add extends ForeignKey |
|
13
|
|
|
{ |
|
14
|
|
|
use OptionsTrait; |
|
15
|
|
|
|
|
16
|
80 |
|
public function __construct( |
|
17
|
|
|
string $table, |
|
18
|
|
|
array $columns, |
|
19
|
|
|
private string $foreignTable, |
|
20
|
|
|
private array $foreignKeys, |
|
21
|
|
|
array $options |
|
22
|
|
|
) { |
|
23
|
80 |
|
$this->options = $options; |
|
24
|
80 |
|
parent::__construct($table, $columns); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
80 |
|
public function execute(CapsuleInterface $capsule): void |
|
31
|
|
|
{ |
|
32
|
80 |
|
$schema = $capsule->getSchema($this->getTable()); |
|
33
|
|
|
|
|
34
|
80 |
|
if ($schema->hasForeignKey($this->columns)) { |
|
35
|
8 |
|
throw new ForeignKeyException( |
|
36
|
8 |
|
"Unable to add foreign key '{$schema->getName()}'.({$this->columnNames()}), " |
|
37
|
|
|
. 'foreign key already exists' |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
72 |
|
$foreignSchema = $capsule->getSchema($this->foreignTable); |
|
42
|
|
|
|
|
43
|
72 |
|
if ($this->foreignTable !== $this->table && !$foreignSchema->exists()) { |
|
44
|
8 |
|
throw new ForeignKeyException( |
|
45
|
8 |
|
"Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}', " |
|
46
|
8 |
|
. "foreign table '{$this->foreignTable}' does not exists" |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
64 |
|
foreach ($this->foreignKeys as $fk) { |
|
51
|
64 |
|
if ($this->foreignTable !== $this->table && !$foreignSchema->hasColumn($fk)) { |
|
52
|
8 |
|
throw new ForeignKeyException( |
|
53
|
8 |
|
"Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}'," |
|
54
|
8 |
|
. " foreign column '{$this->foreignTable}'.'{$fk}' does not exists" |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
56 |
|
$foreignKey = $schema->foreignKey($this->columns)->references( |
|
60
|
56 |
|
$this->foreignTable, |
|
61
|
56 |
|
$this->foreignKeys |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
56 |
|
if ($this->hasOption('name')) { |
|
65
|
40 |
|
$foreignKey->setName($this->getOption('name')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/* |
|
69
|
|
|
* We are allowing both formats "NO_ACTION" and "NO ACTION". |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
56 |
|
$foreignKey->onDelete( |
|
73
|
56 |
|
str_replace( |
|
74
|
|
|
'_', |
|
75
|
|
|
' ', |
|
76
|
56 |
|
$this->getOption('delete', ForeignKeyInterface::NO_ACTION) |
|
77
|
|
|
) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
56 |
|
$foreignKey->onUpdate( |
|
81
|
56 |
|
str_replace( |
|
82
|
|
|
'_', |
|
83
|
|
|
' ', |
|
84
|
56 |
|
$this->getOption('update', ForeignKeyInterface::NO_ACTION) |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|