Used streaming instead of memory to prevent overflow

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-03 14:37:36 +02:00
parent 3ed89a59a8
commit 18d6136de0
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E

View File

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