Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
AI Tutor
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Salma Mohammed Hamed
AI Tutor
Commits
a0cb43d6
Commit
a0cb43d6
authored
Sep 13, 2025
by
SalmaMohammedHamedMustafa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use posrgress for chat history
parent
edfaebe2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
apply_test_schema.py
self_hosted_env/voice_agent/apply_test_schema.py
+76
-0
No files found.
self_hosted_env/voice_agent/apply_test_schema.py
0 → 100644
View file @
a0cb43d6
import
psycopg2
import
os
schema_sql
=
"""
-- Create students table
CREATE TABLE IF NOT EXISTS students (
id SERIAL PRIMARY KEY,
student_id VARCHAR(50) UNIQUE NOT NULL,
grade INTEGER,
nationality VARCHAR(20) NOT NULL DEFAULT 'EGYPTIAN'
);
-- Create chat_history table
CREATE TABLE IF NOT EXISTS chat_history (
id SERIAL PRIMARY KEY,
student_id VARCHAR(50) NOT NULL,
role VARCHAR(20) NOT NULL CHECK (role IN ('user', 'assistant', 'system')),
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE CASCADE
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_chat_history_student_id ON chat_history(student_id);
CREATE INDEX IF NOT EXISTS idx_chat_history_created_at ON chat_history(created_at);
CREATE INDEX IF NOT EXISTS idx_students_nationality ON students(nationality);
-- Insert dummy data for testing
INSERT INTO students (student_id, grade, nationality) VALUES
('student_001', 3, 'EGYPTIAN'),
('student_002', 4, 'SAUDI'),
('student_003', 2, 'EGYPTIAN'),
('student_004', 5, 'SAUDI')
ON CONFLICT (student_id) DO NOTHING;
"""
drop_all_tables_sql
=
"""
DO $$
DECLARE
rec RECORD;
BEGIN
-- drop all tables in public schema
FOR rec IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
EXECUTE 'DROP TABLE IF EXISTS "' || rec.tablename || '" CASCADE';
END LOOP;
END $$;
"""
conn
=
psycopg2
.
connect
(
host
=
os
.
getenv
(
"POSTGRES_HOST"
,
"localhost"
),
port
=
os
.
getenv
(
"POSTGRES_PORT"
,
"5432"
),
user
=
os
.
getenv
(
"POSTGRES_USER"
),
password
=
os
.
getenv
(
"POSTGRES_PASSWORD"
),
dbname
=
os
.
getenv
(
"POSTGRES_DB"
)
)
conn
.
autocommit
=
True
with
conn
.
cursor
()
as
cur
:
# Drop all existing tables (uncomment if needed)
#cur.execute(drop_all_tables_sql)
cur
.
execute
(
schema_sql
)
# Verifications: Select from students and chat_history tables
print
(
"Students table rows:"
)
cur
.
execute
(
"SELECT * FROM students ORDER BY id;"
)
students
=
cur
.
fetchall
()
for
row
in
students
:
print
(
row
)
print
(
"
\n
Chat_history table rows:"
)
cur
.
execute
(
"SELECT * FROM chat_history ORDER BY id;"
)
chat_history
=
cur
.
fetchall
()
for
row
in
chat_history
:
print
(
row
)
conn
.
close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment