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

exclude analytic tables from copy #134

Merged
merged 4 commits into from
Jan 16, 2025
Merged
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
5 changes: 3 additions & 2 deletions src/d2_docker/commands/run_sql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import subprocess
from d2_docker import utils
from d2_docker.utils import get_pg_dump_command

DESCRIPTION = "Run SQL or open interactive session in a d2-docker container"
NAME = "run-sql"
Expand Down Expand Up @@ -29,7 +30,7 @@ def run(args):
utils.logger.debug("DB container: {}".format(db_container))

if args.dump:
cmd = ["docker", "exec", db_container, "pg_dump", "-U", "dhis", "dhis2"]
cmd = ["docker", "exec", db_container, *get_pg_dump_command(compress=False)]
utils.logger.info("Dump SQL for image {}".format(image_name))
utils.run(cmd, raise_on_error=False)
else:
Expand All @@ -54,7 +55,7 @@ def get_stream_db(image):
raise utils.D2DockerError("Container must be running to dump database")

db_container = status["containers"]["db"]
cmd_parts = ["docker", "exec", db_container, "pg_dump", "-U", "dhis", "dhis2", "|", "gzip"]
cmd_parts = ["docker", "exec", db_container, *get_pg_dump_command()]
cmd = subprocess.list2cmdline(cmd_parts)
utils.logger.info("Dump SQL for image: {}".format(cmd))

Expand Down
14 changes: 13 additions & 1 deletion src/d2_docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,24 @@ def export_database(image_name, db_path):
mkdir_p(os.path.dirname(db_path))

with open(db_path, "wb") as db_file:
pg_dump = "set -o pipefail; pg_dump -U dhis dhis2 | gzip"
pg_dump = "set -o pipefail; " + " ".join(get_pg_dump_command())
# -T: Disable pseudo-tty allocation. Otherwise the compressed output pipe is corrupted.
cmd = ["exec", "-T", "db", "bash", "-c", pg_dump]
run_docker_compose(cmd, image_name, stdout=db_file)


def get_pg_dump_command(exclude_table=True, compress=True):
cmd = ["pg_dump", "-U", "dhis", "dhis2"]

if exclude_table:
cmd += ["--exclude-table", "'analytics*'"]

if compress:
cmd += ["|", "gzip"]

return cmd


def load_images_file(input_file):
"""Load docker images from local file."""
return run(["docker", "load", "-i", input_file], capture_output=True)
Expand Down