Repository Public
1-- =============================================================================2-- SP BOUCHER | SOURCE FILE3-- =============================================================================4-- Project : KA Maps Engine5-- File : server/pipeline/geo/90_swap.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-- Bascule atomique : dans UNE transaction, chaque table servie passe de geo à geo_prev et sa remplaçante de geo_next à15-- geo. Les lecteurs voient l'ancien jeu complet ou le nouveau complet, jamais un mélange ni une table absente ; leurs16-- requêtes préparées sont replanifiées d'elles-mêmes (les tables sont nommées sans schéma, search_path = geo).17-- Variable psql : tables (liste séparée par des virgules), mode, version.18\set ON_ERROR_STOP on19BEGIN;20SET LOCAL lock_timeout = '15s';21SET LOCAL search_path = public;22SELECT set_config('kamaps.tables', :'tables', true);23DROP SCHEMA IF EXISTS geo_prev CASCADE;24CREATE SCHEMA geo_prev;25DO $$26DECLARE t text; list text[] := string_to_array(current_setting('kamaps.tables'), ',');27BEGIN28 -- Verrous pris d'un coup, dans un ordre fixe, avant tout déplacement29 FOREACH t IN ARRAY list LOOP30 IF to_regclass('geo.' || quote_ident(t)) IS NOT NULL THEN31 EXECUTE format('LOCK TABLE geo.%I IN ACCESS EXCLUSIVE MODE', t);32 END IF;33 END LOOP;34 FOREACH t IN ARRAY list LOOP35 IF to_regclass('geo_next.' || quote_ident(t)) IS NULL THEN36 RAISE EXCEPTION 'table geo_next.% absente : bascule annulée', t;37 END IF;38 IF to_regclass('geo.' || quote_ident(t)) IS NOT NULL THEN39 EXECUTE format('ALTER TABLE geo.%I SET SCHEMA geo_prev', t);40 END IF;41 EXECUTE format('ALTER TABLE geo_next.%I SET SCHEMA geo', t);42 END LOOP;43END $$;44CREATE TABLE IF NOT EXISTS geo.build_info (id bigserial PRIMARY KEY, built_at timestamptz NOT NULL DEFAULT now(),45 mode text NOT NULL, version text NOT NULL, tables text NOT NULL, note text);46INSERT INTO geo.build_info (mode, version, tables) VALUES (:'mode', :'version', :'tables');47GRANT USAGE ON SCHEMA geo TO km_geo_ro;48GRANT SELECT ON ALL TABLES IN SCHEMA geo TO km_geo_ro;49COMMIT;50