bmc_hub/migrations/205_realign_mismatched_ip_ranges.sql
Christian 3311b8e590 Add comprehensive tests for internet connections module, invoice parsing, and subscription provisioning
- Implement tests for the internet connections module, covering routes, IP range creation, and connection validation.
- Add tests for the Invoice2DataService to validate extraction from GlobalConnect invoices.
- Create tests for subscription network provisioning, ensuring proper handling of network items and IP allocations.
- Include validation checks for subtotal mismatches and ensure error handling for missing IP selections.
2026-07-09 23:44:30 +02:00

36 lines
1.3 KiB
SQL

WITH mismatched AS (
SELECT
ir.id AS range_id,
ir.service_address
FROM internet_connections_ip_ranges ir
JOIN internet_connections_connections current_conn
ON current_conn.id = ir.connection_id
WHERE ir.deleted_at IS NULL
AND current_conn.deleted_at IS NULL
AND current_conn.allocation_model <> 'shared'
AND ir.service_address IS NOT NULL
AND regexp_replace(upper(coalesce(ir.service_address, '')), '[^A-Z0-9]', '', 'g')
<> regexp_replace(upper(coalesce(current_conn.address, '')), '[^A-Z0-9]', '', 'g')
),
candidate_matches AS (
SELECT
m.range_id,
candidate.id AS target_connection_id,
COUNT(*) OVER (PARTITION BY m.range_id) AS candidate_count
FROM mismatched m
JOIN internet_connections_connections candidate
ON candidate.deleted_at IS NULL
AND regexp_replace(upper(coalesce(candidate.address, '')), '[^A-Z0-9]', '', 'g')
= regexp_replace(upper(coalesce(m.service_address, '')), '[^A-Z0-9]', '', 'g')
),
unique_targets AS (
SELECT range_id, target_connection_id
FROM candidate_matches
WHERE candidate_count = 1
)
UPDATE internet_connections_ip_ranges ir
SET connection_id = ut.target_connection_id,
updated_at = CURRENT_TIMESTAMP
FROM unique_targets ut
WHERE ir.id = ut.range_id;