1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Manager for handling migrations. |
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; |
26
|
|
|
|
27
|
|
|
use wpdb; |
28
|
|
|
use PinkCrab\Table_Builder\Builder; |
29
|
|
|
use PinkCrab\DB_Migration\Database_Migration; |
30
|
|
|
use PinkCrab\DB_Migration\Log\Migration_Log_Manager; |
31
|
|
|
|
32
|
|
|
class Migration_Manager { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* All Schemas to be migrated/seeded |
36
|
|
|
* |
37
|
|
|
* @var array<Database_Migration> |
38
|
|
|
*/ |
39
|
|
|
protected $migrations = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The table builder. |
43
|
|
|
* |
44
|
|
|
* @var Builder |
45
|
|
|
*/ |
46
|
|
|
protected $builder; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Access to wpdb |
50
|
|
|
* |
51
|
|
|
* @var wpdb |
52
|
|
|
*/ |
53
|
|
|
protected $wpdb; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The log keeps for the migrations. |
57
|
|
|
* |
58
|
|
|
* @var Migration_Log_Manager |
59
|
|
|
*/ |
60
|
|
|
protected $migration_log; |
61
|
|
|
|
62
|
|
|
public function __construct( Builder $builder, wpdb $wpdb, ?string $migration_log_key = null ) { |
63
|
|
|
$this->builder = $builder; |
64
|
|
|
$this->wpdb = $wpdb; |
65
|
|
|
$this->migration_log = new Migration_Log_Manager( $migration_log_key ); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns access to the migration log. |
71
|
|
|
* |
72
|
|
|
* @return \PinkCrab\DB_Migration\Log\Migration_Log_Manager |
73
|
|
|
* @deprecated 1.0.2 Due to a typo |
74
|
|
|
*/ |
75
|
|
|
public function migation_log(): Migration_Log_Manager { |
76
|
|
|
return $this->migration_log; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns access to the migration log. |
81
|
|
|
* |
82
|
|
|
* Replaces the migration_log() method which has a typo |
83
|
|
|
* |
84
|
|
|
* @return \PinkCrab\DB_Migration\Log\Migration_Log_Manager |
85
|
|
|
* @since 1.0.2 |
86
|
|
|
*/ |
87
|
|
|
public function migration_log(): Migration_Log_Manager { |
88
|
|
|
return $this->migration_log; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Adds a migration to the collection. |
93
|
|
|
* |
94
|
|
|
* @param \PinkCrab\DB_Migration\Database_Migration $migration |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function add_migration( Database_Migration $migration ): self { |
98
|
|
|
$this->migrations[ $migration->get_table_name() ] = $migration; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns all the migrations held in collection. |
104
|
|
|
* |
105
|
|
|
* @return array<Database_Migration> |
106
|
|
|
*/ |
107
|
|
|
public function get_migrations(): array { |
108
|
|
|
return $this->migrations; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Creates all the tables in collection except those passed as excluded |
113
|
|
|
* |
114
|
|
|
* @param string ...$excluded_table Table names to exclude. |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
|
|
public function create_tables( string ...$excluded_table ): self { |
118
|
|
|
|
119
|
|
|
// Remove excluded tables. |
120
|
|
|
$to_create = array_filter( |
121
|
|
|
$this->migrations, |
122
|
|
|
function( Database_Migration $migration ) use ( $excluded_table ): bool { |
123
|
|
|
return ! in_array( $migration->get_table_name(), $excluded_table, true ) |
124
|
|
|
&& $this->migration_log->can_migrate( $migration->get_schema() ); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
// Upsert Tables. |
129
|
|
|
foreach ( $to_create as $migration ) { |
130
|
|
|
$result = $this->builder->create_table( $migration->get_schema() ); |
131
|
|
|
if ( $result === true ) { |
132
|
|
|
$this->migration_log->upsert_migration( $migration->get_schema() ); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Seeds all the tables in collection except those passed as excluded |
142
|
|
|
* |
143
|
|
|
* @param string ...$excluded_table Table names to exclude. |
144
|
|
|
* @return self |
145
|
|
|
*/ |
146
|
|
|
public function seed_tables( string ...$excluded_table ): self { |
147
|
|
|
|
148
|
|
|
// Remove excluded tables. |
149
|
|
|
$to_seed = array_filter( |
150
|
|
|
$this->migrations, |
151
|
|
|
function( Database_Migration $migration ) use ( $excluded_table ): bool { |
152
|
|
|
return ! in_array( $migration->get_table_name(), $excluded_table, true ) |
153
|
|
|
&& ! $this->migration_log->is_seeded( $migration->get_schema() ); |
154
|
|
|
} |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$seeder = new Migration_Seeder( $this->wpdb ); |
158
|
|
|
|
159
|
|
|
foreach ( $to_seed as $migration ) { |
160
|
|
|
$row = $seeder->seed( $migration->get_schema(), $migration->get_seeds() ); |
161
|
|
|
if ( count( $row ) !== 0 ) { |
162
|
|
|
$this->migration_log->mark_table_seeded( $migration->get_schema() ); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Removes all the tables in collection except those passed as excluded |
171
|
|
|
* |
172
|
|
|
* @param string ...$excluded_table Table names to exclude. |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
|
public function drop_tables( string ...$excluded_table ): self { |
176
|
|
|
// Remove excluded tables. |
177
|
|
|
$to_seed = array_filter( |
178
|
|
|
$this->migrations, |
179
|
|
|
function( Database_Migration $migration ) use ( $excluded_table ): bool { |
180
|
|
|
return ! in_array( $migration->get_table_name(), $excluded_table, true ); |
181
|
|
|
} |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
foreach ( $to_seed as $migration ) { |
185
|
|
|
|
186
|
|
|
$result = $this->builder->drop_table( $migration->get_schema() ); |
187
|
|
|
|
188
|
|
|
// Throw exception if fails. |
189
|
|
|
if ( $result === false ) { |
190
|
|
|
throw Migration_Exception::failed_to_drop_table( $migration->get_table_name() ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Remove migration from log. |
194
|
|
|
$this->migration_log->remove_migration( $migration->get_schema() ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|