You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db: Restore original functions in legal_relationship down migration
The up migration modified two shared functions but the down migration
didn't restore them. Now restores the original bodies of
import.create_source_and_mappings_for_definition() and
admin.validate_import_definition().
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: migrations/20260218215337_add_legal_relationship_import.down.sql
+349Lines changed: 349 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -18,4 +18,353 @@ DROP PROCEDURE IF EXISTS import.analyse_legal_relationship(INT, INTEGER, TEXT);
18
18
-- Note: Cannot remove enum value 'legal_relationship' from import_mode in PostgreSQL.
19
19
-- The enum value will remain but be unused.
20
20
21
+
-- Restore import.create_source_and_mappings_for_definition() to original version
22
+
-- (removes the v_has_stat_step guard — original always maps statistical variables)
23
+
CREATE OR REPLACEFUNCTIONimport.create_source_and_mappings_for_definition(
24
+
p_definition_id INT,
25
+
p_source_columns TEXT[] -- Array of source column names expected in the file
26
+
) RETURNS void LANGUAGE plpgsql AS $$
27
+
DECLARE
28
+
v_def public.import_definition;
29
+
v_col_name TEXT;
30
+
v_priority INT :=0;
31
+
v_source_col_id INT;
32
+
v_data_col_id INT;
33
+
v_max_priority INT;
34
+
v_col_rec RECORD;
35
+
BEGIN
36
+
SELECT* INTO v_def FROMpublic.import_definitionWHERE id = p_definition_id;
37
+
38
+
-- Handle validity date mappings based on definition mode
39
+
IF v_def.valid_time_from='job_provided' THEN
40
+
FOR v_col_name INVALUES ('valid_from'), ('valid_to') LOOP
41
+
SELECTdc.id INTO v_data_col_id FROMpublic.import_data_column dc JOINpublic.import_step s ONdc.step_id=s.idWHEREs.code='valid_time'ANDdc.column_name= v_col_name ||'_raw';
ON CONFLICT (definition_id, source_column_id, target_data_column_id) DO NOTHING;
97
+
ELSE
98
+
RAISE EXCEPTION '[Definition %] No matching source_input data column found in "statistical_variables" step for dynamically added stat source column "%".', p_definition_id, v_col_rec.stat_code;
99
+
END IF;
100
+
END LOOP;
101
+
END;
102
+
$$;
103
+
104
+
-- Restore admin.validate_import_definition() to original version
105
+
-- (removes legal_relationship mode branch and conditional external_idents check)
106
+
CREATE OR REPLACEFUNCTIONadmin.validate_import_definition(p_definition_id INT)
107
+
RETURNS void LANGUAGE plpgsql AS $validate_import_definition$
108
+
DECLARE
109
+
v_definition public.import_definition;
110
+
v_error_messages TEXT[] := ARRAY[]::TEXT[];
111
+
v_is_valid BOOLEAN := true;
112
+
v_step_codes TEXT[];
113
+
v_has_time_from_context_step BOOLEAN;
114
+
v_has_time_from_source_step BOOLEAN;
115
+
v_has_valid_from_mapping BOOLEAN := false;
116
+
v_has_valid_to_mapping BOOLEAN := false;
117
+
v_source_col_rec RECORD;
118
+
v_mapping_rec RECORD;
119
+
v_temp_text TEXT;
120
+
BEGIN
121
+
SELECT* INTO v_definition FROMpublic.import_definitionWHERE id = p_definition_id;
122
+
IF NOT FOUND THEN
123
+
RAISE DEBUG 'validate_import_definition: Definition ID % not found. Skipping validation.', p_definition_id;
124
+
RETURN;
125
+
END IF;
126
+
127
+
-- 1. Time Validity Method Check
128
+
-- All definitions must include the 'valid_time' step to ensure uniform processing.
129
+
IF NOT EXISTS (SELECT1FROMpublic.import_definition_step ids JOINpublic.import_step s ONs.id=ids.step_idWHEREids.definition_id= p_definition_id ANDs.code='valid_time') THEN
130
+
v_is_valid := false;
131
+
v_error_messages := array_append(v_error_messages, 'All import definitions must include the "valid_time" step.');
132
+
END IF;
133
+
134
+
-- The following checks ensure the mappings for 'valid_from' and 'valid_to' are consistent with the chosen time validity mode.
135
+
IF v_definition.valid_time_from='source_columns' THEN
136
+
-- Check that 'valid_from_raw' and 'valid_to_raw' are mapped from source columns.
137
+
SELECT EXISTS (
138
+
SELECT1FROMpublic.import_mapping im
139
+
JOINpublic.import_data_column idc ONim.target_data_column_id=idc.idJOINpublic.import_step s ONidc.step_id=s.id
140
+
WHEREim.definition_id= p_definition_id ANDs.code='valid_time'ANDidc.column_name='valid_from_raw'ANDim.source_column_idIS NOT NULLANDim.is_ignored= FALSE
141
+
) INTO v_has_valid_from_mapping;
142
+
SELECT EXISTS (
143
+
SELECT1FROMpublic.import_mapping im
144
+
JOINpublic.import_data_column idc ONim.target_data_column_id=idc.idJOINpublic.import_step s ONidc.step_id=s.id
145
+
WHEREim.definition_id= p_definition_id ANDs.code='valid_time'ANDidc.column_name='valid_to_raw'ANDim.source_column_idIS NOT NULLANDim.is_ignored= FALSE
146
+
) INTO v_has_valid_to_mapping;
147
+
148
+
IF NOT (v_has_valid_from_mapping AND v_has_valid_to_mapping) THEN
149
+
v_is_valid := false;
150
+
v_error_messages := array_append(v_error_messages, 'When valid_time_from="source_columns", mappings for both "valid_from_raw" and "valid_to_raw" from source columns are required.');
151
+
END IF;
152
+
153
+
ELSIF v_definition.valid_time_from='job_provided' THEN
154
+
-- If validity is derived from job-level parameters, the definition must map 'valid_from_raw'
155
+
-- and 'valid_to_raw' to the 'default' source expression. This allows the `import_job_prepare`
156
+
-- function to populate these columns from the job's `default_valid_from`/`to` fields.
157
+
SELECT EXISTS (
158
+
SELECT1FROMpublic.import_mapping im
159
+
JOINpublic.import_data_column idc ONim.target_data_column_id=idc.idJOINpublic.import_step s ONidc.step_id=s.id
IF NOT (v_has_valid_from_mapping AND v_has_valid_to_mapping) THEN
169
+
v_is_valid := false;
170
+
v_error_messages := array_append(v_error_messages, 'When valid_time_from="job_provided", mappings for both "valid_from_raw" and "valid_to_raw" using source_expression="default" are required.');
171
+
END IF;
172
+
173
+
ELSE
174
+
v_is_valid := false;
175
+
v_error_messages := array_append(v_error_messages, 'valid_time_from is NULL or has an unhandled value.');
v_error_messages := array_append(v_error_messages, 'At least one external identifier column (e.g., tax_ident, stat_ident) must be mapped for the "external_idents" step.');
274
+
END IF;
275
+
END;
276
+
END IF;
277
+
278
+
-- Specific check for 'status' step removed, as status_code mapping is now optional.
279
+
-- The analyse_status procedure will handle defaults, and analyse_legal_unit/_establishment
280
+
-- will error if status_id is ultimately not resolved.
281
+
282
+
-- Conditional check for 'data_source_code_raw' mapping:
283
+
-- If import_definition.data_source_id is NULL, a mapping for 'data_source_code_raw' is required.
284
+
IF v_definition.data_source_id IS NULL THEN
285
+
DECLARE
286
+
v_data_source_code_mapped BOOLEAN;
287
+
v_data_source_code_data_column_exists BOOLEAN;
288
+
BEGIN
289
+
-- Check if a data_source_code_raw data column even exists for any of the definition's steps
v_error_messages := array_append(v_error_messages, 'If import_definition.data_source_id is NULL and a "data_source_code_raw" source_input data column is available for the definition''s steps, it must be mapped.');
313
+
END IF;
314
+
ELSE
315
+
-- If data_source_id is NULL and no data_source_code_raw data column is available from steps, it's an error.
316
+
v_is_valid := false;
317
+
v_error_messages := array_append(v_error_messages, 'If import_definition.data_source_id is NULL, a "data_source_code_raw" source_input data column must be available via one of the definition''s steps and mapped. None found.');
318
+
END IF;
319
+
END;
320
+
END IF;
321
+
322
+
-- The old generic loop checking all source_input columns for mapping is removed.
323
+
-- Only specific, critical mappings are checked above.
v_error_messages := array_append(v_error_messages, format('Unused import_source_column: "%s". It is defined but not used in any mapping.', v_source_col_rec.column_name));
v_error_messages := array_append(v_error_messages, format('Mapping ID %s targets data column "%s" in step "%s", but this step is not part of the definition.', v_mapping_rec.mapping_id, v_mapping_rec.target_col_name, v_mapping_rec.target_step_code));
352
+
END LOOP;
353
+
354
+
-- Final Update
355
+
IF v_is_valid THEN
356
+
UPDATEpublic.import_definition
357
+
SET valid = true, validation_error =NULL
358
+
WHERE id = p_definition_id;
359
+
ELSE
360
+
-- Concatenate unique error messages
361
+
SELECT string_agg(DISTINCT error_msg, '; ') INTO v_temp_text FROM unnest(v_error_messages) AS error_msg;
0 commit comments