Files
DeskcommCRM/scripts/selfhost-prelude.sql
T
Rafael MelgaçoandClaude Fable 5 519382ea8a docs(fusion): 4B — selfhost-prelude.sql exposto (Postgres puro) + nota no README
Achado da prova local: o prelude vivia só dentro do test-db.sh; quem usa
Postgres próprio sem Supabase precisava dele e não tinha como saber.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CsMA4LDfgNarXsqEoLJmyV
2026-07-18 12:48:51 -03:00

76 lines
2.9 KiB
SQL

-- selfhost-prelude.sql — stubs de compatibilidade para rodar o baseline num
-- POSTGRES PURO (sem Supabase): roles anon/authenticated/service_role, schemas
-- auth/extensions/storage com stubs mínimos, e as extensões que o dump supõe
-- (uuid-ossp, pgcrypto, vector, citext, pg_trgm).
--
-- QUANDO USAR: só no caminho "Postgres próprio" do self-host. Com um projeto
-- Supabase (o caminho recomendado do README), NADA disto é necessário — o
-- Supabase já fornece tudo nativo (e com Auth/Storage REAIS, não stubs).
-- LIMITE HONESTO: auth.users/storage aqui são STUBS — login e upload de mídia
-- do app exigem Supabase real; o worker/agente funcionam integralmente.
--
-- Fonte: o mesmo prelude do gate de CI (scripts/test-db.sh) — se editar um,
-- edite o outro.
do $$
begin
if not exists (select 1 from pg_roles where rolname = 'anon') then
create role anon nologin;
end if;
if not exists (select 1 from pg_roles where rolname = 'authenticated') then
create role authenticated nologin;
end if;
if not exists (select 1 from pg_roles where rolname = 'service_role') then
create role service_role nologin bypassrls;
end if;
end
$$;
create schema if not exists auth;
create schema if not exists extensions;
-- O baseline referencia extensions.uuid_generate_v4/gen_random_bytes e os tipos
-- public.vector/public.citext + gin_trgm_ops, mas não cria as extensões (pg_dump).
create extension if not exists "uuid-ossp" with schema extensions;
create extension if not exists pgcrypto with schema extensions;
create extension if not exists vector with schema public;
create extension if not exists citext with schema public;
create extension if not exists pg_trgm with schema public;
-- Stubs de storage (o apêndice do baseline cria buckets + policies em storage.objects).
create schema if not exists storage;
create table if not exists storage.buckets (
id text primary key,
name text not null,
public boolean not null default false,
file_size_limit bigint,
allowed_mime_types text[],
created_at timestamptz not null default now()
);
create table if not exists storage.objects (
id uuid primary key default gen_random_uuid(),
bucket_id text references storage.buckets(id),
name text,
owner uuid,
metadata jsonb,
created_at timestamptz not null default now()
);
-- Stub de auth.users (FKs do baseline apontam pra cá).
create table if not exists auth.users (
id uuid primary key default gen_random_uuid(),
email text unique,
created_at timestamptz not null default now()
);
-- Stub de auth.uid() lendo o claim `sub` de request.jwt.claims (mesmo contrato
-- do Supabase; os testes simulam o JWT via set_config).
create or replace function auth.uid() returns uuid
language sql stable
as $fn$
select nullif(current_setting('request.jwt.claims', true)::jsonb ->> 'sub', '')::uuid
$fn$;
grant usage on schema auth, extensions, storage to anon, authenticated, service_role;
grant select on auth.users to anon, authenticated, service_role;