PDO::ERRMODE_EXCEPTION]); } catch (PDOException $e) { fwrite(STDERR, "Connection failed: " . $e->getMessage() . "\n"); exit(1); } $pdo->exec("CREATE DATABASE IF NOT EXISTS `" . DB_NAME . "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); $pdo->exec("USE `" . DB_NAME . "`"); $schema = file_get_contents(__DIR__ . '/schema.sql'); foreach (array_filter(array_map('trim', explode(';', $schema))) as $stmt) { $pdo->exec($stmt); } // Migration: add last_event_at if upgrading from an older schema $pdo->exec( "ALTER TABLE nodes ADD COLUMN IF NOT EXISTS last_event_at DATETIME(6) NULL COMMENT 'timestamp of the newest ingested event from this node' AFTER last_fetched_at" ); // Migration: drop uq_event unique key if upgrading from an older schema $row = $pdo->query( "SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'connections' AND INDEX_NAME = 'uq_event'" )->fetchColumn(); if ($row > 0) { $pdo->exec("ALTER TABLE connections DROP INDEX uq_event"); echo "Dropped legacy uq_event unique key.\n"; } echo "Database '" . DB_NAME . "' and tables created/migrated successfully.\n";