Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid hidden failures when dumping #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pgclone/dump_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def _dump(*, exclude, config, pre_dump_hooks, instance, database, storage_locati
[f"--exclude-table-data={table_name}" for table_name in exclude_tables]
)
# Note - do note format {db_dump_url} with an `f` string.
# It will be formatted later when running the command
# It will be formatted later when running the command.
pg_dump_cmd_fmt = "pg_dump -Fc --no-acl --no-owner {db_dump_url} " + exclude_args
pg_dump_cmd_fmt += " " + storage_client.pg_dump(file_path)

anon_pg_dump_cmd = pg_dump_cmd_fmt.format(db_dump_url="<DB_URL>")
logging.success_msg(f"Creating DB copy with cmd: {anon_pg_dump_cmd}")

pg_dump_cmd = pg_dump_cmd_fmt.format(db_dump_url=db.url(dump_db))
run.shell(pg_dump_cmd, env=storage_client.env)
run.shell(pg_dump_cmd, env=storage_client.env, pipefail=True)

logging.success_msg(f'Database "{database}" successfully dumped to "{dump_key}"')

Expand Down
19 changes: 18 additions & 1 deletion pgclone/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@
import io
import os
import subprocess
import sys

from django.core.management import call_command

from pgclone import exceptions, logging


def shell(cmd, ignore_errors=False, env=None):
def _is_pipefail_supported() -> bool:
"""Check if the current shell supports pipefail."""
if sys.platform == "win32":
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may need a pragma: no cover here for the coverage check


try:
current_shell = os.environ.get("SHELL", "/bin/sh")
subprocess.check_call([current_shell, "-c", "set -o pipefail"], stderr=subprocess.DEVNULL)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False


def shell(cmd, ignore_errors=False, env=None, pipefail=False):
"""
Utility for running a command. Ensures that an error
is raised if it fails.
"""
if pipefail and _is_pipefail_supported(): # pragma: no cover
cmd = [f"set -o pipefail; {cmd}"]

env = env or {}
logger = logging.get_logger()
process = subprocess.Popen(
Expand Down
16 changes: 16 additions & 0 deletions pgclone/tests/test_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess
from unittest.mock import patch

from pgclone import run


def test_is_pipefail_supported():
with patch("subprocess.check_call", autospec=True, return_value=0):
assert run._is_pipefail_supported() is True

with patch(
"subprocess.check_call",
side_effect=subprocess.CalledProcessError(1, "cmd"),
autospec=True,
):
assert run._is_pipefail_supported() is False