data table sql files
This commit is contained in:
13
scripts/init_user.sql
Normal file
13
scripts/init_user.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
-- Create a new database
|
||||||
|
CREATE DATABASE ai_conversations;
|
||||||
|
|
||||||
|
-- Create a dedicated user (role) for your app
|
||||||
|
CREATE USER myapp_user WITH PASSWORD 'secure_password_123';
|
||||||
|
|
||||||
|
-- Grant privileges
|
||||||
|
GRANT ALL PRIVILEGES ON DATABASE ai_conversations TO myapp_user;
|
||||||
|
|
||||||
|
-- Also needed: allow user to create schemas/tables
|
||||||
|
\c ai_conversations
|
||||||
|
ALTER USER myapp_user CREATEDB; -- optional but helpful during dev
|
||||||
|
GRANT ALL ON SCHEMA public TO myapp_user;
|
||||||
20
scripts/recreate_table.sql
Normal file
20
scripts/recreate_table.sql
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
-- Drop the index first (if it exists)
|
||||||
|
DROP INDEX IF EXISTS idx_messages_conversation;
|
||||||
|
|
||||||
|
-- Drop the messages table (if it exists)
|
||||||
|
DROP TABLE IF EXISTS messages;
|
||||||
|
|
||||||
|
-- Recreate the messages table with TEXT conversation_id
|
||||||
|
-- Note: UUID extension is no longer needed since conversation_id is TEXT
|
||||||
|
CREATE TABLE messages (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
conversation_id TEXT NOT NULL,
|
||||||
|
message_type VARCHAR(10) NOT NULL CHECK (message_type IN ('human', 'ai', 'tool')),
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
sequence_number INTEGER NOT NULL CHECK (sequence_number >= 0),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Recreate the index for fast retrieval of all messages in a conversation (in order)
|
||||||
|
CREATE INDEX idx_messages_conversation ON messages (conversation_id, sequence_number);
|
||||||
|
|
||||||
12
scripts/table_schema.sql
Normal file
12
scripts/table_schema.sql
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
-- Create the messages table
|
||||||
|
CREATE TABLE IF NOT EXISTS messages (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
conversation_id TEXT NOT NULL,
|
||||||
|
message_type VARCHAR(10) NOT NULL CHECK (message_type IN ('human', 'ai', 'tool')),
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
sequence_number INTEGER NOT NULL CHECK (sequence_number >= 0),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Index for fast retrieval of all messages in a conversation (in order)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_messages_conversation ON messages (conversation_id, sequence_number);
|
||||||
Reference in New Issue
Block a user