Skip to content

Commit

Permalink
postgis: fix OSM id index for generalized tables
Browse files Browse the repository at this point in the history
  • Loading branch information
olt committed Jun 13, 2019
1 parent 5b0125c commit 576c4b7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
9 changes: 5 additions & 4 deletions database/postgis/postgis.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ 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)
}
}

for _, tbl := range pg.GeneralizedTables {
tableName := tbl.FullName
table := tbl
p.in <- func() error {
return createIndex(pg, tableName, table.Source.Columns)
return createIndex(pg, tableName, table.Source.Columns, true)
}
}

Expand All @@ -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" {
Expand All @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions test/completedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
18 changes: 17 additions & 1 deletion test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 576c4b7

Please sign in to comment.