PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 3, ]); break; } catch (PDOException $e) { if ($attempt === $maxAttempts) { fwrite(STDERR, "Database {$db['host']}:{$db['port']} unreachable after {$maxAttempts} attempts: {$e->getMessage()}\n"); exit(1); } sleep(2); } } echo "Database connection OK.\n"; if ($pdo->query("SHOW TABLES LIKE 'spbu'")->fetch() !== false) { echo "Table 'spbu' already exists, skipping schema init.\n"; exit(0); } echo "Initializing schema from database/schema.sql...\n"; $sql = file_get_contents(__DIR__ . '/../database/schema.sql'); // Buang baris komentar penuh supaya pemecahan per `;` tetap aman. $lines = array_filter( explode("\n", $sql), fn ($line) => !str_starts_with(trim($line), '--') ); $statements = array_filter(array_map('trim', explode(';', implode("\n", $lines)))); foreach ($statements as $statement) { if (preg_match('/^(CREATE\s+DATABASE|USE)\b/i', $statement)) { continue; } $pdo->exec($statement); } echo "Schema initialized: table 'spbu' created and seeded.\n";