1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dami\Migration\Api; |
4
|
|
|
|
5
|
|
|
use Rentgen\Database\Constraint\PrimaryKey; |
6
|
|
|
use Rentgen\Database\Schema; |
7
|
|
|
use Rentgen\Database\Table; |
8
|
|
|
use Rentgen\Schema\Manipulation; |
9
|
|
|
use Rentgen\Schema\Info; |
10
|
|
|
|
11
|
|
|
abstract class MigrationApi |
12
|
|
|
{ |
13
|
|
|
private $actions = null; |
14
|
|
|
private $manipulation; |
15
|
|
|
private $info; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor. |
19
|
|
|
* |
20
|
|
|
* @param Manipulation $manipulation Manipulation instance. |
21
|
|
|
* @param Info $info Info instance. |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Manipulation $manipulation, Info $info) |
24
|
|
|
{ |
25
|
|
|
$this->actions = array(); |
26
|
|
|
$this->manipulation = $manipulation; |
27
|
|
|
$this->info = $info; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create new table. |
32
|
|
|
* |
33
|
|
|
* @param string $name Table name. |
34
|
|
|
* @param array $options Optional options. |
35
|
|
|
* |
36
|
|
|
* @return CreationTableApi CreationTableApi instance. |
37
|
|
|
*/ |
38
|
|
|
public function createTable($name, array $options = []) |
39
|
|
|
{ |
40
|
|
|
$schema = isset($options['schema']) ? new Schema($options['schema']) : null; |
41
|
|
|
|
42
|
|
|
$table = new TableApi($name, $schema, $this->manipulation, $this->actions); |
43
|
|
|
|
44
|
|
|
$primaryKey = isset($options['primary_key']) |
45
|
|
|
? new PrimaryKey($options['primary_key'], $table) |
46
|
|
|
: new PrimaryKey(array(), $table); |
47
|
|
|
if (isset($options['primary_key_auto_increment']) && false === $options['primary_key_auto_increment']) { |
48
|
|
|
$primaryKey->disableAutoIncrement(); |
49
|
|
|
} |
50
|
|
|
$table->addConstraint($primaryKey); |
51
|
|
|
|
52
|
|
|
if (isset($options['comment'])) { |
53
|
|
|
$table->setDescription($options['comment']); |
54
|
|
|
} |
55
|
|
|
$this->actions[] = function () use ($table) { |
56
|
|
|
return $this->manipulation->create($table); |
57
|
|
|
}; |
58
|
|
|
|
59
|
|
|
return $table; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Drop table. |
64
|
|
|
* |
65
|
|
|
* @param string $name Table name. |
66
|
|
|
* @param array $options Optional options. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function dropTable($name, array $options = []) |
71
|
|
|
{ |
72
|
|
|
$schema = isset($options['schema']) ? new Schema($options['schema']) : null; |
73
|
|
|
$table = new Table($name, $schema); |
74
|
|
|
|
75
|
|
|
$cascade = isset($options['cascade']) ? $options['cascade'] : true; |
76
|
|
|
$this->actions[] = function () use ($table, $cascade) { |
77
|
|
|
return $this->manipulation->drop($table, $cascade); |
78
|
|
|
}; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Alter table. |
83
|
|
|
* |
84
|
|
|
* @param string $name Table name. |
85
|
|
|
* @param array $options Optional options. |
86
|
|
|
* |
87
|
|
|
* @return AlterationTableApi |
88
|
|
|
*/ |
89
|
|
|
public function alterTable($name, array $options = []) |
90
|
|
|
{ |
91
|
|
|
$schema = isset($options['schema']) ? new Schema($options['schema']) : null; |
92
|
|
|
|
93
|
|
|
return new TableApi($name, $schema, $this->manipulation, $this->actions); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create new schema. |
98
|
|
|
* |
99
|
|
|
* @param string $name Schema name. |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function createSchema($name) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$schema = new Schema($name); |
106
|
|
|
$this->actions[] = function () use ($schema) { |
107
|
|
|
return $this->manipulation->create($schema); |
108
|
|
|
}; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Drop schema. |
113
|
|
|
* |
114
|
|
|
* @param string $name Schema name. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function dropSchema($name) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$schema = new Schema($name); |
121
|
|
|
$this->actions[] = function () use ($schema) { |
122
|
|
|
return $this->manipulation->drop($schema); |
123
|
|
|
}; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Execute SQL. |
128
|
|
|
* |
129
|
|
|
* @param string $sql SQL to execute. |
130
|
|
|
* |
131
|
|
|
* @return integer |
132
|
|
|
*/ |
133
|
|
|
public function execute($sql) |
134
|
|
|
{ |
135
|
|
|
return $this->manipulation->execute($sql); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get actions to execute. |
140
|
|
|
* |
141
|
|
|
* @return array Actions to execute. |
142
|
|
|
*/ |
143
|
|
|
public function getActions() |
144
|
|
|
{ |
145
|
|
|
return $this->actions; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.