debug3
This commit is contained in:
parent
70cf52dd48
commit
f10cdedce9
28
db/migrations/20260322_fix_appointments_start_time.sql
Normal file
28
db/migrations/20260322_fix_appointments_start_time.sql
Normal file
@ -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;
|
||||||
77
fix_db.php
Normal file
77
fix_db.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
$db = db();
|
||||||
|
|
||||||
|
echo "<h1>Applying Database Fixes...</h1>";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. Add columns if missing
|
||||||
|
try {
|
||||||
|
$db->exec("ALTER TABLE appointments ADD COLUMN start_time DATETIME NULL");
|
||||||
|
echo "Added start_time column.<br>";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "start_time check: " . $e->getMessage() . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db->exec("ALTER TABLE appointments ADD COLUMN end_time DATETIME NULL");
|
||||||
|
echo "Added end_time column.<br>";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "end_time check: " . $e->getMessage() . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.<br>";
|
||||||
|
} else {
|
||||||
|
echo "Column 'appointment_date' does not exist, skipping data migration.<br>";
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Data migration check failed: " . $e->getMessage() . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.<br>";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Could not populate end_time: " . $e->getMessage() . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.<br>";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Could not set start_time to NOT NULL (maybe some rows are NULL?): " . $e->getMessage() . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db->exec("ALTER TABLE appointments MODIFY COLUMN end_time DATETIME NOT NULL");
|
||||||
|
echo "Set end_time to NOT NULL.<br>";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Could not set end_time to NOT NULL: " . $e->getMessage() . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 "<h2>Fix completed successfully.</h2>";
|
||||||
|
echo "<p><a href='dashboard.php' class='btn btn-primary'>Go to Dashboard</a></p>";
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "<h2>Fatal Error during fix:</h2> " . $e->getMessage();
|
||||||
|
}
|
||||||
@ -23,6 +23,8 @@ $scheduled_appointments = $db->query("
|
|||||||
FROM appointments a
|
FROM appointments a
|
||||||
JOIN patients p ON a.patient_id = p.id
|
JOIN patients p ON a.patient_id = p.id
|
||||||
WHERE a.status = 'Scheduled'
|
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();
|
$all_cities = $db->query("SELECT id, name_$lang as name FROM cities")->fetchAll();
|
||||||
|
|||||||
@ -63,12 +63,17 @@ try {
|
|||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
patient_id INT,
|
patient_id INT,
|
||||||
doctor_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',
|
status ENUM('Scheduled', 'Completed', 'Cancelled') DEFAULT 'Scheduled',
|
||||||
reason TEXT,
|
reason TEXT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE,
|
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;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS visits (
|
CREATE TABLE IF NOT EXISTS visits (
|
||||||
|
|||||||
15
test_query.php
Normal file
15
test_query.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
$db = db();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$sql = "SELECT a.id, p.name as patient_name, a.start_time, a.patient_id, a.doctor_id
|
||||||
|
FROM appointments a
|
||||||
|
JOIN patients p ON a.patient_id = p.id
|
||||||
|
WHERE a.status = 'Scheduled'
|
||||||
|
ORDER BY a.start_time ASC";
|
||||||
|
$scheduled_appointments = $db->query($sql)->fetchAll();
|
||||||
|
print_r($scheduled_appointments);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Error: " . $e->getMessage();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user