Skip to content

Commit

Permalink
Merge pull request #134 from EyeSeeTea/feature/exclude_analytics
Browse files Browse the repository at this point in the history
exclude analytic tables from copy
  • Loading branch information
ifoche authored Jan 16, 2025
2 parents 6cb8d5b + 24e6e4c commit 820eeec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 820eeec

Please sign in to comment.