From 3ed89a59a8bfeaa85e7d47fc58cabd93c1552e81 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 3 Jul 2025 12:09:44 +0200 Subject: [PATCH] Added failure handling for bussy databases --- restore_postgres_databases.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/restore_postgres_databases.py b/restore_postgres_databases.py index 422c925..dcff2fb 100644 --- a/restore_postgres_databases.py +++ b/restore_postgres_databases.py @@ -44,14 +44,18 @@ def main(): sys.exit(1) for sqlfile in sql_files: - dbname = os.path.splitext(os.path.basename(sqlfile))[0] + # Extract database name by stripping the full suffix '.backup.sql' + filename = os.path.basename(sqlfile) + if not filename.endswith('.backup.sql'): + continue + dbname = filename[:-len('.backup.sql')] print(f"=== Processing {sqlfile} → database: {dbname} ===") - # Drop the database if it already exists + # Drop the database, forcing disconnect of sessions if necessary run_command([ "docker", "exec", "-i", container, "psql", "-U", "postgres", "-c", - f"DROP DATABASE IF EXISTS \"{dbname}\";" + f"DROP DATABASE IF EXISTS \"{dbname}\" WITH (FORCE);" ]) # Create a fresh database