From 636af460acc4afbf3ffae362babc0d61de4fa5b6 Mon Sep 17 00:00:00 2001 From: Moran Date: Sat, 4 Apr 2026 15:16:36 -0300 Subject: [PATCH] =?UTF-8?q?Migration=20para=20atualizar=20nome=5Fcientific?= =?UTF-8?q?o=20dos=20tombos=20que=20estao=20inconsistentes=20com=20a=20con?= =?UTF-8?q?catena=C3=A7=C3=A3o=20do=20genero.nome=20e=20especie.nome=20ou?= =?UTF-8?q?=20apenas=20genero.nome=20quando=20o=20especie=5Fid=20do=20tomb?= =?UTF-8?q?o=20esta=20vazio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70000_fix-nome-cientifico-inconsistente.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/database/migration/20260404170000_fix-nome-cientifico-inconsistente.ts diff --git a/src/database/migration/20260404170000_fix-nome-cientifico-inconsistente.ts b/src/database/migration/20260404170000_fix-nome-cientifico-inconsistente.ts new file mode 100644 index 0000000..bac7528 --- /dev/null +++ b/src/database/migration/20260404170000_fix-nome-cientifico-inconsistente.ts @@ -0,0 +1,28 @@ +import { Knex } from 'knex' + +export async function run(knex: Knex): Promise { + await knex.transaction(async trx => { + // Atualiza tombos que possuem genero e especie + await trx.raw(` + UPDATE tombos + SET nome_cientifico = CONCAT(g.nome, ' ', e.nome) + FROM generos g, especies e + WHERE tombos.genero_id = g.id + AND tombos.especie_id = e.id + AND ( + tombos.nome_cientifico IS DISTINCT FROM CONCAT(g.nome, ' ', e.nome) + OR e.genero_id IS DISTINCT FROM tombos.genero_id + ) + `) + + // Atualiza tombos que possuem apenas genero (sem especie) + await trx.raw(` + UPDATE tombos + SET nome_cientifico = g.nome + FROM generos g + WHERE tombos.genero_id = g.id + AND tombos.especie_id IS NULL + AND tombos.nome_cientifico IS DISTINCT FROM g.nome + `) + }) +}