From c236dc0e81923a7e4c31806cbe9df8a46b62ae91 Mon Sep 17 00:00:00 2001 From: Andrey Epifancev Date: Thu, 29 Oct 2026 00:10:01 +0400 Subject: [PATCH] =?UTF-8?q?perf(sql):=20=D0=B4=D0=B5=D0=BB=D1=8C=D1=82?= =?UTF-8?q?=D0=B0=20=D0=90=D0=A5=D0=A0=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20?= =?UTF-8?q?=5Fform=5Faffected=5Frows,=20=D1=81=20=D0=BF=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D0=BA=D0=B0=D0=BC=D0=B8=20SUB=5FSUB=5FITEM=20(AURORA-1850)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../sql/0051_upd_form_cells_subtree.sql | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/api/alembic/versions/sql/0051_upd_form_cells_subtree.sql b/api/alembic/versions/sql/0051_upd_form_cells_subtree.sql index 5f8befe..53f754d 100644 --- a/api/alembic/versions/sql/0051_upd_form_cells_subtree.sql +++ b/api/alembic/versions/sql/0051_upd_form_cells_subtree.sql @@ -2740,3 +2740,50 @@ BEGIN END; $function$ ; + + +CREATE OR REPLACE FUNCTION v3.get_form2_ahr_satellite_delta(p_form_id integer, p_source_sheet character varying, p_source_row_id integer) + RETURNS TABLE(row_type character varying, depth integer, sort_order bigint, data jsonb) + LANGUAGE sql + STABLE +AS $function$ + WITH target_lines AS ( + SELECT m.target_line_id AS line_id + FROM v3.form2_ahr_satellite_line_source m + JOIN v3.budget_line bl ON bl.id = m.target_line_id + WHERE m.source_sheet = p_source_sheet + AND m.source_id = p_source_row_id + AND bl.budget_form_id = p_form_id + AND p_source_sheet IN ('AHR_RENT', 'AHR_UTILITY', 'AHR_SECURITY') + + UNION + + SELECT bl.id AS line_id + FROM v3.limit_template lt + JOIN v3.expense_item ei + ON ei.sheet = 'AHR' + AND ei.direction = 'Support' + AND ( + (ei.depth = 3 AND ei.item_id = lt.expense_item_code) + OR ei.num_group_id = lt.expense_item_code + ) + JOIN v3.budget_line bl + ON bl.budget_form_id = p_form_id + AND bl.expense_item_id = ei.id + AND bl.name = regexp_replace( + lt.name, + '^[[:space:]]*-[[:space:]]*', + '' + ) + WHERE p_source_sheet = 'AHR_LIMIT' + AND lt.id = p_source_row_id + AND lt.row_type = 'LEAF' + ) + SELECT r.row_type, r.depth, r.sort_order, r.data + FROM v3._form_affected_rows( + p_form_id, 'AHR', + COALESCE((SELECT array_agg(tl.line_id) FROM target_lines tl), '{}'), + 'Support', NULL) r + ORDER BY r.sort_order; +$function$ +;