Skip to content

Commit d50a4dd

Browse files
ilramdhanclaude
andcommitted
fix(finance): widen rm_cost flag_*_used constraints for V2 cascade labels + pass period to Oracle sync procedure
chk_rm_cost_flag_valuation_used/chk_rm_cost_flag_marketing_used still only allowed V1 stage labels (CONS/STORES/DEPT/PO_1/PO_2/PO_3/INIT), but buildOrApplyCost (V2 calc engine) has written V2 cascade-resolved labels (CR/SR/PR/CL/SL/FL, SP/PP/FP) into those columns since ENG-RM-01/P3. Any head whose cascade resolved to a V2-only label violated the CHECK, failing RM_COST_CALC jobs chained after oracle-sync (e.g. head 202006002 on flag_marketing_used). Widen both constraints to accept the V2 labels alongside the legacy V1 ones. Also wire the Oracle procedure's newly added p_period parameter (PRC_CST_CONSSTKPO_MGT now accepts p_period IN VARCHAR2 DEFAULT NULL 'YYYYMM') through executeProcedure via the existing but previously unused ExecuteProcedureWithParam, so the Oracle-side refresh always targets the same period the Go job already resolved instead of relying on the procedure's own SYSDATE-based auto-period fallback. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 2c7e23a commit d50a4dd

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

services/finance/internal/application/oraclesync/sync_handler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,18 @@ func (h *SyncHandler) publishCostChain(ctx context.Context, period, createdBy st
180180

181181
func (h *SyncHandler) executeProcedure(ctx context.Context, jobID uuid.UUID, period string) error {
182182
logEntry := job.NewExecutionLog(jobID, stepProcedure, job.LogStarted,
183-
fmt.Sprintf("Executing %s.%s (auto-period from SYSDATE, requested: %s)", OracleSchema, OracleProcedure, period), nil)
183+
fmt.Sprintf("Executing %s.%s(p_period => '%s')", OracleSchema, OracleProcedure, period), nil)
184184
if err := h.jobRepo.AddLog(ctx, logEntry); err != nil {
185185
h.logger.Warn().Err(err).Msg("Failed to add procedure log")
186186
}
187187

188188
start := time.Now()
189-
err := h.oracleRepo.ExecuteProcedure(ctx, OracleSchema, OracleProcedure)
189+
// Procedure now accepts p_period IN VARCHAR2 DEFAULT NULL ('YYYYMM'). We
190+
// pass the period ResolvePeriod() already computed so the Go-side job
191+
// period and the Oracle-side refreshed period can never diverge — the
192+
// procedure's own NULL/auto-period job-mode logic (day 1-5 => previous
193+
// month, day 6+ => current month) is left as a fallback for other callers.
194+
err := h.oracleRepo.ExecuteProcedureWithParam(ctx, OracleSchema, OracleProcedure, period)
190195
duration := time.Since(start)
191196

192197
if err != nil {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Revert chk_rm_cost_flag_valuation_used / chk_rm_cost_flag_marketing_used
2+
-- to the original V1-only whitelist.
3+
--
4+
-- WARNING: if any row was written with a V2 label (CR/SR/PR/CL/SL/FL or
5+
-- SP/PP/FP) while this migration was applied, this rollback will fail with
6+
-- SQLSTATE 23514 until those rows are deleted or corrected.
7+
8+
ALTER TABLE cst_rm_cost
9+
DROP CONSTRAINT IF EXISTS chk_rm_cost_flag_valuation_used;
10+
11+
ALTER TABLE cst_rm_cost
12+
ADD CONSTRAINT chk_rm_cost_flag_valuation_used
13+
CHECK (flag_valuation_used IN ('CONS','STORES','DEPT','PO_1','PO_2','PO_3','INIT'));
14+
15+
ALTER TABLE cst_rm_cost
16+
DROP CONSTRAINT IF EXISTS chk_rm_cost_flag_marketing_used;
17+
18+
ALTER TABLE cst_rm_cost
19+
ADD CONSTRAINT chk_rm_cost_flag_marketing_used
20+
CHECK (flag_marketing_used IN ('CONS','STORES','DEPT','PO_1','PO_2','PO_3','INIT'));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Migration: Widen chk_rm_cost_flag_valuation_used / chk_rm_cost_flag_marketing_used
2+
-- to accept the V2 cascade-resolved labels.
3+
--
4+
-- Context: ENG-RM-01/P3 (see calculate_handler_v2.go buildOrApplyCost) changed
5+
-- flag_valuation_used / flag_marketing_used to store the V2 engine's resolved
6+
-- cascade label (SelectValuationWithFlag / SelectMarketingWithFlag in
7+
-- calc_formulas_v2.go) instead of the V1 stage enum. The DB constraints from
8+
-- 000012_create_cst_rm_cost were never updated to match, so any V2 cascade
9+
-- resolving to a V2-only label (CR/SR/PR/CL/SL/FL for valuation,
10+
-- SP/PP/FP for marketing) violates the CHECK — this is what caused
11+
-- RM_COST_CA-* jobs chained after oracle-sync to fail with SQLSTATE 23514.
12+
--
13+
-- flag_simulation_used is untouched: buildOrApplyCost always sets it from
14+
-- head.FlagSimulation() (a V1 Stage), never from a V2 cascade selector.
15+
--
16+
-- Old V1 labels are kept in the allowed set for historical rows.
17+
18+
ALTER TABLE cst_rm_cost
19+
DROP CONSTRAINT IF EXISTS chk_rm_cost_flag_valuation_used;
20+
21+
ALTER TABLE cst_rm_cost
22+
ADD CONSTRAINT chk_rm_cost_flag_valuation_used
23+
CHECK (flag_valuation_used IN (
24+
'CONS','STORES','DEPT','PO_1','PO_2','PO_3','INIT',
25+
'CR','SR','PR','CL','SL','FL'
26+
));
27+
28+
ALTER TABLE cst_rm_cost
29+
DROP CONSTRAINT IF EXISTS chk_rm_cost_flag_marketing_used;
30+
31+
ALTER TABLE cst_rm_cost
32+
ADD CONSTRAINT chk_rm_cost_flag_marketing_used
33+
CHECK (flag_marketing_used IN (
34+
'CONS','STORES','DEPT','PO_1','PO_2','PO_3','INIT',
35+
'SP','PP','FP'
36+
));

0 commit comments

Comments
 (0)