-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-setup.sql
More file actions
110 lines (98 loc) · 4.26 KB
/
database-setup.sql
File metadata and controls
110 lines (98 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- PrismForge AI Database Setup
-- Run this in your Supabase SQL editor
-- Organizations table
CREATE TABLE IF NOT EXISTS organizations (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
domain TEXT,
subscription_tier TEXT DEFAULT 'free',
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Users table
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
role TEXT DEFAULT 'viewer',
organization_id UUID REFERENCES organizations(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Chat sessions table
CREATE TABLE IF NOT EXISTS chat_sessions (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID REFERENCES users(id),
organization_id UUID REFERENCES organizations(id),
session_type TEXT DEFAULT 'phase1_exploration',
status TEXT DEFAULT 'active',
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Document processing table
CREATE TABLE IF NOT EXISTS document_processing (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
session_id UUID REFERENCES chat_sessions(id),
file_name TEXT NOT NULL,
file_type TEXT NOT NULL,
file_size_bytes INTEGER,
processing_status TEXT DEFAULT 'pending',
extracted_data JSONB,
document_summary TEXT,
key_insights TEXT[],
classification TEXT,
token_usage INTEGER DEFAULT 0,
organization_id UUID REFERENCES organizations(id),
uploaded_by UUID REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Teams table
CREATE TABLE IF NOT EXISTS teams (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
organization_id UUID REFERENCES organizations(id) NOT NULL,
created_by UUID REFERENCES users(id),
budget_limit_cents INTEGER,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Team memberships table
CREATE TABLE IF NOT EXISTS team_memberships (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
team_id UUID REFERENCES teams(id) ON DELETE CASCADE,
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
role TEXT DEFAULT 'member',
joined_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
UNIQUE(team_id, user_id)
);
-- Usage tracking table
CREATE TABLE IF NOT EXISTS usage_tracking (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
organization_id UUID REFERENCES organizations(id),
user_id UUID REFERENCES users(id),
session_id UUID REFERENCES chat_sessions(id),
usage_type TEXT NOT NULL,
cost_cents INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Health check table for testing
CREATE TABLE IF NOT EXISTS health_check (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
status TEXT DEFAULT 'healthy',
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Insert initial data
INSERT INTO health_check (status) VALUES ('healthy') ON CONFLICT DO NOTHING;
-- Create a test organization
INSERT INTO organizations (id, name, domain, subscription_tier)
VALUES ('00000000-0000-0000-0000-000000000001', 'Test Organization', 'localhost', 'enterprise')
ON CONFLICT (id) DO NOTHING;
-- Create a test user
INSERT INTO users (id, email, role, organization_id)
VALUES ('00000000-0000-0000-0000-000000000001', 'test@localhost', 'owner', '00000000-0000-0000-0000-000000000001')
ON CONFLICT (email) DO NOTHING;
-- Enable Row Level Security (optional for development)
-- ALTER TABLE organizations ENABLE ROW LEVEL SECURITY;
-- ALTER TABLE users ENABLE ROW LEVEL SECURITY;
-- ALTER TABLE chat_sessions ENABLE ROW LEVEL SECURITY;
-- ALTER TABLE document_processing ENABLE ROW LEVEL SECURITY;