|
| 1 | +-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 2 | +-- |
| 3 | +-- PostGIS - Spatial Types for PostgreSQL |
| 4 | +-- https://postgis.net |
| 5 | +-- |
| 6 | +-- Copyright (C) 2025 Regina Obe <lr@pcorp.us> |
| 7 | +-- |
| 8 | +-- This is free software; you can redistribute and/or modify it under |
| 9 | +-- the terms of the GNU General Public License. See the COPYING file. |
| 10 | +-- |
| 11 | +-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 12 | +--{ |
| 13 | +-- FixCorruptTopGeometryColumn((layerSchema name, layerTable name, layerColumn name) |
| 14 | +-- |
| 15 | +-- Needed to fix corruption of topogeometries caused by upgrade from < 3.6.0 to 3.6.0 and higher |
| 16 | +-- |
| 17 | +-- Availability: 3.6.1 |
| 18 | +CREATE OR REPLACE FUNCTION topology.FixCorruptTopoGeometryColumn(layerSchema name, layerTable name, layerColumn name) |
| 19 | +RETURNS text AS |
| 20 | +$$ |
| 21 | +DECLARE var_sql text; var_row_count bigint; result text; |
| 22 | +BEGIN |
| 23 | + -- if topogeometry is bigint, then fix damaged integer, need to upgrade to bigint |
| 24 | + IF EXISTS ( SELECT 1 |
| 25 | + FROM pg_catalog.pg_type AS pg_type |
| 26 | + JOIN pg_catalog.pg_class AS pg_class ON pg_class.oid = pg_type.typrelid |
| 27 | + JOIN pg_catalog.pg_attribute AS pga ON pga.attrelid = pg_class.oid |
| 28 | + JOIN pg_catalog.pg_type AS pg_attr_type on pg_attr_type.oid = pga.atttypid |
| 29 | +WHERE pg_type.typname::regtype::text = 'topogeometry' AND pga.attname = 'id' |
| 30 | + AND |
| 31 | + pg_type.typnamespace::regnamespace::text = 'topology' AND pga.atttypid::regtype::text = 'bigint' ) THEN |
| 32 | + var_sql = format('UPDATE %1$I.%2$I |
| 33 | + SET |
| 34 | + %3$I = ( |
| 35 | + (%3$I).topology_id, |
| 36 | + (%3$I).layer_id, |
| 37 | + (%3$I).id & 0xFFFFFFFF, |
| 38 | + (%3$I).id >> 32 |
| 39 | + )::topology.topogeometry |
| 40 | + WHERE ( (%3$I).id & 0xFFFFFFFF ) <> (%3$I).id OR ( (%3$I).id >> 32 ) = (%3$I).type ', layerSchema, layerTable, layerColumn); |
| 41 | + |
| 42 | + EXECUTE var_sql; |
| 43 | + GET DIAGNOSTICS var_row_count = ROW_COUNT; |
| 44 | + result = format('%s rows updated for %s.%s.%s column to bigint id type', var_row_count, layerSchema, layerTable, layerColumn); |
| 45 | + ELSE --we are coming from bigint and going back to integer |
| 46 | + var_sql = format('UPDATE %1$I.%2$I |
| 47 | + SET |
| 48 | + %3$I = ( |
| 49 | + (%3$I).topology_id, |
| 50 | + (%3$I).layer_id, |
| 51 | + (%3$I).id, |
| 52 | + l.feature_type |
| 53 | + )::topogeometry |
| 54 | + FROM topology.layer AS l |
| 55 | + WHERE l.topology_id = (%3$I).topology_id AND l.layer_id = (%3$I).layer_id AND (%3$I).type <> l.feature_type ', topo_schema, topo_table, topo_column); |
| 56 | + EXECUTE var_sql; |
| 57 | + GET DIAGNOSTICS var_row_count = ROW_COUNT; |
| 58 | + result = format('%s rows updated for %s.%s.%s column back to integer id type', var_row_count, topo_schema, topo_table, topo_column); |
| 59 | + END IF; |
| 60 | + RETURN result; |
| 61 | +END |
| 62 | +$$ language plpgsql; |
| 63 | +--} |
0 commit comments