Files
madbase/control-plane-ui/tests/integration/setup_db.sql
Vlad Durnea cffdf8af86
Some checks failed
CI/CD Pipeline / unit-tests (push) Failing after 1m16s
CI/CD Pipeline / integration-tests (push) Failing after 2m32s
CI/CD Pipeline / lint (push) Successful in 5m22s
CI/CD Pipeline / e2e-tests (push) Has been skipped
CI/CD Pipeline / build (push) Has been skipped
wip:milestone 0 fixes
2026-03-15 12:35:42 +02:00

71 lines
2.9 KiB
PL/PgSQL

DROP TABLE IF EXISTS public.todos;
CREATE TABLE public.todos (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
title text NOT NULL,
completed boolean DEFAULT false,
priority integer DEFAULT 0,
user_id uuid, -- For RLS testing later
created_at timestamptz DEFAULT now()
);
-- RPC Example
CREATE OR REPLACE FUNCTION public.hello_world(name text)
RETURNS text AS $$
BEGIN
RETURN 'Hello, ' || name || '!';
END;
$$ LANGUAGE plpgsql;
ALTER TABLE public.todos ENABLE ROW LEVEL SECURITY;
-- Grants for public
GRANT ALL ON public.todos TO anon, authenticated;
GRANT ALL ON public.todos TO service_role;
-- Realtime history permissions for service role (trigger writes)
GRANT USAGE ON SCHEMA madbase_realtime TO service_role;
GRANT INSERT ON madbase_realtime.messages TO service_role;
GRANT USAGE, SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA madbase_realtime TO service_role;
-- Grants for Realtime schema
GRANT USAGE ON SCHEMA madbase_realtime TO anon, authenticated;
GRANT ALL ON ALL TABLES IN SCHEMA madbase_realtime TO anon, authenticated;
GRANT ALL ON ALL SEQUENCES IN SCHEMA madbase_realtime TO anon, authenticated;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA madbase_realtime TO anon, authenticated;
-- Allow everything for anon for now to test basic CRUD
CREATE POLICY "Allow anon select" ON public.todos FOR SELECT TO anon USING (true);
CREATE POLICY "Allow anon insert" ON public.todos FOR INSERT TO anon WITH CHECK (true);
CREATE POLICY "Allow anon update" ON public.todos FOR UPDATE TO anon USING (true);
CREATE POLICY "Allow anon delete" ON public.todos FOR DELETE TO anon USING (true);
-- Allow authenticated users
CREATE POLICY "Allow auth select" ON public.todos FOR SELECT TO authenticated USING (true);
CREATE POLICY "Allow auth insert" ON public.todos FOR INSERT TO authenticated WITH CHECK (true);
CREATE POLICY "Allow auth update" ON public.todos FOR UPDATE TO authenticated USING (true);
CREATE POLICY "Allow auth delete" ON public.todos FOR DELETE TO authenticated USING (true);
-- Enable Realtime
CREATE TRIGGER realtime_todos
AFTER INSERT OR UPDATE OR DELETE ON public.todos
FOR EACH ROW EXECUTE FUNCTION madbase_realtime.broadcast_changes();
-- Storage Setup
INSERT INTO storage.buckets (id, name, public) VALUES ('test-bucket', 'test-bucket', true) ON CONFLICT DO NOTHING;
INSERT INTO storage.buckets (id, name, public) VALUES ('public-bucket', 'public-bucket', true) ON CONFLICT DO NOTHING;
INSERT INTO storage.buckets (id, name, public) VALUES ('private-bucket', 'private-bucket', false) ON CONFLICT DO NOTHING;
-- Allow anon to upload to test-bucket and public-bucket
DO $$
BEGIN
IF NOT EXISTS (
SELECT FROM pg_policies WHERE tablename = 'objects' AND policyname = 'Anon can insert into public buckets'
) THEN
CREATE POLICY "Anon can insert into public buckets"
ON storage.objects FOR INSERT
TO anon
WITH CHECK ( bucket_id IN ('test-bucket', 'public-bucket') );
END IF;
END
$$;