41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
/**
|
|
* Database Setup & Migration Script
|
|
*
|
|
* Run this file once to set up the database structure and insert default values.
|
|
* Access via browser: http://localhost/db/setup.php
|
|
* Or via CLI: php db/setup.php
|
|
*/
|
|
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
$pdo = db();
|
|
|
|
$sql_file = __DIR__ . '/schema.sql';
|
|
|
|
if (!file_exists($sql_file)) {
|
|
die("Error: schema.sql not found in db directory.");
|
|
}
|
|
|
|
$sql = file_get_contents($sql_file);
|
|
|
|
try {
|
|
// Execute the full SQL file
|
|
$pdo->exec($sql);
|
|
$message = "Database schema and default data successfully migrated!";
|
|
if (php_sapi_name() === 'cli') {
|
|
echo $message . "\n";
|
|
} else {
|
|
echo "<h2 style='color:green; font-family:sans-serif;'>$message</h2>";
|
|
echo "<p style='font-family:sans-serif;'><a href='../index.php'>Go to App</a></p>";
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "Migration Failed: " . $e->getMessage();
|
|
if (php_sapi_name() === 'cli') {
|
|
echo $error . "\n";
|
|
} else {
|
|
echo "<h2 style='color:red; font-family:sans-serif;'>$error</h2>";
|
|
}
|
|
}
|
|
|