fix(auth): corregir setup_first_admin — psql vars no funcionan en DO $$
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
RETURNING id, email
|
||||
)
|
||||
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;
|
||||
$$;
|
||||
SELECT id, 'platform_admin', '10000000-0000-0000-0000-000000000002'
|
||||
FROM new_user
|
||||
RETURNING (SELECT email FROM new_user), user_id;
|
||||
|
||||
Reference in New Issue
Block a user