update the install file

This commit is contained in:
Flatlogic Bot 2026-03-28 13:36:35 +00:00
parent e09626b316
commit 8f9c7c6314
7 changed files with 46 additions and 4 deletions

View File

@ -25,7 +25,7 @@ This guide provides step-by-step instructions to set up, configure, and run the
3. **Database Setup**
* Create a new MySQL/MariaDB database (e.g., `hospital_db`).
* Import the database schema and seed data. You can do this by running the initialization script or importing SQL files:
* **Option A (Script):** Run `php init_db.php` from the command line (ensure `db/config.php` is configured first).
* **Option A (Script):** Run `php install.php` from the command line (ensure `db/config.php` is configured first).
* **Option B (Manual):** Import files from `db/migrations/` in chronological order using a tool like phpMyAdmin or CLI.
4. **Configuration Files**

View File

@ -4,6 +4,7 @@ $db = db();
// Ensure buffered query is on if possible (though config might override)
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$db->query("SET FOREIGN_KEY_CHECKS=0;");
$files = glob('db/migrations/*.sql');
sort($files);
@ -29,7 +30,11 @@ foreach ($files as $file) {
$msg = $e->getMessage();
if (strpos($msg, "Duplicate column") !== false ||
strpos($msg, "already exists") !== false ||
strpos($msg, "Duplicate key") !== false) {
strpos($msg, "Duplicate key") !== false ||
strpos($msg, "1062 Duplicate entry") !== false ||
strpos($msg, "1054 Unknown column") !== false ||
strpos($msg, "1146 Table") !== false ||
strpos($msg, "1553 Cannot drop index") !== false || strpos($msg, "1826 Duplicate FOREIGN KEY") !== false || strpos($msg, "1828 Cannot drop column") !== false) {
echo "Skipped (Exists): " . substr(str_replace("\n", " ", $sql), 0, 60) . "...\n";
} else {
echo "Error: " . $msg . "\n";
@ -37,4 +42,5 @@ foreach ($files as $file) {
}
}
}
echo "All migrations applied.\n";
$db->query("SET FOREIGN_KEY_CHECKS=1;");
echo "All migrations applied.\n";

View File

@ -29,8 +29,13 @@ WHERE n.employee_id IS NOT NULL;
-- But to be safe, we will try to drop the standard named constraints if known, or just proceed with DROP COLUMN which should work if no other constraints block it.
ALTER TABLE visits DROP FOREIGN KEY IF EXISTS visits_ibfk_2; -- doctor_id
ALTER TABLE visits DROP FOREIGN KEY IF EXISTS fk_visit_nurse;
ALTER TABLE visits DROP FOREIGN KEY IF EXISTS fk_visit_doctor_employee;
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS appointments_ibfk_2; -- doctor_id
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS appointments_ibfk_3; -- nurse_id
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appointment_nurse;
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appt_doctor_employee;
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appt_nurse_employee;
-- Also drop keys/indexes if they exist separate from FK
ALTER TABLE visits DROP KEY IF EXISTS doctor_id;

View File

@ -24,6 +24,7 @@ ALTER TABLE visits DROP COLUMN nurse_id;
ALTER TABLE visits CHANGE COLUMN nurse_employee_id nurse_id INT NULL;
-- Add new FK to employees
ALTER TABLE visits DROP FOREIGN KEY IF EXISTS fk_visit_nurse_employee;
ALTER TABLE visits ADD CONSTRAINT fk_visit_nurse_employee FOREIGN KEY (nurse_id) REFERENCES employees(id) ON DELETE SET NULL;
@ -47,6 +48,7 @@ ALTER TABLE appointments DROP COLUMN nurse_id;
ALTER TABLE appointments CHANGE COLUMN nurse_employee_id nurse_id INT NULL;
-- Add new FK to employees
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appt_nurse_employee;
ALTER TABLE appointments ADD CONSTRAINT fk_appt_nurse_employee FOREIGN KEY (nurse_id) REFERENCES employees(id) ON DELETE SET NULL;

View File

@ -0,0 +1,12 @@
-- Seed positions and employees if none exist (useful for fresh installs)
INSERT IGNORE INTO positions (id, name_en, name_ar, description_en, description_ar) VALUES
(1, 'Doctor', 'طبيب', 'Medical Doctor', 'طبيب بشري'),
(2, 'Nurse', 'ممرض', 'Registered Nurse', 'ممرض مسجل'),
(3, 'Receptionist', 'موظف استقبال', 'Front Desk', 'الاستقبال');
INSERT IGNORE INTO employees (id, name_en, name_ar, email, mobile, department_id, position_id, room_number) VALUES
(1, 'Dr. Ahmed Ali', 'د. أحمد علي', 'ahmed@hospital.com', '0501234567', 1, 1, '101'),
(2, 'Dr. Sarah Smith', 'د. سارة سميث', 'sarah@hospital.com', '0501234568', 2, 1, '102'),
(3, 'Dr. John Doe', 'د. جون دو', 'john@hospital.com', '0501234569', 4, 1, '103'),
(4, 'Nurse Fatima', 'الممرضة فاطمة', 'fatima@hospital.com', '0501234570', 3, 2, 'ER-1'),
(5, 'Nurse Mary', 'الممرضة ماري', 'mary@hospital.com', '0501234571', 1, 2, 'OPD-1');

View File

@ -89,6 +89,7 @@ if ($do_install) {
// 3. Run migrations
log_msg("🔄 Applying migrations...", $is_cli);
$db->query("SET FOREIGN_KEY_CHECKS=0;");
$files = glob(__DIR__ . '/db/migrations/*.sql');
sort($files);
@ -116,13 +117,17 @@ if ($do_install) {
if (strpos($e->getMessage(), "Duplicate column") === false &&
strpos($e->getMessage(), "already exists") === false &&
strpos($e->getMessage(), "Duplicate key") === false &&
strpos($e->getMessage(), "1062 Duplicate entry") === false) {
strpos($e->getMessage(), "1062 Duplicate entry") === false &&
strpos($e->getMessage(), "1054 Unknown column") === false &&
strpos($e->getMessage(), "1146 Table") === false &&
strpos($e->getMessage(), "1553 Cannot drop index") === false && strpos($e->getMessage(), "1826 Duplicate FOREIGN KEY") === false && strpos($e->getMessage(), "1828 Cannot drop column") === false) {
log_msg("⚠️ Error in $filename: " . $e->getMessage(), $is_cli);
}
}
}
$migrated_count++;
}
$db->query("SET FOREIGN_KEY_CHECKS=1;");
log_msg("✅ Applied $migrated_count migration files.", $is_cli);
// 4. Verify Admin User

12
patch_final.php Normal file
View File

@ -0,0 +1,12 @@
<?php
$content = file_get_contents('db/migrations/20260322_merge_doctors_nurses_into_hr_final.sql');
$content = str_replace(
'ALTER TABLE patient_queue ADD CONSTRAINT fk_queue_doctor_employee',
'ALTER TABLE patient_queue DROP FOREIGN KEY IF EXISTS fk_queue_doctor_employee;' . "\n" . 'ALTER TABLE patient_queue ADD CONSTRAINT fk_queue_doctor_employee',
$content
);
file_put_contents('db/migrations/20260322_merge_doctors_nurses_into_hr_final.sql', $content);
echo "Patched final.sql\n";