-- Create products table CREATE TABLE IF NOT EXISTS public.products ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, name TEXT NOT NULL, url TEXT NOT NULL, store TEXT NOT NULL, category TEXT NOT NULL, -- 'harddrive', 'ram', 'processor', 'graphics_card' current_price DECIMAL(10,2) NOT NULL, currency TEXT DEFAULT 'DKK', specs JSONB NOT NULL, -- Store size, brand, speed, etc. last_checked TIMESTAMP WITH TIME ZONE DEFAULT now(), created_at TIMESTAMP WITH TIME ZONE DEFAULT now() ); -- Create alert profiles table (specification-based, not product-specific) CREATE TABLE IF NOT EXISTS public.alert_profiles ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, name TEXT NOT NULL, category TEXT NOT NULL, target_price DECIMAL(10,2) NOT NULL, specs_filter JSONB NOT NULL, -- e.g., {"min_size": "6TB", "type": "SSD"} discord_webhook_url TEXT, enabled BOOLEAN DEFAULT true, created_at TIMESTAMP WITH TIME ZONE DEFAULT now() ); -- Create price history table CREATE TABLE IF NOT EXISTS public.price_history ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, product_id UUID REFERENCES public.products(id) ON DELETE CASCADE, price DECIMAL(10,2) NOT NULL, checked_at TIMESTAMP WITH TIME ZONE DEFAULT now() ); -- Create alerts log table CREATE TABLE IF NOT EXISTS public.alerts_log ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, alert_profile_id UUID REFERENCES public.alert_profiles(id) ON DELETE CASCADE, product_id UUID REFERENCES public.products(id) ON DELETE CASCADE, triggered_price DECIMAL(10,2) NOT NULL, message TEXT, sent_to_discord BOOLEAN DEFAULT false, created_at TIMESTAMP WITH TIME ZONE DEFAULT now() ); -- Enable Row Level Security (but make everything public since no login) ALTER TABLE public.products ENABLE ROW LEVEL SECURITY; ALTER TABLE public.alert_profiles ENABLE ROW LEVEL SECURITY; ALTER TABLE public.price_history ENABLE ROW LEVEL SECURITY; ALTER TABLE public.alerts_log ENABLE ROW LEVEL SECURITY; -- Create public access policies (no authentication required) CREATE POLICY "Public read access for products" ON public.products FOR SELECT USING (true); CREATE POLICY "Public insert access for products" ON public.products FOR INSERT WITH CHECK (true); CREATE POLICY "Public update access for products" ON public.products FOR UPDATE USING (true); CREATE POLICY "Public delete access for products" ON public.products FOR DELETE USING (true); CREATE POLICY "Public read access for alert_profiles" ON public.alert_profiles FOR SELECT USING (true); CREATE POLICY "Public insert access for alert_profiles" ON public.alert_profiles FOR INSERT WITH CHECK (true); CREATE POLICY "Public update access for alert_profiles" ON public.alert_profiles FOR UPDATE USING (true); CREATE POLICY "Public delete access for alert_profiles" ON public.alert_profiles FOR DELETE USING (true); CREATE POLICY "Public read access for price_history" ON public.price_history FOR SELECT USING (true); CREATE POLICY "Public insert access for price_history" ON public.price_history FOR INSERT WITH CHECK (true); CREATE POLICY "Public read access for alerts_log" ON public.alerts_log FOR SELECT USING (true); CREATE POLICY "Public insert access for alerts_log" ON public.alerts_log FOR INSERT WITH CHECK (true); -- Create indexes for better performance CREATE INDEX idx_products_category ON public.products(category); CREATE INDEX idx_products_last_checked ON public.products(last_checked); CREATE INDEX idx_alert_profiles_category ON public.alert_profiles(category); CREATE INDEX idx_alert_profiles_enabled ON public.alert_profiles(enabled); CREATE INDEX idx_price_history_product ON public.price_history(product_id); CREATE INDEX idx_alerts_log_profile ON public.alerts_log(alert_profile_id);