From a66e24bfa2af7de8cacf4c4525f0eb5103aae0eb Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Mon, 5 Dec 2022 13:43:32 +0100 Subject: [PATCH] Ignore psqlrc when loading the rubygems database dump The SQL dump contains ownership instructions that can't be run if you don't have the right users in your database clusters. When someone has a psqlrc with ON_ERROR_STOP, this fails the load of the dump. Use the opportunity to trigger an exception when psql returns a non-zero exit code, rather than continue with an empty/inconsistent database. --- swh/lister/rubygems/lister.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/swh/lister/rubygems/lister.py b/swh/lister/rubygems/lister.py index 6898b8b..917a2d6 100644 --- a/swh/lister/rubygems/lister.py +++ b/swh/lister/rubygems/lister.py @@ -119,9 +119,13 @@ class RubyGemsLister(StatelessLister[RubyGemsListerPage]): dump_tar.extractall(temp_dir) logger.debug("Populating rubygems database with dump %s", dump_id) + + # FIXME: make this work with -v ON_ERROR_STOP=1 psql = subprocess.Popen( - ["psql", "-q", db_url], + ["psql", "--no-psqlrc", "-q", db_url], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, ) # passing value of gzip.open as stdin of subprocess.run makes the process @@ -133,6 +137,18 @@ class RubyGemsLister(StatelessLister[RubyGemsListerPage]): psql.stdin.close() # type: ignore psql.wait() + if psql.returncode != 0: + assert psql.stdout + for line in psql.stdout.readlines(): + logger.warning("psql out: %s", line.decode().strip()) + assert psql.stderr + for line in psql.stderr.readlines(): + logger.warning("psql err: %s", line.decode().strip()) + raise ValueError( + "Loading rubygems dump failed with exit code %s.", + psql.returncode, + ) + def get_pages(self) -> Iterator[RubyGemsListerPage]: # spawn a temporary postgres instance (require initdb executable in environment) with Postgresql() as postgresql: