Repository Public
1-- =============================================================================2-- SP BOUCHER | SOURCE FILE3-- =============================================================================4-- Project : KA Maps Engine5-- File : server/pipeline/geo/80_validate.sql6-- Author : Simon-Pierre Boucher | https://spboucher.ai7-- Contact : support@spboucher.ai8-- Copyright : (c) 2026 Simon-Pierre Boucher9-- License : None specified (no license file in this repository)10-- =============================================================================11-- END OF SP BOUCHER SOURCE HEADER12-- =============================================================================1314-- Seuils d'abandon avant bascule : chaque table à servir doit exister, ne pas être vide, et ne pas avoir perdu plus de15-- 3 % de ses lignes par rapport à la version servie (10 % pour les tables issues d'OSM, plus mouvantes).16-- Variable psql : tables. Sortie : une ligne par table ; lève une exception au premier dépassement.17\set ON_ERROR_STOP on18SELECT set_config('kamaps.tables', :'tables', false);19DO $$20DECLARE t text; n bigint; o bigint; floor_ratio numeric; minimum bigint;21BEGIN22 FOREACH t IN ARRAY string_to_array(current_setting('kamaps.tables'), ',') LOOP23 IF to_regclass('geo_next.' || quote_ident(t)) IS NULL THEN RAISE EXCEPTION 'VALIDATION : geo_next.% absente', t; END IF;24 EXECUTE format('SELECT count(*) FROM geo_next.%I', t) INTO n;25 o := NULL;26 IF to_regclass('geo.' || quote_ident(t)) IS NOT NULL THEN EXECUTE format('SELECT count(*) FROM geo.%I', t) INTO o; END IF;27 floor_ratio := CASE WHEN t IN ('place', 'amenity', 'osm_addr') THEN 0.90 ELSE 0.97 END;28 minimum := CASE t WHEN 'address' THEN 3000000 WHEN 'street' THEN 100000 WHEN 'street_seg' THEN 400000 WHEN 'mun' THEN 110029 WHEN 'boundary' THEN 1500 WHEN 'postcode' THEN 180000 WHEN 'place' THEN 9000 WHEN 'amenity' THEN 4000030 WHEN 'osm_addr' THEN 1000000 ELSE 1 END;31 RAISE NOTICE 'validation % : % lignes (servi : %)', t, n, coalesce(o::text, 'aucune');32 IF n < minimum THEN RAISE EXCEPTION 'VALIDATION : % n''a que % lignes (minimum %)', t, n, minimum; END IF;33 IF o IS NOT NULL AND n < o * floor_ratio THEN34 RAISE EXCEPTION 'VALIDATION : % passe de % à % lignes (seuil %)', t, o, n, floor_ratio;35 END IF;36 END LOOP;37END $$;38