Skip to content

Commit

Permalink
Initial DB scripts fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sajedjalil committed Oct 2, 2024
1 parent 3cf0a02 commit 790e952
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
60 changes: 31 additions & 29 deletions db_scripts/create_db_tables.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
#!/bin/bash

# Database name
DB_NAME="patient_db"

# Create the database
psql -d postgres -c "CREATE DATABASE $DB_NAME;"

# Check if the database was created successfully
if [ $? -ne 0 ]; then
echo "Failed to create database $DB_NAME"
fi

# Create the patient table
psql -d $DB_NAME -c "
create_db() {
if psql -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
echo "Database $DB_NAME already exists."
else
psql -d postgres -c "CREATE DATABASE $DB_NAME;"
echo "Database $DB_NAME created successfully."
fi
}

create_table() {
table_name=$(echo "$1" | sed -n 's/.*CREATE TABLE IF NOT EXISTS \([^ ]*\).*/\1/p')
if psql -d $DB_NAME -c "\d $table_name" &>/dev/null; then
echo "Table $table_name already exists."
else
psql -d $DB_NAME -c "$1"
echo "Table $table_name created successfully."
fi
}

# Create database
create_db

# Create patient table
patient_table="
CREATE TABLE IF NOT EXISTS patient (
id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
Expand All @@ -26,30 +39,19 @@ CREATE TABLE IF NOT EXISTS patient (
next_appointment TIMESTAMP,
doctor_name VARCHAR(100)
);"
create_table "$patient_table"

# Check if the patient table was created successfully
if [ $? -ne 0 ]; then
echo "Failed to create table 'patient' in database $DB_NAME"
exit 1
fi

# Create the chat_history table with modified is_user field
psql -d $DB_NAME -c "
# Create chat_history table
chat_history_table="
CREATE TABLE IF NOT EXISTS chat_history (
id SERIAL PRIMARY KEY,
patient_id INTEGER NOT NULL,
patient_id INTEGER NOT NULL REFERENCES patient(id),
thread_id VARCHAR(50) NOT NULL,
is_user BOOLEAN NOT NULL,
text TEXT NOT NULL,
summary TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (patient_id) REFERENCES patient(id)
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);"
create_table "$chat_history_table"

# Check if the chat_history table was created successfully
if [ $? -ne 0 ]; then
echo "Failed to create table 'chat_history' in database $DB_NAME"
exit 1
fi

echo "Database $DB_NAME and tables 'patient' and 'chat_history' have been created successfully."
echo "Database setup completed."
11 changes: 2 additions & 9 deletions db_scripts/insert_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ INSERT INTO patient (
'2024-03-15 14:00:00',
'Dr. Jane Smith'
) RETURNING id;
-- Insert sample chat history entries
INSERT INTO chat_history (patient_id, thread_id, is_user, text, summary)
VALUES
(currval('patient_id_seq'), 'ebc92830-6110-45d8-bea9-fead9e51cd17', FALSE, 'Hello, I''ve been experiencing increased thirst lately. Is this related to my diabetes?', 'Patient reports increased thirst'),
(currval('patient_id_seq'), 'ebc92830-6110-45d8-bea9-fead9e51cd17', TRUE, 'Hi John, increased thirst can indeed be a symptom of diabetes. Let''s schedule a check-up to monitor your blood sugar levels. How does next week look for you?', 'System suggests check-up for diabetes symptoms'),
(currval('patient_id_seq'), 'nks72743-4565-87s6-idw4-tour3e23we52', FALSE, 'Next week works for me. Should I prepare anything specific for the appointment?', 'Patient agrees to check-up, asks for preparation instructions');
"

# Execute the SQL commands
Expand All @@ -49,10 +42,10 @@ if [ $? -eq 0 ]; then

# Display the inserted data
echo "Displaying inserted patient data:"
psql -d $DB_NAME -c "SELECT * FROM patient;"
psql -d $DB_NAME -c "SELECT * FROM patient;" -P pager=off

echo "Displaying inserted chat history data:"
psql -d $DB_NAME -c "SELECT * FROM chat_history;"
psql -d $DB_NAME -c "SELECT * FROM chat_history;" -P pager=off
else
echo "Failed to insert sample data."
fi

0 comments on commit 790e952

Please sign in to comment.