| 1 | -- Create users table
|
|---|
| 2 | CREATE TABLE IF NOT EXISTS users (
|
|---|
| 3 | user_id SERIAL PRIMARY KEY,
|
|---|
| 4 | username VARCHAR(255) NOT NULL UNIQUE,
|
|---|
| 5 | password VARCHAR(255) NOT NULL,
|
|---|
| 6 | role VARCHAR(50) NOT NULL,
|
|---|
| 7 | first_name VARCHAR(100),
|
|---|
| 8 | last_name VARCHAR(100),
|
|---|
| 9 | patient_id BIGINT,
|
|---|
| 10 | doctor_id BIGINT,
|
|---|
| 11 | is_active BOOLEAN DEFAULT true,
|
|---|
| 12 | FOREIGN KEY (patient_id) REFERENCES patients(patient_id),
|
|---|
| 13 | FOREIGN KEY (doctor_id) REFERENCES doctors(doctor_id)
|
|---|
| 14 | );
|
|---|
| 15 |
|
|---|
| 16 | -- Create index for username lookup
|
|---|
| 17 | CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
|---|
| 18 |
|
|---|
| 19 | -- Insert admin user (password: admin123)
|
|---|
| 20 | INSERT INTO users (username, password, role, first_name, last_name, is_active)
|
|---|
| 21 | VALUES ('admin', 'admin123', 'ADMIN', 'System', 'Administrator', true)
|
|---|
| 22 | ON CONFLICT (username) DO NOTHING;
|
|---|
| 23 |
|
|---|
| 24 | -- Insert sample patient users (password: password123 for all)
|
|---|
| 25 | -- These will be linked to existing patients by EMBG
|
|---|
| 26 | INSERT INTO users (username, password, role, first_name, last_name, patient_id, is_active)
|
|---|
| 27 | SELECT p.embg, 'password123', 'PATIENT', p.first_name, p.last_name, p.patient_id, true
|
|---|
| 28 | FROM patients p
|
|---|
| 29 | WHERE NOT EXISTS (
|
|---|
| 30 | SELECT 1 FROM users u WHERE u.username = p.embg
|
|---|
| 31 | )
|
|---|
| 32 | LIMIT 39;
|
|---|
| 33 |
|
|---|
| 34 | -- Insert doctor users (password: doctor123 for all)
|
|---|
| 35 | -- These will be linked to existing doctors by email
|
|---|
| 36 | INSERT INTO users (username, password, role, first_name, last_name, doctor_id, is_active)
|
|---|
| 37 | SELECT d.email_address, 'doctor123', 'DOCTOR', d.first_name, d.last_name, d.doctor_id, true
|
|---|
| 38 | FROM doctors d
|
|---|
| 39 | WHERE NOT EXISTS (
|
|---|
| 40 | SELECT 1 FROM users u WHERE u.username = d.email_address
|
|---|
| 41 | );
|
|---|