Skip to content

Commit

Permalink
refactor: initial data optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
TanookiVerde committed Jan 16, 2024
1 parent 0ff64c9 commit 8c5df55
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions api/scripts/database_initial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ async def run():
estados = relacao_estado_municipio[['UF','Nome_UF']].drop_duplicates()
municipios = relacao_estado_municipio[['UF','Nome_Município','Código Município Completo']]

municipios_para_criar = []
for _, estado in estados.iterrows():
state, _ = await State.get_or_create(
country=brasil,
Expand All @@ -92,11 +93,14 @@ async def run():
)

for _, municipio in municipios[municipios['UF'] == estado['UF']].iterrows():
await City.get_or_create(
state=state,
code=municipio['Código Município Completo'],
name=municipio['Nome_Município']
municipios_para_criar.append(
City(
state=state,
code=municipio['Código Município Completo'],
name=municipio['Nome_Município']
)
)
City.bulk_create(municipios_para_criar, ignore_conflicts=True)
logger.info("States and Cities created successfully")

await Tortoise.close_connections()
Expand Down

0 comments on commit 8c5df55

Please sign in to comment.