Used streaming instead of memory to prevent overflow

This commit is contained in:
2025-07-03 14:37:36 +02:00
parent 3ed89a59a8
commit 18d6136de0

View File

@@ -11,13 +11,13 @@ import sys
import os
import glob
def run_command(cmd, input_data=None):
def run_command(cmd, stdin=None):
"""
Run a subprocess command and abort immediately on any failure.
:param cmd: list of command parts
:param input_data: bytes to send to process stdin
:param stdin: file-like object to use as stdin
"""
subprocess.run(cmd, input=input_data, check=True)
subprocess.run(cmd, stdin=stdin, check=True)
def main():
@@ -79,14 +79,13 @@ def main():
)
])
# Restore the dump into the database (will abort on first error)
print(f"Restoring dump into {dbname}")
with open(sqlfile, "rb") as f:
sql_data = f.read()
run_command([
"docker", "exec", "-i", container,
"psql", "-U", "postgres", "-d", dbname
], input_data=sql_data)
# Restore the dump into the database by streaming file (will abort on first error)
print(f"Restoring dump into {dbname} (this may take a while)")
with open(sqlfile, 'rb') as infile:
run_command([
"docker", "exec", "-i", container,
"psql", "-U", "postgres", "-d", dbname
], stdin=infile)
print(f"{dbname} restored.")