Migrations

This commit is contained in:
Marc Rejohn Castillano 2026-03-01 18:51:53 +08:00
parent 2100516238
commit 830c99a3ff
7 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,24 @@
-- Add 'cancelled' to task_status enum (if it exists) and add cancellation_reason column
DO $$
BEGIN
-- Add enum value if task_status enum exists
IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'task_status') THEN
IF NOT EXISTS (
SELECT 1
FROM pg_enum
JOIN pg_type ON pg_enum.enumtypid = pg_type.oid
WHERE pg_type.typname = 'task_status' AND pg_enum.enumlabel = 'cancelled'
) THEN
ALTER TYPE task_status ADD VALUE 'cancelled';
END IF;
END IF;
-- Add cancellation_reason column if it doesn't exist
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name='tasks' AND column_name='cancellation_reason'
) THEN
ALTER TABLE public.tasks ADD COLUMN cancellation_reason text;
END IF;
END
$$;

View File

@ -0,0 +1,46 @@
-- Enable RLS on task_activity_logs and allow authenticated inserts
-- Also add cancelled_at column to tasks table if missing
-- Enable RLS for task_activity_logs (idempotent)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
EXECUTE 'ALTER TABLE public.task_activity_logs ENABLE ROW LEVEL SECURITY';
END IF;
EXCEPTION WHEN others THEN
-- ignore
END
$$;
-- Create a permissive INSERT policy for authenticated users (idempotent)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
IF NOT EXISTS (
SELECT 1 FROM pg_policies WHERE polname = 'allow_authenticated_inserts'
AND polrelid = 'public.task_activity_logs'::regclass
) THEN
CREATE POLICY allow_authenticated_inserts
ON public.task_activity_logs
FOR INSERT
TO authenticated
USING (true)
WITH CHECK (true);
END IF;
END IF;
EXCEPTION WHEN others THEN
-- ignore
END
$$;
-- Add cancelled_at column to tasks if it does not exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name='tasks' AND column_name='cancelled_at'
) THEN
ALTER TABLE public.tasks ADD COLUMN cancelled_at timestamptz;
END IF;
END
$$;

View File

@ -0,0 +1,36 @@
-- Ensure authenticated users can insert into task_activity_logs
-- Idempotent: drops and re-creates a permissive INSERT policy for `authenticated`.
DO $$
BEGIN
-- Ensure table exists
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
RAISE NOTICE 'table task_activity_logs does not exist; skipping policy creation';
RETURN;
END IF;
-- Enable row level security (idempotent)
EXECUTE 'ALTER TABLE IF EXISTS public.task_activity_logs ENABLE ROW LEVEL SECURITY';
-- Drop any existing permissive insert policy we manage
IF EXISTS (
SELECT 1 FROM pg_policies
WHERE polname = 'allow_auth_inserts_all'
AND polrelid = 'public.task_activity_logs'::regclass
) THEN
EXECUTE 'DROP POLICY IF EXISTS allow_auth_inserts_all ON public.task_activity_logs';
END IF;
-- Create a permissive INSERT policy for authenticated users
EXECUTE 'CREATE POLICY allow_auth_inserts_all ON public.task_activity_logs FOR INSERT TO authenticated USING (true) WITH CHECK (true)';
-- Also allow the service_role for function-based inserts (optional)
IF NOT EXISTS (
SELECT 1 FROM pg_policies
WHERE polname = 'allow_service_role_all'
AND polrelid = 'public.task_activity_logs'::regclass
) THEN
EXECUTE 'CREATE POLICY allow_service_role_all ON public.task_activity_logs FOR ALL TO authenticated USING (true) WITH CHECK (true)';
END IF;
END
$$;

View File

@ -0,0 +1,15 @@
-- Recreate a permissive INSERT policy for authenticated users on task_activity_logs
-- Idempotent: drops existing policy and recreates it.
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
-- enable RLS (idempotent)
EXECUTE 'ALTER TABLE IF EXISTS public.task_activity_logs ENABLE ROW LEVEL SECURITY';
-- drop any old policy and recreate permissive insert policy for authenticated role
EXECUTE 'DROP POLICY IF EXISTS allow_authenticated_inserts ON public.task_activity_logs';
EXECUTE 'CREATE POLICY allow_authenticated_inserts ON public.task_activity_logs FOR INSERT TO authenticated USING (true) WITH CHECK (true)';
END IF;
END
$$;

View File

@ -0,0 +1,10 @@
-- Fix INSERT policy for task_activity_logs: WITH CHECK only for INSERT
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
EXECUTE 'ALTER TABLE IF EXISTS public.task_activity_logs ENABLE ROW LEVEL SECURITY';
EXECUTE 'DROP POLICY IF EXISTS allow_authenticated_inserts ON public.task_activity_logs';
EXECUTE 'CREATE POLICY allow_authenticated_inserts ON public.task_activity_logs FOR INSERT TO authenticated WITH CHECK (true)';
END IF;
END
$$;

View File

@ -0,0 +1,13 @@
-- Add permissive INSERT policy for anon (idempotent).
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
-- create policy only if it doesn't already exist
IF NOT EXISTS (
SELECT 1 FROM pg_policies WHERE tablename = 'task_activity_logs' AND policyname = 'allow_anon_insert'
) THEN
EXECUTE 'CREATE POLICY allow_anon_insert ON public.task_activity_logs FOR INSERT TO anon WITH CHECK (true)';
END IF;
END IF;
END
$$;

View File

@ -0,0 +1,16 @@
-- Allow authenticated users to SELECT from task_activity_logs
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='task_activity_logs') THEN
-- enable RLS (idempotent)
EXECUTE 'ALTER TABLE IF EXISTS public.task_activity_logs ENABLE ROW LEVEL SECURITY';
-- create SELECT policy for authenticated if not exists
IF NOT EXISTS (
SELECT 1 FROM pg_policies WHERE tablename = 'task_activity_logs' AND policyname = 'allow_authenticated_select'
) THEN
EXECUTE 'CREATE POLICY allow_authenticated_select ON public.task_activity_logs FOR SELECT TO authenticated USING (true)';
END IF;
END IF;
END
$$;