-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathPendingMigrationsMiddleware.php
More file actions
182 lines (153 loc) · 5.18 KB
/
Copy pathPendingMigrationsMiddleware.php
File metadata and controls
182 lines (153 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace Migrations\Middleware;
use Cake\Console\ConsoleIo;
use Cake\Console\TestSuite\StubConsoleInput;
use Cake\Console\TestSuite\StubConsoleOutput;
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Core\InstanceConfigTrait;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Utility\Hash;
use Migrations\Config\Config;
use Migrations\Migration\Manager;
use Migrations\Util\Util;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class PendingMigrationsMiddleware implements MiddlewareInterface
{
use InstanceConfigTrait;
protected const SKIP_QUERY_KEY = 'skip-middleware-check';
protected array $_defaultConfig = [
'paths' => [
'migrations' => ROOT . DS . 'config' . DS . 'Migrations' . DS,
],
'environment' => [
'connection' => 'default',
'migration_table' => 'phinxlog',
],
'app' => null,
'plugins' => null,
];
/**
* @param array<string, mixed> $config
*/
public function __construct(array $config = [])
{
if (!empty($config['plugins']) && $config['plugins'] === true) {
$config['plugins'] = Plugin::loaded();
}
$this->setConfig($config);
}
/**
* Process method.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @throws \Cake\Core\Exception\CakeException
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!Configure::read('debug') || $this->isSkipped($request)) {
return $handler->handle($request);
}
$pendingMigrations = $this->pendingMigrations();
if (!$pendingMigrations) {
return $handler->handle($request);
}
$message = sprintf('Pending migrations need to be run for %s:', implode(', ', array_keys($pendingMigrations))) . PHP_EOL;
$message .= '`' . implode('`,' . PHP_EOL . '`', $pendingMigrations) . '`';
throw new CakeException($message, 503);
}
/**
* @return array<string, string>
*/
protected function pendingMigrations(): array
{
$pending = [];
if (!$this->checkAppMigrations()) {
$pending['app'] = 'bin/cake migrations migrate';
}
/** @var array<string> $plugins */
$plugins = (array)$this->_config['plugins'];
foreach ($plugins as $plugin) {
if (!$this->checkPluginMigrations($plugin)) {
$pending[$plugin] = 'bin/cake migrations migrate -p ' . $plugin;
}
}
return $pending;
}
/**
* @return bool
*/
protected function checkAppMigrations(): bool
{
if ($this->_config['app'] === false) {
return true;
}
$connection = ConnectionManager::get($this->_config['environment']['connection']);
$database = $connection->config()['database'];
$this->_config['environment']['database'] = $database;
$manager = $this->getManager($this->_config);
foreach ($manager->getMigrationVersions() as $version) {
if (!$manager->isMigrated($version)) {
return false;
}
}
return true;
}
/**
* @return bool
*/
protected function checkPluginMigrations(string $plugin): bool
{
$connection = ConnectionManager::get($this->_config['environment']['connection']);
$database = $connection->config()['database'];
$this->_config['environment']['database'] = $database;
$pluginPath = Plugin::path($plugin);
if (!is_dir($pluginPath . 'config' . DS . 'Migrations' . DS)) {
return true;
}
$config = [
'paths' => [
'migrations' => $pluginPath . 'config' . DS . 'Migrations' . DS,
],
] + $this->_config;
$table = Util::tableName($plugin);
$config['environment']['migration_table'] = $table;
$manager = $this->getManager($config);
foreach ($manager->getMigrationVersions() as $version) {
if (!$manager->isMigrated($version)) {
return false;
}
}
return true;
}
/**
* @return bool
*/
protected function isSkipped(ServerRequestInterface $request): bool
{
return (bool)Hash::get($request->getQueryParams(), static::SKIP_QUERY_KEY);
}
/**
* Create a manager instance with stubbed console io
*
* @param array $config Configuration data
* @return \Migrations\Migration\Manager
*/
protected function getManager(array $config): Manager
{
$managerConfig = new Config($config);
$io = new ConsoleIo(
new StubConsoleOutput(),
new StubConsoleOutput(),
new StubConsoleInput([]),
);
return new Manager($managerConfig, $io);
}
}