From 44d1f4b9d9c5fab53ccdb30f05b9b049e22c8364 Mon Sep 17 00:00:00 2001 From: markosbenitez Date: Sat, 27 Jun 2026 09:47:38 -0300 Subject: [PATCH] =?UTF-8?q?fix(auth):=20corregir=20setup=5Ffirst=5Fadmin?= =?UTF-8?q?=20=E2=80=94=20psql=20vars=20no=20funcionan=20en=20DO=20$$?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reescribir con CTE INSERT...RETURNING para encadenar auth.users + user_roles en una sola sentencia SQL plana donde :variable sí funciona. Co-Authored-By: Claude Sonnet 4.6 --- scripts/setup_first_admin.sql | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/scripts/setup_first_admin.sql b/scripts/setup_first_admin.sql index 3d1e929..0c003e2 100644 --- a/scripts/setup_first_admin.sql +++ b/scripts/setup_first_admin.sql @@ -6,21 +6,15 @@ -- -v email='admin@ejemplo.com' -v password='CAMBIAR' \ -- < scripts/setup_first_admin.sql -- +-- NOTA: las variables psql solo funcionan en SQL plano, no en DO $$. +-- Este script usa un CTE INSERT...RETURNING para encadenar ambos inserts. +-- -- CAMBIAR CONTRASEÑA LUEGO DEL PRIMER LOGIN: --- El usuario puede cambiarla desde la app (si se implementa esa UI) --- o el director puede actualizarla con: --- UPDATE auth.users --- SET encrypted_password = extensions.crypt('NuevaContraseña', extensions.gen_salt('bf')) --- WHERE email = 'admin@ejemplo.com'; +-- docker exec -i supabase-db psql -U postgres -d postgres \ +-- -c "UPDATE auth.users SET encrypted_password = extensions.crypt('NuevaContraseña', extensions.gen_salt('bf')) WHERE email = 'admin@ejemplo.com';" -- ============================================================ -DO $$ -DECLARE - v_user_id uuid; - v_inq_org uuid := '10000000-0000-0000-0000-000000000002'; -BEGIN - - -- Insertar usuario en auth.users +WITH new_user AS ( INSERT INTO auth.users ( id, instance_id, @@ -52,13 +46,9 @@ BEGIN '', false ) - RETURNING id INTO v_user_id; - - -- Asignar rol platform_admin vinculado a InQuality - INSERT INTO public.user_roles (user_id, app_role, org_id) - VALUES (v_user_id, 'platform_admin', v_inq_org); - - RAISE NOTICE 'Usuario creado: % (id=%)', :'email', v_user_id; - -END; -$$; + RETURNING id, email +) +INSERT INTO public.user_roles (user_id, app_role, org_id) +SELECT id, 'platform_admin', '10000000-0000-0000-0000-000000000002' +FROM new_user +RETURNING (SELECT email FROM new_user), user_id;