36326-vm/db/setup_volunteers.php
2025-11-26 15:44:00 +00:00

46 lines
1.7 KiB
PHP

<?php
require_once 'config.php';
try {
$pdo = db();
// volunteers table
$sql_volunteers = "
CREATE TABLE IF NOT EXISTS volunteers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);";
$pdo->exec($sql_volunteers);
echo "Database table 'volunteers' created successfully (if it didn't exist).\n";
// volunteer_assignments table
$sql_assignments = "
CREATE TABLE IF NOT EXISTS volunteer_assignments (
id INT AUTO_INCREMENT PRIMARY KEY,
listing_id INT NOT NULL,
volunteer_id INT NOT NULL,
assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (listing_id) REFERENCES food_listings(id) ON DELETE CASCADE,
FOREIGN KEY (volunteer_id) REFERENCES volunteers(id) ON DELETE CASCADE
);";
$pdo->exec($sql_assignments);
echo "Database table 'volunteer_assignments' created successfully (if it didn't exist).\n";
// Add 'assigned' to the status enum in food_listings
// Note: This might fail if the enum value already exists, which is fine.
try {
$sql_alter_listings = "ALTER TABLE food_listings MODIFY status ENUM('available', 'claimed', 'collected', 'assigned') DEFAULT 'available'";
$pdo->exec($sql_alter_listings);
echo "Altered 'food_listings' table to add 'assigned' status.\n";
} catch (PDOException $e) {
// This will likely throw an error if you run it more than once.
echo "Could not alter 'food_listings' table (maybe 'assigned' status already exists).\n";
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}