M2 Umsetzung
This commit is contained in:
+71
-5
@@ -31,12 +31,78 @@ function dev_pdo(): PDO
|
||||
|
||||
function dev_apply_schema(PDO $pdo): void
|
||||
{
|
||||
$schema = file_get_contents(__DIR__ . '/../database/mysql-dev-schema.sql');
|
||||
if ($schema === false) {
|
||||
fwrite(STDERR, "Could not read schema file.\n");
|
||||
dev_apply_migrations($pdo);
|
||||
}
|
||||
|
||||
function dev_migrations_dir(): string
|
||||
{
|
||||
return __DIR__ . '/../database/migrations';
|
||||
}
|
||||
|
||||
function dev_ensure_migration_table(PDO $pdo): void
|
||||
{
|
||||
$pdo->exec(
|
||||
'CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version VARCHAR(255) PRIMARY KEY,
|
||||
applied_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, true>
|
||||
*/
|
||||
function dev_applied_migrations(PDO $pdo): array
|
||||
{
|
||||
dev_ensure_migration_table($pdo);
|
||||
|
||||
$applied = [];
|
||||
$stmt = $pdo->query('SELECT version FROM schema_migrations ORDER BY version');
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $version) {
|
||||
$applied[(string)$version] = true;
|
||||
}
|
||||
|
||||
return $applied;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string> Applied migration filenames.
|
||||
*/
|
||||
function dev_apply_migrations(PDO $pdo): array
|
||||
{
|
||||
$dir = dev_migrations_dir();
|
||||
$files = glob($dir . '/*.sql');
|
||||
if ($files === false || $files === []) {
|
||||
fwrite(STDERR, "No migration files found in {$dir}.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$pdo->exec($schema);
|
||||
}
|
||||
sort($files, SORT_STRING);
|
||||
$applied = dev_applied_migrations($pdo);
|
||||
$newlyApplied = [];
|
||||
$insert = $pdo->prepare('INSERT INTO schema_migrations (version) VALUES (?)');
|
||||
|
||||
foreach ($files as $file) {
|
||||
$version = basename($file);
|
||||
if (isset($applied[$version])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sql = file_get_contents($file);
|
||||
if ($sql === false) {
|
||||
fwrite(STDERR, "Could not read migration file {$version}.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo->exec($sql);
|
||||
$insert->execute([$version]);
|
||||
$newlyApplied[] = $version;
|
||||
} catch (Throwable $e) {
|
||||
fwrite(STDERR, "Migration {$version} failed: {$e->getMessage()}\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
return $newlyApplied;
|
||||
}
|
||||
|
||||
@@ -2,35 +2,18 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
$required = ['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS', 'DEV_AUTH_EMAIL'];
|
||||
foreach ($required as $name) {
|
||||
if ((getenv($name) ?: '') === '') {
|
||||
fwrite(STDERR, "Missing environment variable: {$name}\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
require __DIR__ . '/dev-db.php';
|
||||
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;port=%s;dbname=%s;charset=utf8mb4',
|
||||
getenv('DB_HOST'),
|
||||
getenv('DB_PORT') ?: '3306',
|
||||
getenv('DB_NAME')
|
||||
);
|
||||
|
||||
$pdo = new PDO($dsn, getenv('DB_USER'), getenv('DB_PASS'), [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
|
||||
$schema = file_get_contents(__DIR__ . '/../database/mysql-dev-schema.sql');
|
||||
if ($schema === false) {
|
||||
fwrite(STDERR, "Could not read schema file.\n");
|
||||
$email = getenv('DEV_AUTH_EMAIL');
|
||||
if ($email === false || trim($email) === '') {
|
||||
fwrite(STDERR, "Missing environment variable: DEV_AUTH_EMAIL\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$pdo->exec($schema);
|
||||
$pdo = dev_pdo();
|
||||
dev_apply_schema($pdo);
|
||||
|
||||
$email = strtolower(trim((string)getenv('DEV_AUTH_EMAIL')));
|
||||
$email = strtolower(trim((string)$email));
|
||||
$name = trim((string)(getenv('DEV_AUTH_NAME') ?: 'Test Admin'));
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
@@ -64,4 +47,3 @@ if ($noticeCount === 0) {
|
||||
}
|
||||
|
||||
echo "MySQL dev schema is ready for {$email}.\n";
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/dev-db.php';
|
||||
|
||||
$pdo = dev_pdo();
|
||||
$applied = dev_apply_migrations($pdo);
|
||||
|
||||
if ($applied === []) {
|
||||
echo "No new migrations. Database is up to date.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
foreach ($applied as $version) {
|
||||
echo "Applied migration: {$version}\n";
|
||||
}
|
||||
|
||||
echo "Applied " . count($applied) . " migration(s).\n";
|
||||
|
||||
Reference in New Issue
Block a user