38701-vm/db/setup.php
Flatlogic Bot 5db61988c3 sadiq
2026-02-23 06:39:28 +00:00

39 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
function setup_database() {
$pdo = db();
$sql_file = __DIR__ . '/database.sql';
if (!file_exists($sql_file)) {
return ['success' => false, 'error' => 'Database SQL file not found.'];
}
try {
$sql = file_get_contents($sql_file);
// Split SQL by semicolon, but be careful with multi-line statements if any
// For simplicity, we can use PDO exec for the whole file if it's well-formed
// Or split and execute parts.
// Using exec on the whole block is often okay for small/medium dumps
$pdo->exec($sql);
return ['success' => true, 'message' => 'Database initialized successfully.'];
} catch (PDOException $e) {
return ['success' => false, 'error' => 'Database import failed: ' . $e->getMessage()];
}
}
// If run from CLI or specifically requested
if (php_sapi_name() === 'cli' || isset($_GET['run'])) {
$res = setup_database();
if (php_sapi_name() === 'cli') {
echo ($res['success'] ? "SUCCESS: " . $res['message'] : "ERROR: " . $res['error']) . "\n";
} else {
header('Content-Type: application/json');
echo json_encode($res);
}
}