From 576c4b745d03e77294b80b0545acd42f2337a469 Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Thu, 13 Jun 2019 21:28:44 +0200 Subject: [PATCH] postgis: fix OSM id index for generalized tables --- database/postgis/postgis.go | 9 +++++---- test/completedb_test.go | 16 ++++++++++++++++ test/helper_test.go | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/database/postgis/postgis.go b/database/postgis/postgis.go index 6b9bf666..ea21db03 100644 --- a/database/postgis/postgis.go +++ b/database/postgis/postgis.go @@ -175,7 +175,7 @@ func (pg *PostGIS) Finish() error { tableName := tbl.FullName table := tbl p.in <- func() error { - return createIndex(pg, tableName, table.Columns) + return createIndex(pg, tableName, table.Columns, false) } } @@ -183,7 +183,7 @@ func (pg *PostGIS) Finish() error { tableName := tbl.FullName table := tbl p.in <- func() error { - return createIndex(pg, tableName, table.Source.Columns) + return createIndex(pg, tableName, table.Source.Columns, true) } } @@ -195,7 +195,7 @@ func (pg *PostGIS) Finish() error { return nil } -func createIndex(pg *PostGIS, tableName string, columns []ColumnSpec) error { +func createIndex(pg *PostGIS, tableName string, columns []ColumnSpec, generalizedTable bool) error { foundIDCol := false for _, cs := range columns { if cs.Name == "id" { @@ -214,11 +214,12 @@ func createIndex(pg *PostGIS, tableName string, columns []ColumnSpec) error { return err } } - if col.FieldType.Name == "id" && foundIDCol { + if col.FieldType.Name == "id" && (foundIDCol || generalizedTable) { // Create index for OSM ID required for diff updates, but only if // the table does have an `id` column. // The explicit `id` column prevented the creation of our composite // PRIMARY KEY index of id (serial) and OSM ID. + // Generalized tables also do not have a PRIMARY KEY. sql := fmt.Sprintf(`CREATE INDEX "%s_%s_idx" ON "%s"."%s" USING BTREE ("%s")`, tableName, col.Name, pg.Config.ImportSchema, tableName, col.Name) step := log.Step(fmt.Sprintf("Creating OSM id index on %s", tableName)) diff --git a/test/completedb_test.go b/test/completedb_test.go index 0ea35ea1..1c3b7e27 100644 --- a/test/completedb_test.go +++ b/test/completedb_test.go @@ -69,6 +69,22 @@ func TestComplete(t *testing.T) { } }) + t.Run("CheckIndices", func(t *testing.T) { + if !ts.indexExists(t, ts.dbschemaProduction(), "osm_roads", "osm_roads_pkey") { + t.Fatal("osm_id idx missing for osm_roads") + } + if !ts.indexExists(t, ts.dbschemaProduction(), "osm_roads", "osm_roads_geom") { + t.Fatal("geom idx missing for osm_roads") + } + if !ts.indexExists(t, ts.dbschemaProduction(), "osm_landusages_gen0", "osm_landusages_gen0_osm_id_idx") { + t.Fatal("osm_id idx missing for osm_landusages_gen0") + } + if !ts.indexExists(t, ts.dbschemaProduction(), "osm_landusages_gen0", "osm_landusages_gen0_geom") { + t.Fatal("geom idx missing for osm_landusages_gen0") + } + + }) + t.Run("OnlyNewStyleMultipolgon", func(t *testing.T) { ts.assertRecords(t, []checkElem{ {"osm_landusages", -1001, "wood", nil}, diff --git a/test/helper_test.go b/test/helper_test.go index e731d6da..6eafb4f5 100644 --- a/test/helper_test.go +++ b/test/helper_test.go @@ -177,7 +177,23 @@ func (ts *importTestSuite) dropSchemas() { } func (ts *importTestSuite) tableExists(t *testing.T, schema, table string) bool { - row := ts.db.QueryRow(fmt.Sprintf(`SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name='%s' AND table_schema='%s')`, table, schema)) + row := ts.db.QueryRow( + `SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name=$1 AND table_schema=$2)`, + table, schema, + ) + var exists bool + if err := row.Scan(&exists); err != nil { + t.Error(err) + return false + } + return exists +} + +func (ts *importTestSuite) indexExists(t *testing.T, schema, table, index string) bool { + row := ts.db.QueryRow( + `SELECT EXISTS(SELECT * FROM pg_indexes WHERE tablename=$1 AND schemaname=$2 AND indexname like $3)`, + table, schema, index, + ) var exists bool if err := row.Scan(&exists); err != nil { t.Error(err)