Skip to content

Commit

Permalink
Merge pull request osm2pgsql-dev#2260 from lonvia/properly-propagate-…
Browse files Browse the repository at this point in the history
…schema-on-update

Properly forward schema parameters from replication script
  • Loading branch information
joto authored Oct 1, 2024
2 parents 7609e0e + 9b16c43 commit 7d7a970
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
14 changes: 8 additions & 6 deletions scripts/osm2pgsql-replication
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class DBError(Exception):

class DBConnection:

def __init__(self, args):
self.schema = args.middle_schema
def __init__(self, schema, args):
self.schema = schema

# If dbname looks like a conninfo string use it as such
if args.database and any(part in args.database for part in ['=', '://']):
Expand Down Expand Up @@ -535,8 +535,10 @@ def update(props, args):

osm2pgsql = [args.osm2pgsql_cmd, '--append', '--slim', '--prefix', args.prefix]
osm2pgsql.extend(args.extra_params)
if args.middle_schema != 'public':
if args.middle_schema:
osm2pgsql.extend(('--middle-schema', args.middle_schema))
if args.schema:
osm2pgsql.extend(('--schema', args.schema))
if args.database:
osm2pgsql.extend(('-d', args.database))
if args.username:
Expand Down Expand Up @@ -717,16 +719,16 @@ def main(prog_args=None):
datefmt='%Y-%m-%d %H:%M:%S',
level=max(4 - args.verbose, 1) * 10)

args.middle_schema = args.middle_schema or args.schema or 'public'
prop_table_schema = args.middle_schema or args.schema or 'public'

with DBConnection(args) as db:
with DBConnection(prop_table_schema, args) as db:
if db.table_exists(Osm2pgsqlProperties.PROP_TABLE_NAME):
props = Osm2pgsqlProperties(db)
else:
props = LegacyProperties(db, args.prefix)

if not props.is_updatable:
LOG.fatal(f'osm2pgsql middle table "{args.middle_schema}.{args.prefix}_ways" not found in database "{db.name}". '
LOG.fatal(f'osm2pgsql middle table "{prop_table_schema}.{args.prefix}_ways" not found in database "{db.name}". '
'Database needs to be imported in --slim mode.')
return 1

Expand Down
68 changes: 68 additions & 0 deletions tests/bdd/command-line/replication_schema.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Feature: Tests for the osm2pgsql-replication script with schemas

Scenario: Replication updates work on database with schema
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the database schema foobar
And the replication service at http://example.com/europe/liechtenstein-updates
| sequence | timestamp |
| 9999999 | 2013-08-01T01:00:02Z |
| 10000000 | 2013-09-01T01:00:00Z |
| 10000001 | 2013-10-01T01:00:00Z |
When running osm2pgsql pgsql with parameters
| --slim | --schema | foobar |
And running osm2pgsql-replication
| init | --schema | foobar |
And running osm2pgsql-replication
| update | --schema | foobar |

Then table foobar.osm2pgsql_properties contains
| property | value |
| replication_base_url | http://example.com/europe/liechtenstein-updates |
| replication_sequence_number | 10000001 |
| replication_timestamp | 2013-10-01T01:00:00Z |


Scenario: Replication updates work on database with different middle schema
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the database schema foobar
And the replication service at http://example.com/europe/liechtenstein-updates
| sequence | timestamp |
| 9999999 | 2013-08-01T01:00:02Z |
| 10000000 | 2013-09-01T01:00:00Z |
| 10000001 | 2013-10-01T01:00:00Z |
When running osm2pgsql pgsql with parameters
| --slim | --middle-schema | foobar |
And running osm2pgsql-replication
| init | --middle-schema | foobar |
And running osm2pgsql-replication
| update | --middle-schema | foobar |

Then table foobar.osm2pgsql_properties contains
| property | value |
| replication_base_url | http://example.com/europe/liechtenstein-updates |
| replication_sequence_number | 10000001 |
| replication_timestamp | 2013-10-01T01:00:00Z |


Scenario: Replication updates work on database with middle schema different from schema
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the database schema foobar
And the database schema baz
And the replication service at http://example.com/europe/liechtenstein-updates
| sequence | timestamp |
| 9999999 | 2013-08-01T01:00:02Z |
| 10000000 | 2013-09-01T01:00:00Z |
| 10000001 | 2013-10-01T01:00:00Z |
When running osm2pgsql pgsql with parameters
| --slim | --middle-schema | foobar | --schema | baz |
And running osm2pgsql-replication
| init | --middle-schema | foobar | --schema | baz |
And running osm2pgsql-replication
| update | --middle-schema | foobar | --schema | baz |

Then table foobar.osm2pgsql_properties contains
| property | value |
| replication_base_url | http://example.com/europe/liechtenstein-updates |
| replication_sequence_number | 10000001 |
| replication_timestamp | 2013-10-01T01:00:00Z |

0 comments on commit 7d7a970

Please sign in to comment.