|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* An abstract class for defninig migrations |
|
6
|
|
|
* |
|
7
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
8
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
10
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
11
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
12
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
13
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
14
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
15
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
16
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
17
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Glynn Quelch <[email protected]> |
|
20
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
21
|
|
|
* @package PinkCrab\DB_Migration |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace PinkCrab\DB_Migration; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use PinkCrab\Table_Builder\Schema; |
|
28
|
|
|
|
|
29
|
|
|
abstract class Database_Migration { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The tables schema. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Schema |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $schema; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The data to be seeded |
|
40
|
|
|
* |
|
41
|
|
|
* @var array<array<string, mixed>> |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $seed_data; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The tables name |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $table_name = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws Exception If table name not defiend. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct() { |
|
56
|
|
|
$this->schema = new Schema( $this->table_name, array( $this, 'schema' ) ); |
|
57
|
|
|
$this->seed_data = $this->seed( array() ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Defines the schema for the migration. |
|
62
|
|
|
* |
|
63
|
|
|
* @param Schema $schema_config |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
abstract public function schema( Schema $schema_config ): void; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Defines the data to be seeded. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array<string, mixed> $seeds |
|
72
|
|
|
* @return array<string, mixed> |
|
73
|
|
|
*/ |
|
74
|
|
|
public function seed( array $seeds ): array { |
|
75
|
|
|
return $seeds; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the internal schema. |
|
80
|
|
|
* |
|
81
|
|
|
* @return Schema |
|
82
|
|
|
*/ |
|
83
|
|
|
public function get_schema(): Schema { |
|
84
|
|
|
return $this->schema; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the current seed data. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array<string, mixed> |
|
91
|
|
|
*/ |
|
92
|
|
|
public function get_seeds(): array { |
|
93
|
|
|
return $this->seed_data; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the definied table name. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function get_table_name(): string { |
|
102
|
|
|
return $this->table_name; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|