-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
98 lines (86 loc) · 3.16 KB
/
Copy pathschema.sql
File metadata and controls
98 lines (86 loc) · 3.16 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
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT UNIQUE NOT NULL,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
avatar_url TEXT,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS user_face_embeddings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
embedding vector(512) NOT NULL,
selfie_count INT DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT now(),
active BOOLEAN DEFAULT true,
UNIQUE(user_id)
);
CREATE TABLE IF NOT EXISTS communities (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT UNIQUE NOT NULL,
slug TEXT UNIQUE NOT NULL,
description TEXT,
banner_url TEXT,
created_by UUID REFERENCES users(id),
member_count INT DEFAULT 1,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS community_members (
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
community_id UUID REFERENCES communities(id) ON DELETE CASCADE,
role TEXT DEFAULT 'member',
joined_at TIMESTAMPTZ DEFAULT now(),
PRIMARY KEY (user_id, community_id)
);
CREATE TABLE IF NOT EXISTS threads (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
community_id UUID REFERENCES communities(id) ON DELETE CASCADE,
created_by UUID REFERENCES users(id),
title TEXT NOT NULL,
description TEXT,
event_date DATE,
location TEXT,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS photos (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
thread_id UUID REFERENCES threads(id) ON DELETE CASCADE,
uploaded_by UUID REFERENCES users(id),
storage_key TEXT NOT NULL,
url TEXT NOT NULL,
storage_key_thumb TEXT NOT NULL,
url_thumb TEXT NOT NULL,
indexed BOOLEAN DEFAULT false,
face_count INT DEFAULT 0,
uploaded_at TIMESTAMPTZ DEFAULT now(),
retry_count INT DEFAULT 0,
last_error TEXT,
last_attempted_at TIMESTAMPTZ
);
CREATE TABLE IF NOT EXISTS face_embeddings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
photo_id UUID REFERENCES photos(id) ON DELETE CASCADE,
thread_id UUID REFERENCES threads(id) ON DELETE CASCADE,
embedding vector(512) NOT NULL,
bbox JSONB,
det_score FLOAT,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS photo_faces (
photo_id UUID REFERENCES photos(id) ON DELETE CASCADE,
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
confidence FLOAT,
confirmed BOOLEAN DEFAULT NULL,
bbox JSONB,
matched_at TIMESTAMPTZ DEFAULT now(),
PRIMARY KEY (photo_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_face_embeddings_hnsw
ON face_embeddings USING hnsw (embedding vector_cosine_ops);
CREATE INDEX IF NOT EXISTS idx_face_embeddings_thread
ON face_embeddings(thread_id);
CREATE INDEX IF NOT EXISTS idx_photo_faces_user
ON photo_faces(user_id);
CREATE INDEX IF NOT EXISTS idx_photos_thread
ON photos(thread_id);