Files
editor/supabase/migrations/20240216000004_create_property_likes.sql
T
Aymeric RabotandClaude Opus 4.6 aa27e0dd7a refactor: move supabase config to repo root, fix port and env setup
- Move supabase/ from packages/db/ to repo root for simpler CLI usage
- Use custom ports (553xx) to avoid conflicts with other local Supabase instances
- Add --env-mode=loose to turbo dev so root .env vars reach Next.js
- Remove hardcoded --port 3000 from next dev (reads PORT env var instead)
- Add .env.example with Supabase env var placeholders
- Add supabase local state dirs to .gitignore

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-18 01:48:53 -05:00

42 lines
1.4 KiB
PL/PgSQL

-- Create property_likes table to track user likes
CREATE TABLE IF NOT EXISTS property_likes (
id TEXT PRIMARY KEY,
property_id TEXT NOT NULL REFERENCES properties(id) ON DELETE CASCADE,
user_id TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
-- Ensure a user can only like a property once
UNIQUE(property_id, user_id)
);
-- Enable RLS
ALTER TABLE property_likes ENABLE ROW LEVEL SECURITY;
-- RLS Policies
-- Users can view all likes (to see like counts)
CREATE POLICY "Anyone can view likes"
ON property_likes FOR SELECT
USING (true);
-- Users can insert their own likes
CREATE POLICY "Users can create their own likes"
ON property_likes FOR INSERT
WITH CHECK (user_id = current_setting('app.user_id', true)::TEXT);
-- Users can delete their own likes
CREATE POLICY "Users can delete their own likes"
ON property_likes FOR DELETE
USING (user_id = current_setting('app.user_id', true)::TEXT);
-- Create index for efficient querying
CREATE INDEX IF NOT EXISTS idx_property_likes_property_id ON property_likes(property_id);
CREATE INDEX IF NOT EXISTS idx_property_likes_user_id ON property_likes(user_id);
-- Function to get like count for a property
CREATE OR REPLACE FUNCTION get_property_like_count(property_id TEXT)
RETURNS INTEGER AS $$
BEGIN
RETURN (SELECT COUNT(*)::INTEGER FROM property_likes WHERE property_likes.property_id = $1);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER STABLE;