-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
93 lines (80 loc) · 3.21 KB
/
Copy pathschema.sql
File metadata and controls
93 lines (80 loc) · 3.21 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
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE IF NOT EXISTS app_users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email text NOT NULL UNIQUE,
name text NOT NULL DEFAULT '',
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS auth_sessions (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
token_hash text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL,
revoked_at timestamptz
);
CREATE INDEX IF NOT EXISTS idx_auth_sessions_user_id ON auth_sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_auth_sessions_token_hash ON auth_sessions(token_hash);
CREATE TABLE IF NOT EXISTS app_storage_documents (
user_id uuid NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
key text NOT NULL,
payload jsonb NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, key)
);
CREATE TABLE IF NOT EXISTS visitors (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
ip_hash text NOT NULL UNIQUE,
first_seen_at timestamptz NOT NULL,
last_seen_at timestamptz NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
visitor_id uuid NOT NULL REFERENCES visitors(id) ON DELETE CASCADE,
started_at timestamptz NOT NULL,
ended_at timestamptz,
user_agent text,
referrer text
);
CREATE INDEX IF NOT EXISTS idx_sessions_visitor_id ON sessions(visitor_id);
CREATE INDEX IF NOT EXISTS idx_sessions_started_at ON sessions(started_at);
CREATE TABLE IF NOT EXISTS uploads (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
session_id uuid NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
original_name text NOT NULL,
stored_name text NOT NULL,
mime text NOT NULL,
size_bytes bigint NOT NULL,
sha256 text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_uploads_session_id ON uploads(session_id);
CREATE INDEX IF NOT EXISTS idx_uploads_created_at ON uploads(created_at);
CREATE INDEX IF NOT EXISTS idx_uploads_sha256 ON uploads(sha256);
CREATE TABLE IF NOT EXISTS surveys (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
session_id uuid NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
purpose text NOT NULL,
additions text NOT NULL,
contact text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_surveys_session_id ON surveys(session_id);
CREATE INDEX IF NOT EXISTS idx_surveys_created_at ON surveys(created_at);
-- Stores processed result files (saved to disk alongside uploads)
CREATE TABLE IF NOT EXISTS results (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
session_id uuid NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
upload_id uuid REFERENCES uploads(id) ON DELETE SET NULL,
upload_original_name text NOT NULL,
result_name text NOT NULL,
stored_name text NOT NULL,
size_bytes bigint NOT NULL,
query text NOT NULL DEFAULT '',
rows_processed int NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_results_session_id ON results(session_id);
CREATE INDEX IF NOT EXISTS idx_results_created_at ON results(created_at);