Last active
June 15, 2022 19:55
-
-
Save Marlysson/050762f526e67c85cedc729361052f38 to your computer and use it in GitHub Desktop.
Replica de dados
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core import management | |
from pathlib import Path | |
class Command(management.base.BaseCommand): | |
def handle(self, *args, **options): | |
apps = ["core"] | |
models = ["core.ModelA", "core.ModelB"] | |
fixture_name = "audit" | |
fixture_file = Path(f"core/fixtures/{fixture_name}.json") | |
# Dump do banco original com modelos especificados para um arquivo | |
with open(fixture_file, "w") as fixture: | |
management.call_command("dumpdata", *models, database="default", format="json", output=fixture_file) | |
# Manter os mesmos modelos entre os bancos | |
management.call_command("migrate", *apps, database="default") | |
management.call_command("migrate", *apps, database="audit") | |
# Limpeza do banco de audit | |
management.call_command("flush", database="audit", interactive=False) | |
# Load dos dados gerados para o banco de audit | |
management.call_command("loaddata", fixture_name, database="audit") | |
# Deleção do arquivo de fixture | |
fixture_file.unlink() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': BASE_DIR / 'db.sqlite3', | |
}, | |
'audit': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': BASE_DIR / 'audit.sqlite3', | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment