|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Model for migration log. |
|
7
|
|
|
* |
|
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Glynn Quelch <[email protected]> |
|
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
22
|
|
|
* @package PinkCrab\DB_Migration |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace PinkCrab\DB_Migration\Log; |
|
26
|
|
|
|
|
27
|
|
|
use DateTimeImmutable; |
|
28
|
|
|
use PinkCrab\Table_Builder\Schema; |
|
29
|
|
|
|
|
30
|
|
|
class Migration_Log { |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The table name |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $table_name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Has of the table columns |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $schema_hash; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Denotes if the table has been seeded with data. |
|
49
|
|
|
* |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $seeded = false; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Date the migration was created on |
|
56
|
|
|
* |
|
57
|
|
|
* @var DateTimeImmutable |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $created_on; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Date the migration was last updated |
|
63
|
|
|
* |
|
64
|
|
|
* @var DateTimeImmutable |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $updated_on; |
|
67
|
|
|
|
|
68
|
|
|
public function __construct( |
|
69
|
|
|
string $table_name, |
|
70
|
|
|
string $schema_hash, |
|
71
|
|
|
bool $seeded, |
|
72
|
|
|
DateTimeImmutable $created_on, |
|
73
|
|
|
?DateTimeImmutable $updated_on = null |
|
74
|
|
|
) { |
|
75
|
|
|
$this->table_name = $table_name; |
|
76
|
|
|
$this->schema_hash = $schema_hash; |
|
77
|
|
|
$this->seeded = $seeded; |
|
78
|
|
|
$this->created_on = $created_on; |
|
79
|
|
|
$this->updated_on = $updated_on ?? $created_on; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** NAMED CONSTRUCTORS */ |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Creates a new Migration record. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
|
88
|
|
|
* @return self |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function new_from_schema( Schema $schema ): self { |
|
91
|
|
|
return new self( |
|
92
|
|
|
$schema->get_table_name(), |
|
93
|
|
|
self::compose_column_hash( $schema ), |
|
94
|
|
|
false, |
|
95
|
|
|
new DateTimeImmutable(), |
|
96
|
|
|
new DateTimeImmutable() |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns a new instance with the defined updated schema and updated data |
|
102
|
|
|
* |
|
103
|
|
|
* @param \DateTimeImmutable|null $updated_on |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
|
|
public function as_updated( Schema $schema, ?DateTimeImmutable $updated_on = null ): self { |
|
107
|
|
|
return new self( |
|
108
|
|
|
$this->table_name(), |
|
109
|
|
|
self::compose_column_hash( $schema ), |
|
110
|
|
|
$this->is_seeded(), |
|
111
|
|
|
$this->created_on(), |
|
112
|
|
|
$updated_on ?? new DateTimeImmutable() |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns a new instace of itself, marked as seeded. |
|
118
|
|
|
* |
|
119
|
|
|
* @param \DateTimeImmutable|null $updated_on |
|
120
|
|
|
* @return self |
|
121
|
|
|
*/ |
|
122
|
|
|
public function as_seeded( ?DateTimeImmutable $updated_on = null ): self { |
|
123
|
|
|
return new self( |
|
124
|
|
|
$this->table_name(), |
|
125
|
|
|
$this->schema_hash(), |
|
126
|
|
|
true, |
|
127
|
|
|
$this->created_on(), |
|
128
|
|
|
$updated_on ?? new DateTimeImmutable() |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Generates the column hash from the tables schema. |
|
134
|
|
|
* |
|
135
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function compose_column_hash( Schema $schema ): string { |
|
139
|
|
|
$export = array( |
|
140
|
|
|
'name' => $schema->get_table_name(), |
|
141
|
|
|
'columns' => $schema->get_columns(), |
|
142
|
|
|
'indexes' => $schema->get_indexes(), |
|
143
|
|
|
'foreign_keys' => $schema->get_foreign_keys(), |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
return md5( \serialize( $export ) ?: $schema->get_table_name() ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize, Serialised to preserve types |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** GETTERS */ |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the table name |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function table_name(): string { |
|
157
|
|
|
return $this->table_name; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get has of the table columns |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function schema_hash(): string { |
|
166
|
|
|
return $this->schema_hash; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get date the migration was last updated |
|
171
|
|
|
* |
|
172
|
|
|
* @return DateTimeImmutable |
|
173
|
|
|
*/ |
|
174
|
|
|
public function updated_on(): DateTimeImmutable { |
|
175
|
|
|
return $this->updated_on; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get date the migration was created on |
|
180
|
|
|
* |
|
181
|
|
|
* @return DateTimeImmutable |
|
182
|
|
|
*/ |
|
183
|
|
|
public function created_on(): DateTimeImmutable { |
|
184
|
|
|
return $this->created_on; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get denotes if the table has been seeded with data. |
|
189
|
|
|
* |
|
190
|
|
|
* @return bool |
|
191
|
|
|
*/ |
|
192
|
|
|
public function is_seeded(): bool { |
|
193
|
|
|
return $this->seeded; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|