Source code for ferrosoft.apps.ferrobase.management.commands.loaddata_tenant

import sys

from django.core import management
from django.db import transaction, IntegrityError

from ferrosoft.apps.ferrobase.management.tenantcommand import TenantAwareCommand


[docs] class Command(TenantAwareCommand): """Management command that loads fixtures into a tenant database. Wraps Django's ``loaddata`` with tenant context activation and runs the import inside an atomic transaction so a partial fixture failure does not leave the database in an inconsistent state. """
[docs] def add_arguments(self, parser): super().add_arguments(parser) parser.add_argument("fixture", nargs="+", type=str)
[docs] def handle_tenant(self, *args, **options): try: with transaction.atomic(using=self.tenant.database_name): management.call_command( "loaddata", *options["fixture"], database=self.tenant.database_name, ) except IntegrityError as e: self.stderr.write(str(e)) self.stderr.write( self.style.ERROR("Failed importing fixture to tenant %s" % self.tenant) ) sys.exit(2)