diff --git a/app/models/subscription_event_trigger.rb b/app/models/subscription_event_trigger.rb index 222e6eca8f8..33b719396e4 100644 --- a/app/models/subscription_event_trigger.rb +++ b/app/models/subscription_event_trigger.rb @@ -2,6 +2,10 @@ class SubscriptionEventTrigger < ApplicationRecord validates :organization_id, :external_subscription_id, :created_at, presence: true + + def self.trigger(organization_id:, external_subscription_id:) + connection.select_all sanitize_sql_array(["call trigger_subscription_update(?,?, null)", organization_id, external_subscription_id]) + end end # == Schema Information diff --git a/db/events_structure.sql b/db/events_structure.sql index 09e77ae638c..530b647d983 100644 --- a/db/events_structure.sql +++ b/db/events_structure.sql @@ -570,7 +570,7 @@ DROP TABLE IF EXISTS public.add_ons; DROP TABLE IF EXISTS public.active_storage_variant_records; DROP TABLE IF EXISTS public.active_storage_blobs; DROP TABLE IF EXISTS public.active_storage_attachments; -DROP PROCEDURE IF EXISTS public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying); +DROP PROCEDURE IF EXISTS public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying, INOUT result_id uuid); DROP TYPE IF EXISTS public.tax_status; DROP TYPE IF EXISTS public.subscription_invoicing_reason; DROP TYPE IF EXISTS public.payment_payable_payment_status; @@ -683,10 +683,10 @@ CREATE TYPE public.tax_status AS ENUM ( -- --- Name: trigger_subscription_update(uuid, character varying); Type: PROCEDURE; Schema: public; Owner: - +-- Name: trigger_subscription_update(uuid, character varying, uuid); Type: PROCEDURE; Schema: public; Owner: - -- -CREATE PROCEDURE public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying) +CREATE PROCEDURE public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying, INOUT result_id uuid) LANGUAGE plpgsql AS $$ BEGIN @@ -700,7 +700,8 @@ CREATE PROCEDURE public.trigger_subscription_update(IN p_organization_id uuid, I p_external_subscription_id, NOW() ) - ON CONFLICT DO NOTHING; + ON CONFLICT DO NOTHING + RETURNING id INTO result_id; END; $$; diff --git a/db/migrate/20241203140905_add_stored_procedure_for_triggering_subscription.rb b/db/migrate/20241203140905_add_stored_procedure_for_triggering_subscription.rb index 808b19f20af..42b055f3577 100644 --- a/db/migrate/20241203140905_add_stored_procedure_for_triggering_subscription.rb +++ b/db/migrate/20241203140905_add_stored_procedure_for_triggering_subscription.rb @@ -6,7 +6,8 @@ def up execute " CREATE OR REPLACE PROCEDURE trigger_subscription_update( p_organization_id UUID, - p_external_subscription_id varchar + p_external_subscription_id varchar, + result_id INOUT UUID ) LANGUAGE plpgsql AS $$ @@ -21,7 +22,8 @@ def up p_external_subscription_id, NOW() ) - ON CONFLICT DO NOTHING; + ON CONFLICT DO NOTHING + RETURNING id INTO result_id; END; $$;" end diff --git a/db/structure.sql b/db/structure.sql index 09e77ae638c..530b647d983 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -570,7 +570,7 @@ DROP TABLE IF EXISTS public.add_ons; DROP TABLE IF EXISTS public.active_storage_variant_records; DROP TABLE IF EXISTS public.active_storage_blobs; DROP TABLE IF EXISTS public.active_storage_attachments; -DROP PROCEDURE IF EXISTS public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying); +DROP PROCEDURE IF EXISTS public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying, INOUT result_id uuid); DROP TYPE IF EXISTS public.tax_status; DROP TYPE IF EXISTS public.subscription_invoicing_reason; DROP TYPE IF EXISTS public.payment_payable_payment_status; @@ -683,10 +683,10 @@ CREATE TYPE public.tax_status AS ENUM ( -- --- Name: trigger_subscription_update(uuid, character varying); Type: PROCEDURE; Schema: public; Owner: - +-- Name: trigger_subscription_update(uuid, character varying, uuid); Type: PROCEDURE; Schema: public; Owner: - -- -CREATE PROCEDURE public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying) +CREATE PROCEDURE public.trigger_subscription_update(IN p_organization_id uuid, IN p_external_subscription_id character varying, INOUT result_id uuid) LANGUAGE plpgsql AS $$ BEGIN @@ -700,7 +700,8 @@ CREATE PROCEDURE public.trigger_subscription_update(IN p_organization_id uuid, I p_external_subscription_id, NOW() ) - ON CONFLICT DO NOTHING; + ON CONFLICT DO NOTHING + RETURNING id INTO result_id; END; $$;