Skip to content

Commit 26639a2

Browse files
committed
WIP: fix topogeometry column function addition
1 parent 4748b53 commit 26639a2

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

topology/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ topology.sql: \
141141
sql/manage/CreateTopology.sql.in \
142142
sql/manage/FindLayer.sql.in \
143143
sql/manage/FindTopology.sql.in \
144+
sql/manage/FixCorruptTopoGeometryColumn.sql.in \
144145
sql/manage/ManageHelper.sql.in \
145146
sql/manage/MakeTopologyPrecise.sql.in \
146147
sql/manage/TotalTopologySize.sql.in \
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
--}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- See https://trac.osgeo.org/postgis/ticket/5102
22
SELECT topology.CopyTopology('upgrade_test', 'upgrade_test_copy');
33

4+
-- check if corruption
5+
select geometrytype(tg) from upgrade_test.feature limit 2;
6+
47
SELECT topology.DropTopology('upgrade_test');
58
SELECT topology.DropTopology('upgrade_test_copy');
69

topology/topology.sql.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,7 @@ LANGUAGE 'plpgsql' VOLATILE STRICT;
14171417
#include "sql/manage/CopyTopology.sql.in"
14181418
#include "sql/manage/FindTopology.sql.in"
14191419
#include "sql/manage/FindLayer.sql.in"
1420+
#include "sql/manage/FixCorruptTopoGeometryColumn.sql.in"
14201421
#include "sql/manage/populate_topology_layer.sql.in"
14211422
#include "sql/manage/RenameTopology.sql.in"
14221423
#include "sql/manage/ValidateTopology.sql.in"

0 commit comments

Comments
 (0)