Skip to content

Commit

Permalink
fix: update fixture commands (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Jan 31, 2024
1 parent 27531e5 commit 8447fcb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
13 changes: 6 additions & 7 deletions bd_api/apps/core/management/commands/dumpfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 12 additions & 9 deletions bd_api/apps/core/management/commands/loadfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 8447fcb

Please sign in to comment.