59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
class DatabaseInstaller {
|
|
public static function isInstalled() {
|
|
require_once __DIR__ . '/../db/config.php';
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->query("SHOW TABLES LIKE 'users'");
|
|
return $stmt->fetch() !== false;
|
|
} catch (PDOException $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function install() {
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$schemaFile = __DIR__ . '/../db/schema.sql';
|
|
$seedFile = __DIR__ . '/../db/seed.sql';
|
|
|
|
if (!file_exists($schemaFile)) {
|
|
throw new Exception("Schema file not found at $schemaFile");
|
|
}
|
|
|
|
self::executeSqlFile($schemaFile);
|
|
|
|
if (file_exists($seedFile)) {
|
|
self::executeSqlFile($seedFile);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static function executeSqlFile($filePath) {
|
|
// Use constants from db/config.php
|
|
$host = DB_HOST;
|
|
$name = DB_NAME;
|
|
$user = DB_USER;
|
|
$pass = DB_PASS;
|
|
|
|
$command = sprintf(
|
|
'mysql -h %s -u %s -p%s %s < %s',
|
|
escapeshellarg($host),
|
|
escapeshellarg($user),
|
|
escapeshellarg($pass),
|
|
escapeshellarg($name),
|
|
escapeshellarg($filePath)
|
|
);
|
|
|
|
$output = [];
|
|
$returnVar = 0;
|
|
exec($command . ' 2>&1', $output, $returnVar);
|
|
|
|
if ($returnVar !== 0) {
|
|
throw new Exception("Failed to execute SQL file: " . implode("\n", $output));
|
|
}
|
|
}
|
|
}
|