-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-simple-setup.sql
More file actions
161 lines (131 loc) · 5.68 KB
/
Copy pathsupabase-simple-setup.sql
File metadata and controls
161 lines (131 loc) · 5.68 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
-- PANASA Website - Simple Database Setup
-- Start with just the players table to get the ratings page working
-- =============================================
-- 1. DROP EXISTING POLICIES AND TABLES (if they exist)
-- =============================================
-- Drop existing policies
DROP POLICY IF EXISTS "Allow public read access to players" ON public.players;
DROP POLICY IF EXISTS "Allow public read access to federations" ON public.federations;
DROP POLICY IF EXISTS "Allow public read access to published news" ON public.news;
DROP POLICY IF EXISTS "Allow public read access to published events" ON public.events;
DROP POLICY IF EXISTS "Allow public read access to published resources" ON public.resources;
-- Drop existing tables
DROP TABLE IF EXISTS public.players CASCADE;
DROP TABLE IF EXISTS public.federations CASCADE;
DROP TABLE IF EXISTS public.news CASCADE;
DROP TABLE IF EXISTS public.events CASCADE;
DROP TABLE IF EXISTS public.resources CASCADE;
-- =============================================
-- 2. CREATE PLAYERS TABLE
-- =============================================
CREATE TABLE public.players (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
nick TEXT NOT NULL UNIQUE,
country TEXT NOT NULL,
name TEXT NOT NULL,
games INTEGER NOT NULL DEFAULT 0,
rating INTEGER NOT NULL DEFAULT 1000,
last_played DATE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Enable RLS and create policy
ALTER TABLE public.players ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow public read access to players" ON public.players
FOR SELECT USING (true);
-- Create indexes
CREATE INDEX players_rating_idx ON public.players (rating DESC);
CREATE INDEX players_country_idx ON public.players (country);
CREATE INDEX players_name_idx ON public.players (name);
-- =============================================
-- 3. CREATE FEDERATIONS TABLE
-- =============================================
CREATE TABLE public.federations (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
country TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
president TEXT NOT NULL,
secretary TEXT,
email TEXT NOT NULL,
phone TEXT NOT NULL,
address TEXT NOT NULL,
website TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Enable RLS and create policy
ALTER TABLE public.federations ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow public read access to federations" ON public.federations
FOR SELECT USING (true);
-- =============================================
-- 4. CREATE NEWS TABLE
-- =============================================
CREATE TABLE public.news (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
title TEXT NOT NULL,
summary TEXT,
content TEXT NOT NULL,
author TEXT NOT NULL,
image_url TEXT,
published BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Enable RLS and create policy
ALTER TABLE public.news ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow public read access to published news" ON public.news
FOR SELECT USING (published = true);
-- Create index
CREATE INDEX news_published_created_at_idx ON public.news (published, created_at DESC);
-- =============================================
-- 5. CREATE EVENTS TABLE
-- =============================================
CREATE TABLE public.events (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
title TEXT NOT NULL,
description TEXT NOT NULL,
location TEXT NOT NULL,
start_date DATE NOT NULL,
end_date DATE,
image_url TEXT,
registration_url TEXT,
published BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- Enable RLS and create policy
ALTER TABLE public.events ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow public read access to published events" ON public.events
FOR SELECT USING (published = true);
-- Create index
CREATE INDEX events_published_start_date_idx ON public.events (published, start_date DESC);
-- =============================================
-- 6. UPDATE TRIGGERS
-- =============================================
-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = timezone('utc'::text, now());
RETURN NEW;
END;
$$ language 'plpgsql';
-- Create triggers
CREATE TRIGGER update_players_updated_at BEFORE UPDATE ON public.players
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_federations_updated_at BEFORE UPDATE ON public.federations
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_news_updated_at BEFORE UPDATE ON public.news
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_events_updated_at BEFORE UPDATE ON public.events
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
-- =============================================
-- SUCCESS MESSAGE
-- =============================================
DO $$
BEGIN
RAISE NOTICE 'PANASA database setup completed successfully!';
RAISE NOTICE 'Tables created: players, federations, news, events';
RAISE NOTICE 'All tables have public read access enabled';
RAISE NOTICE 'You can now run insert-players.sql to add player data';
END $$;