Files
thrilltrack-explorer/supabase/migrations/20251015192625_e5dbaea9-c017-4bd5-9803-e740adf3ca24.sql
gpt-engineer-app[bot] 444634dc85 Approve database migration
2025-10-15 19:29:06 +00:00

66 lines
2.4 KiB
SQL

-- Create entity_timeline_events table for historical milestones
CREATE TABLE public.entity_timeline_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
entity_id UUID NOT NULL,
entity_type TEXT NOT NULL CHECK (entity_type IN ('park', 'ride', 'company')),
event_type TEXT NOT NULL CHECK (event_type IN (
'name_change',
'operator_change',
'owner_change',
'location_change',
'status_change',
'closure',
'reopening',
'renovation',
'expansion',
'acquisition',
'milestone',
'other'
)),
event_date DATE NOT NULL,
event_date_precision TEXT DEFAULT 'day' CHECK (event_date_precision IN ('day', 'month', 'year')),
title TEXT NOT NULL,
description TEXT,
-- Type-specific relational data (NO JSON!)
from_value TEXT,
to_value TEXT,
from_entity_id UUID,
to_entity_id UUID,
from_location_id UUID REFERENCES public.locations(id),
to_location_id UUID REFERENCES public.locations(id),
-- Metadata
is_public BOOLEAN NOT NULL DEFAULT true,
display_order INTEGER DEFAULT 0,
created_by UUID REFERENCES auth.users(id),
approved_by UUID,
submission_id UUID REFERENCES public.content_submissions(id),
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
-- Create indexes for performance
CREATE INDEX idx_timeline_events_entity ON public.entity_timeline_events(entity_type, entity_id);
CREATE INDEX idx_timeline_events_date ON public.entity_timeline_events(event_date DESC);
CREATE INDEX idx_timeline_events_submission ON public.entity_timeline_events(submission_id) WHERE submission_id IS NOT NULL;
CREATE INDEX idx_timeline_events_created_by ON public.entity_timeline_events(created_by);
-- Enable RLS
ALTER TABLE public.entity_timeline_events ENABLE ROW LEVEL SECURITY;
-- Public can view approved public events
CREATE POLICY "Public can view public timeline events"
ON public.entity_timeline_events FOR SELECT
USING (is_public = true AND approved_by IS NOT NULL);
-- Users can view their own pending submissions
CREATE POLICY "Users can view their own timeline submissions"
ON public.entity_timeline_events FOR SELECT
USING (created_by = auth.uid() AND approved_by IS NULL);
-- Service role can manage (for approval process)
CREATE POLICY "Service role can manage timeline events"
ON public.entity_timeline_events FOR ALL
USING (auth.jwt() ->> 'role' = 'service_role');