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 "

Fix completed successfully.

"; + echo "

Go to Dashboard

"; + +} catch (PDOException $e) { + echo "

Fatal Error during fix:

" . $e->getMessage(); +} diff --git a/includes/common_data.php b/includes/common_data.php index 6601b96..95f4ee7 100644 --- a/includes/common_data.php +++ b/includes/common_data.php @@ -23,6 +23,8 @@ $scheduled_appointments = $db->query(" FROM appointments a JOIN patients p ON a.patient_id = p.id WHERE a.status = 'Scheduled' - ORDER BY a.start_time ASC")->fetchAll();$all_countries = require __DIR__ . "/countries.php"; + ORDER BY a.start_time ASC")->fetchAll(); + +$all_countries = require __DIR__ . "/countries.php"; $all_cities = $db->query("SELECT id, name_$lang as name FROM cities")->fetchAll(); diff --git a/init_db.php b/init_db.php index f4e824c..cababd9 100644 --- a/init_db.php +++ b/init_db.php @@ -63,12 +63,17 @@ try { id INT AUTO_INCREMENT PRIMARY KEY, patient_id INT, doctor_id INT, - appointment_date DATETIME, + nurse_id INT, + visit_type ENUM('Clinic', 'Home') DEFAULT 'Clinic', + address TEXT, + start_time DATETIME NOT NULL, + end_time DATETIME NOT NULL, status ENUM('Scheduled', 'Completed', 'Cancelled') DEFAULT 'Scheduled', reason TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE, - FOREIGN KEY (doctor_id) REFERENCES doctors(id) ON DELETE CASCADE + FOREIGN KEY (doctor_id) REFERENCES doctors(id) ON DELETE CASCADE, + FOREIGN KEY (nurse_id) REFERENCES nurses(id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS visits ( diff --git a/test_query.php b/test_query.php new file mode 100644 index 0000000..0980ed5 --- /dev/null +++ b/test_query.php @@ -0,0 +1,15 @@ +query($sql)->fetchAll(); + print_r($scheduled_appointments); +} catch (PDOException $e) { + echo "Error: " . $e->getMessage(); +}