68 lines
2.3 KiB
PHP
68 lines
2.3 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$message = '';
|
|
|
|
try {
|
|
// 1. Connect to MySQL server without specifying a database
|
|
$pdo_admin = new PDO('mysql:host='.DB_HOST.';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
]);
|
|
$message .= "MySQL server connection successful.<br>";
|
|
|
|
// 2. Create the database if it doesn't exist
|
|
$pdo_admin->exec("CREATE DATABASE IF NOT EXISTS `".DB_NAME."`");
|
|
$message .= "Database `".DB_NAME."` created or already exists.<br>";
|
|
|
|
// 3. Now, connect to the specific database using the existing db() function
|
|
$pdo = db();
|
|
$message .= "Database `".DB_NAME."` connection successful.<br>";
|
|
|
|
// 4. Execute 001_create_products_table.sql
|
|
$sql_create = file_get_contents('db/migrations/001_create_products_table.sql');
|
|
if ($sql_create === false) {
|
|
throw new Exception("Could not read migration file: 001_create_products_table.sql");
|
|
}
|
|
$pdo->exec($sql_create);
|
|
$message .= "`products` table created successfully.<br>";
|
|
|
|
// 5. Execute 002_seed_products.sql
|
|
$sql_seed = file_get_contents('db/migrations/002_seed_products.sql');
|
|
if ($sql_seed === false) {
|
|
throw new Exception("Could not read migration file: 002_seed_products.sql");
|
|
}
|
|
$pdo->exec($sql_seed);
|
|
$message .= "`products` table seeded with sample data.<br>";
|
|
|
|
$message .= "<br><strong>Installation complete!</strong> You can now <a href='index.php'>go to the homepage</a>.";
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
$message = "An error occurred: " . $e->getMessage();
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Installation</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<style>
|
|
body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background-color: #f8f9fa; }
|
|
.card { max-width: 600px; width: 100%; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="card">
|
|
<div class="card-header">Database Setup</div>
|
|
<div class="card-body">
|
|
<p class="card-text"><?php echo $message; ?></p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|