diff --git a/db/migrations/20260322_fix_appointments_start_time.sql b/db/migrations/20260322_fix_appointments_start_time.sql
new file mode 100644
index 0000000..e164f78
--- /dev/null
+++ b/db/migrations/20260322_fix_appointments_start_time.sql
@@ -0,0 +1,28 @@
+-- Fix appointments table schema: Add start_time/end_time if missing
+-- This handles the case where init_db.php created the table with appointment_date instead of start_time
+
+-- Add start_time if it doesn't exist
+-- Note: MySQL/MariaDB < 10.2 don't support ADD COLUMN IF NOT EXISTS easily, so we use a stored procedure or just suppress errors in apply_migrations.php
+-- Assuming MariaDB 10.2+ or suppression in apply_migrations.php for "Duplicate column"
+
+ALTER TABLE appointments ADD COLUMN start_time DATETIME NULL;
+ALTER TABLE appointments ADD COLUMN end_time DATETIME NULL;
+
+-- Migrate data if appointment_date exists
+-- Check if appointment_date exists first? SQL doesn't have conditional logic outside procedures easily.
+-- We'll try to update, if column doesn't exist it will fail but that's fine if start_time is already populated.
+-- Wait, if appointment_date doesn't exist, this query will fail and might stop migration script if not handled.
+-- But apply_migrations.php continues on error? No, it catches exceptions per statement.
+
+-- We can wrap in a procedure to be safe, but apply_migrations.php splits by ';'.
+-- So let's just try the update. If appointment_date doesn't exist, it fails harmlessly.
+
+UPDATE appointments SET start_time = appointment_date WHERE start_time IS NULL;
+UPDATE appointments SET end_time = DATE_ADD(start_time, INTERVAL 30 MINUTE) WHERE end_time IS NULL AND start_time IS NOT NULL;
+
+-- Make start_time NOT NULL after populating
+ALTER TABLE appointments MODIFY COLUMN start_time DATETIME NOT NULL;
+ALTER TABLE appointments MODIFY COLUMN end_time DATETIME NOT NULL;
+
+-- Drop appointment_date if it exists (optional cleanup)
+-- ALTER TABLE appointments DROP COLUMN appointment_date;
diff --git a/fix_db.php b/fix_db.php
new file mode 100644
index 0000000..232c191
--- /dev/null
+++ b/fix_db.php
@@ -0,0 +1,77 @@
+Applying Database Fixes...";
+
+try {
+ // 1. Add columns if missing
+ try {
+ $db->exec("ALTER TABLE appointments ADD COLUMN start_time DATETIME NULL");
+ echo "Added start_time column.
";
+ } catch (PDOException $e) {
+ echo "start_time check: " . $e->getMessage() . "
";
+ }
+
+ try {
+ $db->exec("ALTER TABLE appointments ADD COLUMN end_time DATETIME NULL");
+ echo "Added end_time column.
";
+ } catch (PDOException $e) {
+ echo "end_time check: " . $e->getMessage() . "
";
+ }
+
+ // 2. Migrate data
+ // Check if appointment_date exists before trying to use it
+ try {
+ $stmt = $db->query("SHOW COLUMNS FROM appointments LIKE 'appointment_date'");
+ if ($stmt->fetch()) {
+ $db->exec("UPDATE appointments SET start_time = appointment_date WHERE start_time IS NULL");
+ echo "Migrated data from appointment_date to start_time.
";
+ } else {
+ echo "Column 'appointment_date' does not exist, skipping data migration.
";
+ }
+ } catch (PDOException $e) {
+ echo "Data migration check failed: " . $e->getMessage() . "
";
+ }
+
+ // Populate end_time based on start_time
+ try {
+ $db->exec("UPDATE appointments SET end_time = DATE_ADD(start_time, INTERVAL 30 MINUTE) WHERE end_time IS NULL AND start_time IS NOT NULL");
+ echo "Populated end_time based on start_time.
";
+ } catch (PDOException $e) {
+ echo "Could not populate end_time: " . $e->getMessage() . "
";
+ }
+
+ // 3. Set NOT NULL constraint
+ try {
+ // Only if start_time has values (or table is empty)
+ $db->exec("ALTER TABLE appointments MODIFY COLUMN start_time DATETIME NOT NULL");
+ echo "Set start_time to NOT NULL.
";
+ } catch (PDOException $e) {
+ echo "Could not set start_time to NOT NULL (maybe some rows are NULL?): " . $e->getMessage() . "
";
+ }
+
+ try {
+ $db->exec("ALTER TABLE appointments MODIFY COLUMN end_time DATETIME NOT NULL");
+ echo "Set end_time to NOT NULL.
";
+ } catch (PDOException $e) {
+ echo "Could not set end_time to NOT NULL: " . $e->getMessage() . "
";
+ }
+
+ // 4. Ensure other columns exist (like nurse_id, visit_type, etc. from previous migrations)
+ try {
+ $db->exec("ALTER TABLE appointments ADD COLUMN nurse_id INT NULL");
+ } catch (Exception $e) {}
+ try {
+ $db->exec("ALTER TABLE appointments ADD COLUMN visit_type ENUM('Clinic', 'Home') DEFAULT 'Clinic'");
+ } catch (Exception $e) {}
+ try {
+ $db->exec("ALTER TABLE appointments ADD COLUMN address TEXT NULL");
+ } catch (Exception $e) {}
+
+ echo "