1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Migrations\V2; |
6
|
|
|
|
7
|
|
|
use Cycle\Database\DatabaseInterface; |
8
|
|
|
use Cycle\Migrations\CapsuleInterface; |
9
|
|
|
use Cycle\Migrations\Exception\MigrationException; |
10
|
|
|
use Cycle\Migrations\MigrationInterface; |
11
|
|
|
use Cycle\Migrations\State; |
12
|
|
|
|
13
|
|
|
abstract class Migration implements MigrationInterface |
14
|
|
|
{ |
15
|
|
|
use ColumnTrait; |
16
|
|
|
|
17
|
|
|
// Target migration database |
18
|
|
|
protected const DATABASE = null; |
19
|
|
|
|
20
|
|
|
private ?State $state = null; |
21
|
|
|
private CapsuleInterface $capsule; |
22
|
|
|
|
23
|
|
|
public function getDatabase(): ?string |
24
|
|
|
{ |
25
|
|
|
return static::DATABASE; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function withCapsule(CapsuleInterface $capsule): MigrationInterface |
29
|
|
|
{ |
30
|
|
|
$migration = clone $this; |
31
|
|
|
$migration->capsule = $capsule; |
32
|
|
|
|
33
|
|
|
return $migration; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function withState(State $state): MigrationInterface |
37
|
|
|
{ |
38
|
|
|
$migration = clone $this; |
39
|
|
|
$migration->state = $state; |
40
|
|
|
|
41
|
|
|
return $migration; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getState(): State |
45
|
|
|
{ |
46
|
|
|
if (empty($this->state)) { |
47
|
|
|
throw new MigrationException('Unable to get migration state, no state are set'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->state; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function database(): DatabaseInterface |
54
|
|
|
{ |
55
|
|
|
if (empty($this->capsule)) { |
56
|
|
|
throw new MigrationException('Unable to get database, no capsule are set'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->capsule->getDatabase(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function table(string $tableName): TableBlueprint |
63
|
|
|
{ |
64
|
|
|
return new TableBlueprint($tableName, $this->capsule); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function index(array $fields, ?string $name = null): Index |
68
|
|
|
{ |
69
|
|
|
return new Index($fields, $name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function foreignKey(array $fields, string $table, array $outerKeys): ForeignKey |
73
|
|
|
{ |
74
|
|
|
return new ForeignKey($fields, $table, $outerKeys); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function enum(string $name, array $values): TypeBuilder |
78
|
|
|
{ |
79
|
|
|
return new TypeBuilder( |
80
|
|
|
$name, |
81
|
|
|
$values, |
82
|
|
|
TypeBuilder::ENUM, |
83
|
|
|
$this->database() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|