-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
96 lines (85 loc) · 4.31 KB
/
supabase_schema.sql
File metadata and controls
96 lines (85 loc) · 4.31 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
-- =============================================================================
-- Fancy 2FA (Guardian Gate) - Full Database Schema
-- =============================================================================
-- Version: 1.0.2
-- Description: Complete database schema for fresh deployment (non-migration)
-- Usage: Run this SQL in your Supabase SQL Editor for new installations
-- =============================================================================
-- -----------------------------------------------------------------------------
-- Users Table
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
email TEXT,
client TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- -----------------------------------------------------------------------------
-- Tags Table
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tags (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
color TEXT NOT NULL
);
-- -----------------------------------------------------------------------------
-- Accounts Table
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS accounts (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
issuer TEXT NOT NULL,
account TEXT NOT NULL,
secret TEXT NOT NULL,
remark TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Add comment for remark column
COMMENT ON COLUMN accounts.remark IS 'Account remark/note';
-- -----------------------------------------------------------------------------
-- Account_Tags Join Table
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS account_tags (
account_id BIGINT REFERENCES accounts(id) ON DELETE CASCADE,
tag_id BIGINT REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (account_id, tag_id)
);
-- -----------------------------------------------------------------------------
-- Share Links Table
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS share_links (
id SERIAL PRIMARY KEY,
short_link VARCHAR(16) NOT NULL UNIQUE,
account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for share_links
CREATE INDEX IF NOT EXISTS idx_share_links_short_link ON share_links(short_link);
CREATE INDEX IF NOT EXISTS idx_share_links_account_id ON share_links(account_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_share_links_account_unique ON share_links(account_id);
-- -----------------------------------------------------------------------------
-- Row Level Security (RLS)
-- -----------------------------------------------------------------------------
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE tags ENABLE ROW LEVEL SECURITY;
ALTER TABLE accounts ENABLE ROW LEVEL SECURITY;
ALTER TABLE account_tags ENABLE ROW LEVEL SECURITY;
ALTER TABLE share_links ENABLE ROW LEVEL SECURITY;
-- -----------------------------------------------------------------------------
-- RLS Policies
-- -----------------------------------------------------------------------------
-- Note: These policies allow full access. For production, you may want to
-- implement more restrictive policies based on your authentication setup.
-- If using SERVICE_ROLE key in the backend, RLS is bypassed.
CREATE POLICY "Allow full access to service role" ON users FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "Allow full access to service role" ON tags FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "Allow full access to service role" ON accounts FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "Allow full access to service role" ON account_tags FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "Allow full access to service role" ON share_links FOR ALL USING (true) WITH CHECK (true);
-- -----------------------------------------------------------------------------
-- Default Data
-- -----------------------------------------------------------------------------
-- Insert default admin user if not exists
INSERT INTO users (name, email, client)
VALUES ('admin', '[email protected]', 'default')
ON CONFLICT DO NOTHING;