-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.sql
More file actions
50 lines (44 loc) · 1.65 KB
/
Copy pathuser.sql
File metadata and controls
50 lines (44 loc) · 1.65 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
-- 1. Create the base users table FIRST
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
-- Profile Data
first_name VARCHAR(255),
last_name VARCHAR(255),
avatar_url VARCHAR(255),
bio TEXT,
-- Settings & Status
theme VARCHAR(50) DEFAULT 'light',
notifications_enabled BOOLEAN DEFAULT TRUE,
is_email_verified BOOLEAN DEFAULT FALSE,
-- Timestamps
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 2. Consolidate temporary tokens (Resets, Verifications, Magic Links)
CREATE TABLE IF NOT EXISTS user_tokens (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE,
token_type VARCHAR(50) NOT NULL, -- e.g., 'email_verify', 'password_reset'
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 3. Sessions (if you aren't using JWTs)
CREATE TABLE IF NOT EXISTS sessions (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
session_token VARCHAR(255) NOT NULL UNIQUE,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 4. Activity Logs (Great for SaaS analytics and audit trails)
CREATE TABLE IF NOT EXISTS user_activities (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
activity_type VARCHAR(255) NOT NULL,
activity_data JSONB,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);