1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Database; |
10
|
|
|
|
11
|
|
|
use Joomla\Database\DatabaseDriver; |
12
|
|
|
use Joomla\Database\Exception\ExecutionFailureException; |
13
|
|
|
use Joomla\Database\Exception\PrepareStatementFailureException; |
14
|
|
|
use Joomla\StatsServer\Database\Exception\CannotInitializeMigrationsException; |
15
|
|
|
use Joomla\StatsServer\Database\Exception\UnknownMigrationException; |
16
|
|
|
use League\Flysystem\Filesystem; |
17
|
|
|
use League\Flysystem\UnreadableFileException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class for managing database migrations |
21
|
|
|
*/ |
22
|
|
|
class Migrations |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Database connector |
26
|
|
|
* |
27
|
|
|
* @var DatabaseDriver |
28
|
|
|
*/ |
29
|
|
|
private $database; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Filesystem adapter |
33
|
|
|
* |
34
|
|
|
* @var Filesystem |
35
|
|
|
*/ |
36
|
|
|
private $filesystem; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param DatabaseDriver $database Database connector |
42
|
|
|
* @param Filesystem $filesystem Filesystem adapter |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct(DatabaseDriver $database, Filesystem $filesystem) |
45
|
|
|
{ |
46
|
4 |
|
$this->database = $database; |
47
|
4 |
|
$this->filesystem = $filesystem; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks the migration status of the current installation |
52
|
|
|
* |
53
|
|
|
* @return MigrationsStatus |
54
|
|
|
*/ |
55
|
3 |
|
public function checkStatus(): MigrationsStatus |
56
|
|
|
{ |
57
|
3 |
|
$response = new MigrationsStatus; |
58
|
|
|
|
59
|
|
|
try |
60
|
|
|
{ |
61
|
|
|
// First get the list of applied migrations |
62
|
3 |
|
$appliedMigrations = $this->database->setQuery( |
63
|
3 |
|
$this->database->getQuery(true) |
64
|
3 |
|
->select('version') |
65
|
3 |
|
->from('#__migrations') |
66
|
3 |
|
)->loadColumn(); |
67
|
|
|
} |
68
|
1 |
|
catch (ExecutionFailureException | PrepareStatementFailureException $exception) |
69
|
|
|
{ |
70
|
|
|
// On PDO we're checking "42S02, 1146, Table 'XXX.#__migrations' doesn't exist" |
71
|
1 |
|
if (strpos($exception->getMessage(), "migrations' doesn't exist") === false) |
72
|
|
|
{ |
73
|
|
|
throw $exception; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$response->tableExists = false; |
77
|
|
|
|
78
|
1 |
|
return $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Now get the list of all known migrations |
82
|
2 |
|
$knownMigrations = []; |
83
|
|
|
|
84
|
2 |
|
foreach ($this->filesystem->listContents() as $migrationFiles) |
85
|
|
|
{ |
86
|
2 |
|
$knownMigrations[] = $migrationFiles['filename']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Don't rely on file system ordering. |
90
|
2 |
|
sort($knownMigrations); |
91
|
|
|
|
92
|
|
|
// Validate all migrations are applied; the count and latest versions should match |
93
|
2 |
|
if (\count($appliedMigrations) === \count($knownMigrations)) |
94
|
|
|
{ |
95
|
1 |
|
$appliedValues = array_values($appliedMigrations); |
96
|
1 |
|
$knownValues = array_values($knownMigrations); |
97
|
|
|
|
98
|
1 |
|
$latestApplied = (int) end($appliedValues); |
99
|
1 |
|
$latestKnown = (int) end($knownValues); |
100
|
|
|
|
101
|
|
|
// Versions match, good to go |
102
|
1 |
|
if ($latestApplied === $latestKnown) |
103
|
|
|
{ |
104
|
1 |
|
$response->latest = true; |
105
|
|
|
|
106
|
1 |
|
return $response; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// The system is not on the latest version, get the relevant data |
111
|
1 |
|
$response->missingMigrations = \count($knownMigrations) - \count($appliedMigrations); |
112
|
1 |
|
$response->currentVersion = array_pop($appliedMigrations); |
113
|
1 |
|
$response->latestVersion = array_pop($knownMigrations); |
114
|
|
|
|
115
|
1 |
|
return $response; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Migrate the database |
120
|
|
|
* |
121
|
|
|
* @param string|null $version Optional migration version to run |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
3 |
|
public function migrateDatabase(?string $version = null): void |
126
|
|
|
{ |
127
|
|
|
try |
128
|
|
|
{ |
129
|
|
|
// Determine the migrations to apply |
130
|
3 |
|
$appliedMigrations = $this->database->setQuery( |
131
|
3 |
|
$this->database->getQuery(true) |
132
|
3 |
|
->select('version') |
133
|
3 |
|
->from('#__migrations') |
134
|
3 |
|
)->loadColumn(); |
135
|
|
|
} |
136
|
1 |
|
catch (ExecutionFailureException | PrepareStatementFailureException $exception) |
137
|
|
|
{ |
138
|
|
|
// If the table does not exist, we can still try to run migrations |
139
|
1 |
|
if (strpos($exception->getMessage(), "migrations' doesn't exist") === false) |
140
|
|
|
{ |
141
|
|
|
throw $exception; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// If given a version, we can only execute it if it is the first migration, otherwise we've got other problems |
145
|
1 |
|
if ($version !== null && $version !== '') |
146
|
|
|
{ |
147
|
1 |
|
$firstMigration = $this->filesystem->listContents()[0]; |
148
|
|
|
|
149
|
1 |
|
if ($firstMigration['filename'] !== $version) |
150
|
|
|
{ |
151
|
|
|
throw new CannotInitializeMigrationsException( |
152
|
|
|
'The migrations have not yet been initialized and the first migration has not been given as the version to run.' |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$appliedMigrations = []; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// If a version is specified, check if that migration is already applied and if not, run that one only |
161
|
3 |
|
if ($version !== null && $version !== '') |
162
|
|
|
{ |
163
|
|
|
// If it's already applied, there's nothing to do here |
164
|
2 |
|
if (\in_array($version, $appliedMigrations)) |
165
|
|
|
{ |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
$this->doMigration($version); |
170
|
|
|
|
171
|
1 |
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// We need to check the known migrations and filter out the applied ones to know what to do |
175
|
2 |
|
$knownMigrations = []; |
176
|
|
|
|
177
|
2 |
|
foreach ($this->filesystem->listContents() as $migrationFiles) |
178
|
|
|
{ |
179
|
2 |
|
$knownMigrations[] = $migrationFiles['filename']; |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
foreach (array_diff($knownMigrations, $appliedMigrations) as $version) |
183
|
|
|
{ |
184
|
1 |
|
$this->doMigration($version); |
185
|
|
|
} |
186
|
2 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Perform the database migration for the specified version |
190
|
|
|
* |
191
|
|
|
* @param string $version Migration version to run |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
* |
195
|
|
|
* @throws UnknownMigrationException |
196
|
|
|
* @throws UnreadableFileException |
197
|
|
|
*/ |
198
|
3 |
|
private function doMigration(string $version): void |
199
|
|
|
{ |
200
|
3 |
|
$sqlFile = $version . '.sql'; |
201
|
|
|
|
202
|
3 |
|
if (!$this->filesystem->has($sqlFile)) |
203
|
|
|
{ |
204
|
1 |
|
throw new UnknownMigrationException($sqlFile); |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
$queries = $this->filesystem->read($sqlFile); |
208
|
|
|
|
209
|
2 |
|
if ($queries === false) |
210
|
|
|
{ |
211
|
|
|
throw new UnreadableFileException( |
212
|
|
|
sprintf( |
213
|
|
|
'Could not read data from the %s SQL file, please update the database manually.', |
214
|
|
|
$sqlFile |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
foreach (DatabaseDriver::splitSql($queries) as $query) |
220
|
|
|
{ |
221
|
2 |
|
$query = trim($query); |
222
|
|
|
|
223
|
2 |
|
if (!empty($query)) |
224
|
|
|
{ |
225
|
2 |
|
$this->database->setQuery($query)->execute(); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Log the migration into the database |
230
|
2 |
|
$this->database->setQuery( |
231
|
2 |
|
$this->database->getQuery(true) |
232
|
2 |
|
->insert('#__migrations') |
233
|
2 |
|
->columns('version') |
234
|
2 |
|
->values($version) |
235
|
2 |
|
)->execute(); |
236
|
2 |
|
} |
237
|
|
|
} |
238
|
|
|
|