diff --git a/lib/supabase.ts b/lib/supabase.ts index ff32ad32..61927afd 100644 --- a/lib/supabase.ts +++ b/lib/supabase.ts @@ -166,6 +166,35 @@ export interface Database { indexed_at?: string; }; }; + ratings: { + Row: { + id: number; + storyline_id: number; + rater_address: string; + rating: number; + comment: string | null; + created_at: string; + updated_at: string; + }; + Insert: { + id?: never; + storyline_id: number; + rater_address: string; + rating: number; + comment?: string | null; + created_at?: string; + updated_at?: string; + }; + Update: { + id?: never; + storyline_id?: number; + rater_address?: string; + rating?: number; + comment?: string | null; + created_at?: string; + updated_at?: string; + }; + }; }; }; } @@ -174,3 +203,4 @@ export interface Database { export type Storyline = Database["public"]["Tables"]["storylines"]["Row"]; export type Plot = Database["public"]["Tables"]["plots"]["Row"]; export type Donation = Database["public"]["Tables"]["donations"]["Row"]; +export type Rating = Database["public"]["Tables"]["ratings"]["Row"]; diff --git a/supabase/migrations/00005_ratings.sql b/supabase/migrations/00005_ratings.sql new file mode 100644 index 00000000..d7af25f9 --- /dev/null +++ b/supabase/migrations/00005_ratings.sql @@ -0,0 +1,19 @@ +-- Ratings table: one rating per user per storyline (upsert pattern) +-- Public read, no public write (writes via service role only) + +CREATE TABLE ratings ( + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + storyline_id BIGINT NOT NULL REFERENCES storylines(storyline_id), + rater_address TEXT NOT NULL, + rating SMALLINT NOT NULL CHECK (rating >= 1 AND rating <= 5), + comment TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + UNIQUE (storyline_id, rater_address) +); + +CREATE INDEX idx_ratings_storyline ON ratings (storyline_id); + +ALTER TABLE ratings ENABLE ROW LEVEL SECURITY; + +CREATE POLICY "Public read" ON ratings FOR SELECT USING (true);