Skip to content

Commit ddf176c

Browse files
author
a.b.christie
committed
fix: Fix "current user cannot be dropped" on recovery
1 parent 53a1a45 commit ddf176c

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

sql-recovery/recovery.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
DATABASE_EXPECTED_COUNT = os.environ.get('DATABASE_EXPECTED_COUNT', '')
174174
# A specific database?
175175
DATABASE = os.environ.get('DATABASE', '')
176+
RECOVERY_DATABASE_ROOT_USER = os.environ.get('RECOVERY_DATABASE_ROOT_USER', 'postgres')
176177
# Extract configuration from the environment.
177178
# Postgres material...
178179
PGHOST = os.environ.get('PGHOST', '')
@@ -544,6 +545,18 @@ def error(error_no):
544545
write_termination_message('Unpack failed')
545546
sys.exit(0)
546547

548+
# Filter - Remove actions on the root (postgres) user, which will otherwise
549+
# cause the recovery to fail with "current user cannot be dropped".
550+
# We only need to do this if the recovery is from a dumpall.
551+
if DATABASE:
552+
TARGET_FILE = "%s/%s.sql" % (RECOVERY_ROOT_DIR, DECOMPRESSED_RECOVERY_NAME)
553+
FILTER_PG_CMD = "egrep -v '^(CREATE|DROP) ROLE %s;' %s > filtered.sql ; mv filtered.sql %s" % (RECOVERY_DATABASE_ROOT_USER, TARGET_FILE, TARGET_FILE)
554+
print(" $", FILTER_PG_CMD)
555+
COMPLETED_PROCESS = subprocess.run(FILTER_PG_CMD,
556+
shell=True,
557+
stderr=subprocess.PIPE,
558+
check=False)
559+
547560
print(" $", RECOVERY_CMD)
548561
COMPLETED_PROCESS = subprocess.run(RECOVERY_CMD,
549562
shell=True,

0 commit comments

Comments
 (0)