-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
103 lines (87 loc) · 3.1 KB
/
supabase-schema.sql
File metadata and controls
103 lines (87 loc) · 3.1 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
99
100
101
102
103
-- Coins Inventory Database Schema
-- Run this in your Supabase SQL Editor
-- Create coins table
CREATE TABLE IF NOT EXISTS coins (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
year INTEGER NOT NULL,
country TEXT NOT NULL,
denomination TEXT NOT NULL,
mint_mark TEXT,
grade TEXT,
value NUMERIC,
description TEXT,
image TEXT,
date_added TIMESTAMPTZ NOT NULL DEFAULT NOW(),
category TEXT,
composition TEXT,
weight NUMERIC,
diameter NUMERIC,
registration_number TEXT,
grading_company TEXT,
face_value TEXT,
special_collection TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Create an index on date_added for faster sorting
CREATE INDEX IF NOT EXISTS idx_coins_date_added ON coins(date_added DESC);
-- Create an index on country for filtering
CREATE INDEX IF NOT EXISTS idx_coins_country ON coins(country);
-- Create a full-text search index for searching coins
CREATE INDEX IF NOT EXISTS idx_coins_search ON coins USING gin(
to_tsvector('english', coalesce(name, '') || ' ' || coalesce(country, '') || ' ' || coalesce(denomination, ''))
);
-- Enable Row Level Security (RLS)
ALTER TABLE coins ENABLE ROW LEVEL SECURITY;
-- Create a policy that allows anyone to read all coins (public access)
CREATE POLICY "Allow public read access" ON coins
FOR SELECT
USING (true);
-- Create a policy that allows anyone to insert coins (public access)
CREATE POLICY "Allow public insert access" ON coins
FOR INSERT
WITH CHECK (true);
-- Create a policy that allows anyone to update coins (public access)
CREATE POLICY "Allow public update access" ON coins
FOR UPDATE
USING (true)
WITH CHECK (true);
-- Create a policy that allows anyone to delete coins (public access)
CREATE POLICY "Allow public delete access" ON coins
FOR DELETE
USING (true);
-- Create a function to automatically update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create a trigger to call the function on updates
CREATE TRIGGER update_coins_updated_at
BEFORE UPDATE ON coins
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Create storage bucket for coin images
INSERT INTO storage.buckets (id, name, public)
VALUES ('coin-images', 'coin-images', true)
ON CONFLICT (id) DO NOTHING;
-- Create storage policy for public read access
CREATE POLICY "Public read access for coin images" ON storage.objects
FOR SELECT
USING (bucket_id = 'coin-images');
-- Create storage policy for public upload access
CREATE POLICY "Public upload access for coin images" ON storage.objects
FOR INSERT
WITH CHECK (bucket_id = 'coin-images');
-- Create storage policy for public update access
CREATE POLICY "Public update access for coin images" ON storage.objects
FOR UPDATE
USING (bucket_id = 'coin-images')
WITH CHECK (bucket_id = 'coin-images');
-- Create storage policy for public delete access
CREATE POLICY "Public delete access for coin images" ON storage.objects
FOR DELETE
USING (bucket_id = 'coin-images');