91 lines
3.1 KiB
PHP
91 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Schema-Migrationen anwenden.
|
||
|
|
*
|
||
|
|
* Wird von tools/migrate.php (CLI) und von der Route /migrate?token=… genutzt.
|
||
|
|
* Letztere gibt es, weil auf dem netcup-Webhosting kein SSH zur Verfügung
|
||
|
|
* steht: Nach `tools/deploy.sh --target netcup --sync` zieht
|
||
|
|
* curl -sS "https://drinkitcold.com/migrate?token=<app.cron_token>"
|
||
|
|
* die ausstehenden Migrationen nach.
|
||
|
|
*
|
||
|
|
* Migrationen sind additiv und werden über schema_migrations genau einmal
|
||
|
|
* ausgeführt; ein erneuter Aufruf ist gefahrlos.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** @return list<int> */
|
||
|
|
function migrations_applied(): array
|
||
|
|
{
|
||
|
|
return array_map('intval', db()->query('SELECT version FROM schema_migrations')->fetchAll(PDO::FETCH_COLUMN));
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @return array<string, string> Dateiname => SQL (nur ausstehende) */
|
||
|
|
function migrations_pending(): array
|
||
|
|
{
|
||
|
|
db()->exec(
|
||
|
|
'CREATE TABLE IF NOT EXISTS schema_migrations (
|
||
|
|
version INT UNSIGNED NOT NULL,
|
||
|
|
applied_at DATETIME NOT NULL,
|
||
|
|
PRIMARY KEY (version)
|
||
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
|
||
|
|
);
|
||
|
|
$applied = migrations_applied();
|
||
|
|
$files = glob(DIC_ROOT . '/sql/migrations/*.sql') ?: [];
|
||
|
|
sort($files, SORT_STRING);
|
||
|
|
|
||
|
|
$pending = [];
|
||
|
|
foreach ($files as $file) {
|
||
|
|
$name = basename($file);
|
||
|
|
if (!preg_match('/^(\d+)_/', $name, $match)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (in_array((int) $match[1], $applied, true)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$pending[$name] = (string) file_get_contents($file);
|
||
|
|
}
|
||
|
|
return $pending;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Führt alle ausstehenden Migrationen aus.
|
||
|
|
*
|
||
|
|
* @return array{applied: list<string>, skipped: list<string>}
|
||
|
|
*/
|
||
|
|
function migrations_run(): array
|
||
|
|
{
|
||
|
|
$result = ['applied' => [], 'skipped' => []];
|
||
|
|
foreach (migrations_pending() as $name => $sql) {
|
||
|
|
preg_match('/^(\d+)_/', $name, $match);
|
||
|
|
$version = (int) $match[1];
|
||
|
|
// Kommentare entfernen und an Semikola trennen (wie tools/migrate.php).
|
||
|
|
$clean = (string) preg_replace('/^\s*--.*$/m', '', $sql);
|
||
|
|
$statements = array_filter(array_map('trim', explode(';', $clean)));
|
||
|
|
if ($statements === []) {
|
||
|
|
$result['skipped'][] = $name;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
foreach ($statements as $statement) {
|
||
|
|
if ($statement === '') {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
db()->exec($statement);
|
||
|
|
} catch (PDOException $error) {
|
||
|
|
// "Gibt es schon": Bei einem frisch installierten Schema sind
|
||
|
|
// Spalten/Tabellen früherer Migrationen bereits vorhanden
|
||
|
|
// (install.php spielt schema.sql ein). Das gilt als erledigt.
|
||
|
|
if (in_array((int) ($error->errorInfo[1] ?? 0), [1050, 1060, 1061, 1091], true)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
throw new RuntimeException($name . ': ' . $error->getMessage(), 0, $error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
db()->prepare('INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (?, ?)')
|
||
|
|
->execute([$version, utc_now()]);
|
||
|
|
$result['applied'][] = $name;
|
||
|
|
}
|
||
|
|
return $result;
|
||
|
|
}
|