Skip to content

Commit 8760df7

Browse files
committed
osm2pgsql-replication: meaningful error when middle tables do not exist
... or the prefix is a bad one. There is no way to distinguish that.
1 parent 49f1868 commit 8760df7

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

scripts/osm2pgsql-replication

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def connect(args):
8585
host=args.host, port=args.port)
8686

8787

88+
def table_exists(conn, table_name):
89+
with conn.cursor() as cur:
90+
cur.execute('SELECT * FROM pg_tables where tablename = %s', (table_name, ))
91+
return cur.rowcount > 0
92+
93+
8894
def compute_database_date(conn, prefix):
8995
""" Determine the date of the database from the newest object in the
9096
database.
@@ -199,12 +205,11 @@ def status(conn, args):
199205

200206
results = {}
201207

202-
with conn.cursor() as cur:
203-
cur.execute('SELECT * FROM pg_tables where tablename = %s', (args.table_name, ))
204-
if cur.rowcount < 1:
205-
results['status'] = 1
206-
results['error'] = "Cannot find replication status table. Run 'osm2pgsql-replication init' first."
207-
else:
208+
if not table_exists(conn, args.table_name):
209+
results['status'] = 1
210+
results['error'] = "Cannot find replication status table. Run 'osm2pgsql-replication init' first."
211+
else:
212+
with conn.cursor() as cur:
208213
cur.execute(sql.SQL('SELECT * FROM {}').format(args.table))
209214
if cur.rowcount != 1:
210215
results['status'] = 2
@@ -344,13 +349,12 @@ def update(conn, args):
344349
may be missing in the rare case that the replication service stops responding
345350
after the updates have been downloaded.
346351
"""
347-
with conn.cursor() as cur:
348-
cur.execute('SELECT * FROM pg_tables where tablename = %s', (args.table_name, ))
349-
if cur.rowcount < 1:
350-
LOG.fatal("Cannot find replication status table. "
351-
"Run 'osm2pgsql-replication init' first.")
352-
return 1
352+
if not table_exists(conn, args.table_name):
353+
LOG.fatal("Cannot find replication status table. "
354+
"Run 'osm2pgsql-replication init' first.")
355+
return 1
353356

357+
with conn.cursor() as cur:
354358
cur.execute(sql.SQL('SELECT * FROM {}').format(args.table))
355359
if cur.rowcount != 1:
356360
LOG.fatal("Updates not set up correctly. Run 'osm2pgsql-updates init' first.")
@@ -525,10 +529,16 @@ def main():
525529
args.table = sql.Identifier(args.table_name)
526530

527531
conn = connect(args)
528-
ret = args.handler(conn, args)
529-
conn.close()
530532

531-
return ret
533+
try:
534+
if not table_exists(conn, f'{args.prefix}_ways'):
535+
LOG.fatal(f'osm2pgsql middle table "{args.prefix}_ways" not found in database "{args.database}". '
536+
'Database needs to be imported in --slim mode.')
537+
return 1
538+
539+
return args.handler(conn, args)
540+
finally:
541+
conn.close()
532542

533543

534544
if __name__ == '__main__':

0 commit comments

Comments
 (0)