Skip to content

Commit 035f5c7

Browse files
committed
Organizando e iniciando novo populate em comandos
1 parent 33c3081 commit 035f5c7

4 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
from enum import Enum
3+
from pathlib import Path
4+
5+
6+
class LocalPolulate(Enum):
7+
FOLDER = "backups"
8+
FILE_NAME = "backup_prod_v1.json"
9+
URL_JSON = "https://storage.googleapis.com/basedosdados-dev/backend_backup/backup_prod_v1.json"
10+
PATH_FILE = Path(FOLDER) / FILE_NAME
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from django.core.management import call_command
4+
from django.core.management.base import BaseCommand
5+
from loguru import logger
6+
7+
from .constants import LocalPolulate
8+
from .utils import remove_connection_accounts
9+
10+
11+
class Command(BaseCommand):
12+
help = "Dump APP v1, avoiding APP accout"
13+
14+
def handle(self, *args, **options) -> None:
15+
LocalPolulate.PATH_FILE.value.parent.mkdir(parents=True, exist_ok=True)
16+
17+
logger.info("Fazendo Dump")
18+
call_command("dumpdata", "v1", indent=2, output=str(LocalPolulate.PATH_FILE.value))
19+
20+
logger.info("Removendo conexão com dos dados de V1 com Account")
21+
remove_connection_accounts(LocalPolulate.PATH_FILE.value)
22+
23+
logger.info("Processo concluído com sucesso!")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
from django.core.management import call_command
3+
from django.core.management.base import BaseCommand
4+
from loguru import logger
5+
6+
from .constants import LocalPolulate
7+
from .utils import download_backup
8+
9+
10+
class Command(BaseCommand):
11+
help = "Populate local database with initial data from prod"
12+
13+
def handle(self, *args, **options) -> None:
14+
download_backup(LocalPolulate.URL_JSON.value, LocalPolulate.PATH_FILE.value)
15+
16+
logger.info("Purge previous database if exists")
17+
call_command("flush", interactive=False)
18+
19+
logger.info("Migrate development database")
20+
call_command("migrate")
21+
22+
logger.info("Load data!")
23+
call_command("loaddata", str(LocalPolulate.PATH_FILE.value))
24+
25+
logger.info("Atualizar index!")
26+
call_command("update_index")
27+
28+
logger.info("Processo concluído com sucesso!")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
import json
3+
from pathlib import Path
4+
5+
import requests
6+
7+
8+
def download_backup(url_arquivo: str, caminho_salvar: str) -> None:
9+
"""
10+
Baixa um arquivo JSON grande usando chunks para economia de memória.
11+
12+
Args:
13+
url_arquivo: URL do arquivo JSON
14+
caminho_salvar: Caminho onde salvar o arquivo
15+
"""
16+
# Configurações para controle de memória
17+
tamanho_chunk = 1024 * 1024 # 1MB por chunk
18+
19+
try:
20+
resposta = requests.get(url_arquivo, stream=True)
21+
22+
tamanho_total = int(resposta.headers.get("content-length", 0))
23+
24+
caminho_arquivo = Path(caminho_salvar)
25+
caminho_arquivo.parent.mkdir(parents=True, exist_ok=True)
26+
27+
with open(caminho_salvar, "wb") as arquivo:
28+
bytes_baixados = 0
29+
30+
for chunk in resposta.iter_content(chunk_size=tamanho_chunk):
31+
if chunk:
32+
arquivo.write(chunk)
33+
bytes_baixados += len(chunk)
34+
35+
print(
36+
f"\rBaixando: {bytes_baixados/tamanho_total*100:.2f}%", end="", flush=True
37+
)
38+
39+
print("\n\nDownload concluído!")
40+
41+
except Exception as e:
42+
print(f"Erro ao baixar o arquivo: {str(e)}")
43+
44+
45+
def remove_connection_accounts(path: str) -> None:
46+
"""
47+
Remove qualquer conexão com que o JSON gerado do APP v1 tenha com account
48+
49+
Args:
50+
path: Caminho para o path JSON
51+
"""
52+
53+
try:
54+
# Ler o path
55+
with open(path, "r", encoding="utf-8") as json_file:
56+
dados = json.load(json_file)
57+
58+
# Processar cada registro
59+
for registro in dados:
60+
if registro.get("model") == "v1.table":
61+
registro.setdefault("fields", {}).update(
62+
{"published_by": [], "data_cleaned_by": []}
63+
)
64+
elif registro.get("model") == "v1.informationrequest":
65+
registro.setdefault("fields", {})["started_by"] = None
66+
67+
# Salvar alterações
68+
with open(path, "w", encoding="utf-8") as f:
69+
json.dump(dados, f, indent=2, ensure_ascii=False)
70+
71+
except FileNotFoundError:
72+
print(f"Erro: path '{path}' não encontrado")
73+
except json.JSONDecodeError:
74+
print(f"Erro: path '{path}' contém JSON inválido")
75+
except Exception as e:
76+
print(f"Erro inesperado ao processar '{path}': {str(e)}")

0 commit comments

Comments
 (0)