- First run after refactor.

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-10-06 22:33:43 -03:00
parent 84f28bc956
commit 76dc51e980
6 changed files with 40 additions and 113 deletions

View file

@ -1,6 +0,0 @@
CREATE TABLE clicks (
campaign_id TEXT NOT NULL,
email TEXT NOT NULL,
updated_at TIMESTAMP DEFAULT NOW(),
UNIQUE(campaign_id, email)
);

View file

@ -1,11 +0,0 @@
CREATE TABLE public.system_automations (
id uuid NOT NULL,
kind int4 NULL,
target varchar(32) NULL,
schedule bpchar(12) NULL,
param varchar(32) NOT NULL,
is_active bool DEFAULT true NOT NULL,
last_triggered timestamptz NULL,
CONSTRAINT system_automations_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_active_automations ON public.system_automations USING btree (kind) WHERE is_active;

View file

@ -1,13 +0,0 @@
CREATE TABLE IF NOT EXISTS user_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id TEXT NOT NULL,
bot_id TEXT NOT NULL,
answer_mode TEXT NOT NULL DEFAULT 'direct',
context JSONB NOT NULL DEFAULT '{}',
current_tool TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(user_id, bot_id)
);
CREATE INDEX IF NOT EXISTS idx_user_sessions_user_bot ON user_sessions(user_id, bot_id);

View file

@ -1,57 +0,0 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
username VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS bots (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
llm_provider VARCHAR(100) NOT NULL,
config JSONB DEFAULT '{}',
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS user_sessions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
title VARCHAR(500) NOT NULL,
context_data JSONB DEFAULT '{}',
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE(user_id, bot_id)
);
CREATE TABLE IF NOT EXISTS message_history (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
session_id UUID NOT NULL REFERENCES user_sessions(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role VARCHAR(50) NOT NULL,
content_encrypted TEXT NOT NULL,
message_type VARCHAR(50) DEFAULT 'text',
message_index INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_user_sessions_user_id ON user_sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_user_sessions_bot_id ON user_sessions(bot_id);
CREATE INDEX IF NOT EXISTS idx_message_history_session_id ON message_history(session_id);
CREATE INDEX IF NOT EXISTS idx_message_history_user_id ON message_history(user_id);
CREATE INDEX IF NOT EXISTS idx_message_history_created_at ON message_history(created_at);
INSERT INTO bots (id, name, llm_provider)
VALUES ('00000000-0000-0000-0000-000000000000', 'Default Bot', 'mock')
ON CONFLICT (id) DO NOTHING;
INSERT INTO users (id, username, email, password_hash)
VALUES ('00000000-0000-0000-0000-000000000001', 'demo', 'demo@example.com', '$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG')
ON CONFLICT (id) DO NOTHING;

View file

@ -1,4 +1,6 @@
-- User authentication and profiles -- Optimized database schema
-- Core tables
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255) UNIQUE NOT NULL, username VARCHAR(255) UNIQUE NOT NULL,
@ -10,7 +12,6 @@ CREATE TABLE IF NOT EXISTS users (
is_active BOOLEAN DEFAULT true is_active BOOLEAN DEFAULT true
); );
-- Bot configurations
CREATE TABLE IF NOT EXISTS bots ( CREATE TABLE IF NOT EXISTS bots (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
@ -24,13 +25,12 @@ CREATE TABLE IF NOT EXISTS bots (
is_active BOOLEAN DEFAULT true is_active BOOLEAN DEFAULT true
); );
-- User sessions with optimized storage
CREATE TABLE IF NOT EXISTS user_sessions ( CREATE TABLE IF NOT EXISTS user_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE, bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
title VARCHAR(500) NOT NULL DEFAULT 'New Conversation', title VARCHAR(500) NOT NULL DEFAULT 'New Conversation',
answer_mode VARCHAR(50) NOT NULL DEFAULT 'direct', answer_mode INTEGER NOT NULL DEFAULT 0, -- 0=direct, 1=tool
context_data JSONB NOT NULL DEFAULT '{}', context_data JSONB NOT NULL DEFAULT '{}',
current_tool VARCHAR(255), current_tool VARCHAR(255),
message_count INTEGER NOT NULL DEFAULT 0, message_count INTEGER NOT NULL DEFAULT 0,
@ -41,14 +41,13 @@ CREATE TABLE IF NOT EXISTS user_sessions (
UNIQUE(user_id, bot_id, title) UNIQUE(user_id, bot_id, title)
); );
-- Encrypted message history with analytics-friendly structure
CREATE TABLE IF NOT EXISTS message_history ( CREATE TABLE IF NOT EXISTS message_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
session_id UUID NOT NULL REFERENCES user_sessions(id) ON DELETE CASCADE, session_id UUID NOT NULL REFERENCES user_sessions(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role VARCHAR(50) NOT NULL CHECK (role IN ('user', 'assistant', 'system')), role INTEGER NOT NULL, -- 0=user, 1=assistant, 2=system
content_encrypted TEXT NOT NULL, content_encrypted TEXT NOT NULL,
message_type VARCHAR(50) NOT NULL DEFAULT 'text', message_type INTEGER NOT NULL DEFAULT 0, -- 0=text, 1=image, 2=audio, 3=file
media_url TEXT, media_url TEXT,
token_count INTEGER NOT NULL DEFAULT 0, token_count INTEGER NOT NULL DEFAULT 0,
processing_time_ms INTEGER, processing_time_ms INTEGER,
@ -57,18 +56,17 @@ CREATE TABLE IF NOT EXISTS message_history (
message_index INTEGER NOT NULL message_index INTEGER NOT NULL
); );
-- Bot channel configurations -- Channel and integration tables
CREATE TABLE IF NOT EXISTS bot_channels ( CREATE TABLE IF NOT EXISTS bot_channels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE, bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
channel_type VARCHAR(50) NOT NULL CHECK (channel_type IN ('web', 'whatsapp', 'meet', 'api')), channel_type INTEGER NOT NULL, -- 0=web, 1=whatsapp, 2=voice, 3=api
config JSONB NOT NULL DEFAULT '{}', config JSONB NOT NULL DEFAULT '{}',
is_active BOOLEAN DEFAULT true, is_active BOOLEAN DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(bot_id, channel_type) UNIQUE(bot_id, channel_type)
); );
-- WhatsApp number mappings
CREATE TABLE IF NOT EXISTS whatsapp_numbers ( CREATE TABLE IF NOT EXISTS whatsapp_numbers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE, bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
@ -78,18 +76,26 @@ CREATE TABLE IF NOT EXISTS whatsapp_numbers (
UNIQUE(phone_number, bot_id) UNIQUE(phone_number, bot_id)
); );
-- User email mappings for web channel -- Automation tables
CREATE TABLE IF NOT EXISTS user_emails ( CREATE TABLE IF NOT EXISTS system_automations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, kind INTEGER NOT NULL, -- 0=scheduled, 1=table_update, 2=table_insert, 3=table_delete
email VARCHAR(255) NOT NULL, target VARCHAR(32),
is_primary BOOLEAN DEFAULT false, schedule CHAR(12),
verified BOOLEAN DEFAULT false, param VARCHAR(32) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), is_active BOOLEAN DEFAULT true NOT NULL,
UNIQUE(email) last_triggered TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );
-- Tools registry CREATE TABLE IF NOT EXISTS clicks (
campaign_id TEXT NOT NULL,
email TEXT NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(campaign_id, email)
);
-- Tools and context tables
CREATE TABLE IF NOT EXISTS tools ( CREATE TABLE IF NOT EXISTS tools (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) UNIQUE NOT NULL, name VARCHAR(255) UNIQUE NOT NULL,
@ -100,7 +106,6 @@ CREATE TABLE IF NOT EXISTS tools (
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );
-- Manual context injections
CREATE TABLE IF NOT EXISTS context_injections ( CREATE TABLE IF NOT EXISTS context_injections (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
session_id UUID NOT NULL REFERENCES user_sessions(id) ON DELETE CASCADE, session_id UUID NOT NULL REFERENCES user_sessions(id) ON DELETE CASCADE,
@ -122,8 +127,20 @@ CREATE TABLE IF NOT EXISTS usage_analytics (
total_processing_time_ms INTEGER NOT NULL DEFAULT 0 total_processing_time_ms INTEGER NOT NULL DEFAULT 0
); );
-- Indexes for performance -- Performance indexes
CREATE INDEX IF NOT EXISTS idx_user_sessions_user_bot ON user_sessions(user_id, bot_id);
CREATE INDEX IF NOT EXISTS idx_user_sessions_updated_at ON user_sessions(updated_at);
CREATE INDEX IF NOT EXISTS idx_message_history_session_id ON message_history(session_id); CREATE INDEX IF NOT EXISTS idx_message_history_session_id ON message_history(session_id);
CREATE INDEX IF NOT EXISTS idx_message_history_created_at ON message_history(created_at); CREATE INDEX IF NOT EXISTS idx_message_history_created_at ON message_history(created_at);
CREATE INDEX IF NOT EXISTS idx_user_sessions_user_bot ON user_sessions(user_id, bot_id);
CREATE INDEX IF NOT EXISTS idx_usage_analytics_date ON usage_analytics(date); CREATE INDEX IF NOT EXISTS idx_usage_analytics_date ON usage_analytics(date);
CREATE INDEX IF NOT EXISTS idx_system_automations_active ON system_automations(kind) WHERE is_active;
CREATE INDEX IF NOT EXISTS idx_bot_channels_type ON bot_channels(channel_type) WHERE is_active;
-- Default data
INSERT INTO bots (id, name, llm_provider, context_provider)
VALUES ('00000000-0000-0000-0000-000000000000', 'Default Bot', 'mock', 'mock')
ON CONFLICT (id) DO NOTHING;
INSERT INTO users (id, username, email, password_hash)
VALUES ('00000000-0000-0000-0000-000000000001', 'demo', 'demo@example.com', '$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG')
ON CONFLICT (id) DO NOTHING;

View file

@ -15,15 +15,12 @@ pub struct Organization {
pub struct Bot { pub struct Bot {
pub bot_id: Uuid, pub bot_id: Uuid,
pub name: String, pub name: String,
pub status: BotStatus, pub status: i32,
pub config: serde_json::Value, pub config: serde_json::Value,
pub created_at: DateTime<Utc>, pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>, pub updated_at: DateTime<Utc>,
} }
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::Type)]
#[serde(rename_all = "snake_case")]
#[sqlx(type_name = "bot_status", rename_all = "snake_case")]
pub enum BotStatus { pub enum BotStatus {
Active, Active,
Inactive, Inactive,