-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
68 lines (57 loc) · 2.02 KB
/
schema.sql
File metadata and controls
68 lines (57 loc) · 2.02 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
-- dynQR Database Schema for Supabase
-- Run this in your Supabase SQL Editor
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Create qr_codes table
CREATE TABLE qr_codes (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
short_id VARCHAR(8) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
current_url TEXT NOT NULL,
description TEXT,
scan_count INTEGER DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
last_scanned_at TIMESTAMP WITH TIME ZONE
);
-- Indexes for performance
CREATE INDEX idx_qr_codes_short_id ON qr_codes(short_id);
CREATE INDEX idx_qr_codes_user_id ON qr_codes(user_id);
-- Row Level Security (RLS)
ALTER TABLE qr_codes ENABLE ROW LEVEL SECURITY;
-- Policy: Users can only view their own QR codes
CREATE POLICY "Users can view own QR codes"
ON qr_codes FOR SELECT
USING (auth.uid() = user_id);
-- Policy: Users can create QR codes assigned to themselves
CREATE POLICY "Users can create QR codes"
ON qr_codes FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Policy: Users can update their own QR codes
CREATE POLICY "Users can update own QR codes"
ON qr_codes FOR UPDATE
USING (auth.uid() = user_id);
-- Policy: Users can delete their own QR codes
CREATE POLICY "Users can delete own QR codes"
ON qr_codes FOR DELETE
USING (auth.uid() = user_id);
-- Policy: Public read for redirect handler (by short_id only)
-- This allows the /qr/[shortId] route to work without authentication
CREATE POLICY "Public can read QR by short_id"
ON qr_codes FOR SELECT
USING (true);
-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Trigger to auto-update updated_at
CREATE TRIGGER update_qr_codes_updated_at
BEFORE UPDATE ON qr_codes
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();