102 lines
4.3 KiB
PL/PgSQL
102 lines
4.3 KiB
PL/PgSQL
-- BILL-14: persist optional registration seat preferences across verification.
|
|
--
|
|
-- The legacy 12-argument capability remains available during rolling deploys.
|
|
-- New runtimes use the v2 capability, which validates both quantities inside
|
|
-- the SECURITY DEFINER boundary before it can write the pre-auth row.
|
|
--
|
|
-- Inverse SQL (manual coordinated rollback only):
|
|
-- BEGIN;
|
|
-- SET LOCAL ROLE hivegrid_rls_definer;
|
|
-- REVOKE EXECUTE ON FUNCTION public.hivegrid_create_registration_v2(
|
|
-- text,text,text,text,text,text,text,integer,integer,text,text,text,text,text
|
|
-- ) FROM hivegrid_app;
|
|
-- DROP FUNCTION IF EXISTS public.hivegrid_create_registration_v2(
|
|
-- text,text,text,text,text,text,text,integer,integer,text,text,text,text,text
|
|
-- );
|
|
-- RESET ROLE;
|
|
-- ALTER TABLE public."Registration"
|
|
-- DROP COLUMN IF EXISTS "selectedFieldSeatQuantity",
|
|
-- DROP COLUMN IF EXISTS "selectedOfficeSeatQuantity";
|
|
-- COMMIT;
|
|
|
|
BEGIN;
|
|
|
|
ALTER TABLE public."Registration"
|
|
ADD COLUMN IF NOT EXISTS "selectedOfficeSeatQuantity" INTEGER,
|
|
ADD COLUMN IF NOT EXISTS "selectedFieldSeatQuantity" INTEGER;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT current_setting('is_superuser')::boolean
|
|
AND NOT pg_has_role(current_user, 'hivegrid_rls_definer', 'MEMBER') THEN
|
|
RAISE EXCEPTION 'Registration preference migration requires hivegrid_rls_definer membership';
|
|
END IF;
|
|
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'hivegrid_app') THEN
|
|
RAISE EXCEPTION 'hivegrid_app must exist before registration preference capability';
|
|
END IF;
|
|
END$$;
|
|
|
|
GRANT CREATE ON SCHEMA public TO hivegrid_rls_definer;
|
|
SET LOCAL ROLE hivegrid_rls_definer;
|
|
|
|
CREATE OR REPLACE FUNCTION public.hivegrid_create_registration_v2(
|
|
p_id text, p_email text, p_first_name text, p_last_name text, p_token text,
|
|
p_company text, p_price_id text, p_office_seat_quantity integer,
|
|
p_field_seat_quantity integer, p_lead_id text, p_lead_token text,
|
|
p_lead_plan_id text, p_ip_address text, p_slug text
|
|
)
|
|
RETURNS SETOF public."Registration"
|
|
LANGUAGE sql VOLATILE SECURITY DEFINER
|
|
SET search_path = pg_catalog, public SET row_security = off
|
|
AS $function$
|
|
INSERT INTO public."Registration" AS registration (
|
|
"id", "email", "firstName", "lastName", "token", "company",
|
|
"selectedSubscriptionPriceId", "selectedOfficeSeatQuantity",
|
|
"selectedFieldSeatQuantity", "leadConversionLeadId", "leadConversionToken",
|
|
"leadConversionPlanId", "ipAddress", "slug"
|
|
)
|
|
SELECT
|
|
p_id, lower(trim(p_email)), p_first_name, p_last_name, p_token, p_company,
|
|
p_price_id, p_office_seat_quantity, p_field_seat_quantity, p_lead_id,
|
|
p_lead_token, p_lead_plan_id, p_ip_address, p_slug
|
|
WHERE
|
|
(p_office_seat_quantity IS NULL AND p_field_seat_quantity IS NULL)
|
|
OR (
|
|
p_office_seat_quantity BETWEEN 1 AND 10000
|
|
AND p_field_seat_quantity BETWEEN 0 AND 10000
|
|
)
|
|
ON CONFLICT ("email") DO UPDATE
|
|
SET "createdAt" = CURRENT_TIMESTAMP,
|
|
"firstName" = EXCLUDED."firstName",
|
|
"lastName" = EXCLUDED."lastName",
|
|
"token" = EXCLUDED."token",
|
|
"company" = EXCLUDED."company",
|
|
"selectedSubscriptionPriceId" = EXCLUDED."selectedSubscriptionPriceId",
|
|
"selectedOfficeSeatQuantity" = EXCLUDED."selectedOfficeSeatQuantity",
|
|
"selectedFieldSeatQuantity" = EXCLUDED."selectedFieldSeatQuantity",
|
|
"leadConversionLeadId" = EXCLUDED."leadConversionLeadId",
|
|
"leadConversionToken" = EXCLUDED."leadConversionToken",
|
|
"leadConversionPlanId" = EXCLUDED."leadConversionPlanId",
|
|
"ipAddress" = EXCLUDED."ipAddress",
|
|
"slug" = EXCLUDED."slug"
|
|
WHERE registration."createdTenantId" IS NULL
|
|
AND registration."createdAt" <= CURRENT_TIMESTAMP - INTERVAL '24 hours'
|
|
AND registration."token" <> EXCLUDED."token"
|
|
RETURNING registration.*
|
|
$function$;
|
|
|
|
ALTER FUNCTION public.hivegrid_create_registration_v2(
|
|
text,text,text,text,text,text,text,integer,integer,text,text,text,text,text
|
|
) OWNER TO hivegrid_rls_definer;
|
|
REVOKE ALL ON FUNCTION public.hivegrid_create_registration_v2(
|
|
text,text,text,text,text,text,text,integer,integer,text,text,text,text,text
|
|
) FROM PUBLIC, hivegrid_app, hivegrid_worker, hivegrid_authentik_jit;
|
|
GRANT EXECUTE ON FUNCTION public.hivegrid_create_registration_v2(
|
|
text,text,text,text,text,text,text,integer,integer,text,text,text,text,text
|
|
) TO hivegrid_app;
|
|
|
|
RESET ROLE;
|
|
REVOKE CREATE ON SCHEMA public FROM hivegrid_rls_definer;
|
|
|
|
COMMIT;
|