From 8447fcbfad6d0dcdb80bea590a05327ee1091f0a Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Wed, 31 Jan 2024 20:23:34 -0300 Subject: [PATCH] fix: update fixture commands (#553) --- .../core/management/commands/dumpfixture.py | 13 ++++++------ .../core/management/commands/loadfixture.py | 21 +++++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bd_api/apps/core/management/commands/dumpfixture.py b/bd_api/apps/core/management/commands/dumpfixture.py index 6f23f365..21fccadc 100644 --- a/bd_api/apps/core/management/commands/dumpfixture.py +++ b/bd_api/apps/core/management/commands/dumpfixture.py @@ -5,9 +5,12 @@ from django.core.management.commands.dumpdata import Command as DumpDataCommand from faker import Faker +from loguru import logger fake = Faker("pt_BR") +logger = logger.bind(module="core") + def empty(): ts = datetime.now() @@ -41,17 +44,13 @@ def empty(): class Command(DumpDataCommand): - """Dump data, avoiding profiles and payments""" + """Dump data, avoiding profiles""" def handle(self, *args, **options) -> str | None: - print("Dump fixtures") - options["exclude"] = [ - "djstripe", - *options["exclude"], - ] + logger.info("Dump fixtures") response = super().handle(*args, **options) - print("Filter fixtures") + logger.info("Filter fixtures") output = options["output"] with open(output, "r") as file: data = load(file) diff --git a/bd_api/apps/core/management/commands/loadfixture.py b/bd_api/apps/core/management/commands/loadfixture.py index c8e65787..1b09a17d 100644 --- a/bd_api/apps/core/management/commands/loadfixture.py +++ b/bd_api/apps/core/management/commands/loadfixture.py @@ -4,6 +4,7 @@ from django.conf import settings from django.core.management import call_command from django.db import connection +from loguru import logger from modeltranslation.management.commands.loaddata import Command as LoadDataCommand from bd_api.utils import is_prd @@ -14,6 +15,8 @@ DB_PATH = Path(settings.DATABASES.get("default", {}).get("NAME", ".")) DB_STATEMENT = "TRUNCATE" if IS_POSTGRES and not IS_SQLITE else "DELETE FROM" +logger = logger.bind(module="core") + class Command(LoadDataCommand): """Load data with pre and post processing hooks @@ -33,33 +36,33 @@ class Command(LoadDataCommand): def add_arguments(self, parser): super().add_arguments(parser) parser.add_argument( - "--skip-index", - dest="skip_index", + "--build-index", + dest="build_index", action="store_true", - help="Skip index build after loading fixtures", + help="Build index after loading fixtures", ) def handle(self, *args, **options) -> str | None: if is_prd(): return None - print("Purge previous database if exists") + logger.info("Purge previous database if exists") call_command("flush", interactive=False) - print("Migrate development database") + logger.info("Migrate development database") call_command("migrate") - print("Purge database restrictions") + logger.info("Purge database restrictions") with connection.cursor() as cursor: cursor.execute(f"{DB_STATEMENT} auth_permission") cursor.execute(f"{DB_STATEMENT} django_admin_log") cursor.execute(f"{DB_STATEMENT} django_content_type") - print("Load fixtures") + logger.info("Load fixtures") response = super().handle(*args, **options) - if not options["skip_index"]: - print("Build index") + if options["build_index"]: + logger.info("Build index") try: call_command("rebuild_index", interactive=False) except Exception: