|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yentu; |
|
4
|
|
|
|
|
5
|
|
|
use clearice\io\Io; |
|
6
|
|
|
use yentu\factories\DatabaseManipulatorFactory; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class Migrations |
|
10
|
|
|
{ |
|
11
|
|
|
private $manipulatorFactory; |
|
12
|
|
|
private $config; |
|
13
|
|
|
private $io; |
|
14
|
|
|
|
|
15
|
42 |
|
public function __construct(Io $io, DatabaseManipulatorFactory $manipulatorFactory, array $config) |
|
16
|
|
|
{ |
|
17
|
42 |
|
$this->manipulatorFactory = $manipulatorFactory; |
|
18
|
42 |
|
$this->config = $config; |
|
19
|
42 |
|
$this->io = $io; |
|
20
|
42 |
|
} |
|
21
|
|
|
|
|
22
|
10 |
|
public function getAllPaths() |
|
23
|
|
|
{ |
|
24
|
10 |
|
return array_merge( |
|
25
|
|
|
array( |
|
26
|
|
|
array( |
|
27
|
10 |
|
'home' => $this->getPath('migrations'), |
|
28
|
10 |
|
'variables' => $this->config['variables'] |
|
29
|
|
|
) |
|
30
|
10 |
|
), $this->config['other_migrations'] |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Return an array of all migrations available. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
10 |
|
public function getMigrationFiles($path) |
|
41
|
|
|
{ |
|
42
|
10 |
|
if (!file_exists($path)) |
|
43
|
|
|
return []; |
|
44
|
10 |
|
$migrationFiles = scandir($path, 0); |
|
45
|
10 |
|
$migrations = array(); |
|
46
|
10 |
|
foreach ($migrationFiles as $migration) { |
|
47
|
10 |
|
$details = $this->getMigrationDetails($migration); |
|
48
|
10 |
|
if ($details === false) |
|
49
|
10 |
|
continue; |
|
50
|
10 |
|
$migrations[$details['timestamp']] = $details; |
|
51
|
10 |
|
unset($migrations[$details['timestamp']][0]); |
|
52
|
10 |
|
unset($migrations[$details['timestamp']][1]); |
|
53
|
10 |
|
unset($migrations[$details['timestamp']][2]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
10 |
|
return $migrations; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return the details of a migration extracted from the file name. |
|
61
|
|
|
* This method uses a regular expression to extract the timestamp and |
|
62
|
|
|
* migration name from the migration script. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $migration |
|
65
|
|
|
* @return array|bool |
|
66
|
|
|
*/ |
|
67
|
10 |
|
private function getMigrationDetails($migration) |
|
68
|
|
|
{ |
|
69
|
10 |
|
if (preg_match("/^(?<timestamp>[0-9]{14})\_(?<migration>[a-z][a-z0-9\_]*)\.php$/", $migration, $details)) { |
|
70
|
10 |
|
$details['file'] = $migration; |
|
71
|
|
|
} else { |
|
72
|
10 |
|
$details = false; |
|
73
|
|
|
} |
|
74
|
10 |
|
return $details; |
|
75
|
|
|
} |
|
76
|
|
|
/** |
|
77
|
|
|
* Returns an array of all migrations, in all configured migrations |
|
78
|
|
|
* directories. |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getAllMigrations() |
|
82
|
|
|
{ |
|
83
|
|
|
$migrations = array(); |
|
84
|
|
|
foreach ($this->getAllPaths() as $migration) { |
|
85
|
|
|
$migrations = $migrations + $this->getMigrationFiles($migration['home']); |
|
86
|
|
|
} |
|
87
|
|
|
return $migrations; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns an array of all migrations that have been run on the database. |
|
93
|
|
|
* The information returned includes the timestamp, the name of the migration |
|
94
|
|
|
* and the default schema on which it was run. |
|
95
|
|
|
* @return array |
|
96
|
|
|
* @throws exceptions\DatabaseManipulatorException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getRunMirations() |
|
99
|
|
|
{ |
|
100
|
|
|
$db = $this->manipulatorFactory->createManipulator(); |
|
101
|
|
|
$runMigrations = $db->query("SELECT DISTINCT version, migration, default_schema FROM yentu_history ORDER BY version"); |
|
102
|
|
|
$migrations = array(); |
|
103
|
|
|
foreach ($runMigrations as $migration) { |
|
104
|
|
|
$migrations[$migration['version']] = array( |
|
105
|
|
|
'timestamp' => $migration['version'], |
|
106
|
|
|
'migration' => $migration['migration'], |
|
107
|
|
|
'default_schema' => $migration['default_schema'] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $migrations; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns a path relative to the current yentu home. |
|
116
|
|
|
* @param string $path |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
42 |
|
public function getPath($path) |
|
120
|
|
|
{ |
|
121
|
42 |
|
return $this->config['home'] . DIRECTORY_SEPARATOR . $path; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Announce a migration based on the command and the arguments called for |
|
127
|
|
|
* the migration. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $command The action being performed |
|
130
|
|
|
* @param string $itemType The type of item |
|
131
|
|
|
* @param array $arguments The arguments of the |
|
132
|
|
|
*/ |
|
133
|
10 |
|
public function announce($command, $itemType, $arguments) |
|
134
|
|
|
{ |
|
135
|
10 |
|
$this->io->output( |
|
136
|
10 |
|
"\n - " . ucfirst("{$command}ing ") . |
|
137
|
10 |
|
preg_replace("/([a-z])([A-Z])/", "$1 $2", $itemType) . " " . |
|
138
|
10 |
|
$this->getMigrationEventDescription($command, Parameters::wrap($arguments)), Io::OUTPUT_LEVEL_2 |
|
139
|
|
|
); |
|
140
|
10 |
|
$this->io->output("."); |
|
141
|
10 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Convert the arguments of a migration event to a string description. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $command |
|
147
|
|
|
* @param array $arguments |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
10 |
|
private function getMigrationEventDescription($command, $arguments) |
|
151
|
|
|
{ |
|
152
|
10 |
|
$dir = ''; |
|
153
|
10 |
|
$destination = ''; |
|
154
|
10 |
|
$arguments = Parameters::wrap($arguments, ['name' => null]); |
|
155
|
|
|
|
|
156
|
10 |
|
if ($command == 'add') { |
|
157
|
10 |
|
$dir = 'to'; |
|
158
|
6 |
|
} else if ($command == 'drop') { |
|
159
|
|
|
$dir = 'from'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
10 |
|
if (isset($arguments['table']) && isset($arguments['schema'])) { |
|
163
|
|
|
$destination = "table " . |
|
164
|
10 |
|
($arguments['schema'] != '' ? "{$arguments['schema']}." : '' ) . |
|
165
|
10 |
|
"{$arguments['table']}'"; |
|
166
|
10 |
|
} elseif (isset($arguments['schema']) && !isset($arguments['table'])) { |
|
167
|
10 |
|
$destination = "schema '{$arguments['schema']}'"; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
10 |
|
if (is_string($arguments)) { |
|
171
|
1 |
|
return $arguments; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
10 |
|
if (isset($arguments['column'])) { |
|
175
|
10 |
|
$item = $arguments['column']; |
|
176
|
|
|
} else { |
|
177
|
10 |
|
$item = $arguments['name']; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
10 |
|
return "'$item' $dir $destination"; |
|
181
|
|
|
} |
|
182
|
|
|
} |