From 8e06b01eb31e10bb3c7b2c3cce9bed4b6d3de93b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Sat, 21 Feb 2026 11:43:27 -0300 Subject: [PATCH] =?UTF-8?q?Previne=20erro=20de=20migra=C3=A7=C3=A3o=20se?= =?UTF-8?q?=20usu=C3=A1rio=20sigi=5Fqs=20n=C3=A3o=20existir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/eventos/migrations/0060_drop_viw_eventos.py | 11 +++++++---- .../eventos/migrations/0062_create_viw_eventos.py | 11 +++++++---- .../migrations/0066_viw_eventos_paricipante.py | 11 +++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/sigi/apps/eventos/migrations/0060_drop_viw_eventos.py b/sigi/apps/eventos/migrations/0060_drop_viw_eventos.py index ebdf608..cc89ea7 100644 --- a/sigi/apps/eventos/migrations/0060_drop_viw_eventos.py +++ b/sigi/apps/eventos/migrations/0060_drop_viw_eventos.py @@ -4,6 +4,8 @@ from django.db import migrations SQL_STMT = "DROP VIEW IF EXISTS viw_eventos;" SQL_REVERSE_STMT = """ +DO $$ +BEGIN create view viw_eventos as select e.id, e.nome, e.descricao, e.solicitante, e.data_inicio, e.data_termino, e.local, e.publico_alvo, @@ -33,7 +35,10 @@ from eventos_evento e inner join contatos_municipio m on m.codigo_ibge = o.municipio_id inner join contatos_unidadefederativa uf on uf.codigo_ibge = m.uf_id inner join eventos_tipoevento t on t.id = e.tipo_evento_id; -grant select on viw_eventos to sigi_qs; +if exists (select 1 from pg_roles where rolname = 'sigi_qs') then + grant select on viw_eventos to sigi_qs; +end if; +END $$ """ @@ -43,6 +48,4 @@ class Migration(migrations.Migration): ("eventos", "0059_separa_hora_da_data"), ] - operations = [ - migrations.RunSQL(sql=SQL_STMT, reverse_sql=SQL_REVERSE_STMT) - ] + operations = [migrations.RunSQL(sql=SQL_STMT, reverse_sql=SQL_REVERSE_STMT)] diff --git a/sigi/apps/eventos/migrations/0062_create_viw_eventos.py b/sigi/apps/eventos/migrations/0062_create_viw_eventos.py index 3dfbbf4..e04a0dd 100644 --- a/sigi/apps/eventos/migrations/0062_create_viw_eventos.py +++ b/sigi/apps/eventos/migrations/0062_create_viw_eventos.py @@ -3,6 +3,8 @@ from django.db import migrations SQL_STMT = """ +DO $$ +BEGIN create view viw_eventos as select e.id, e.nome, e.descricao, e.solicitante, (e.data_inicio + e.hora_inicio) as data_inicio, (e.data_termino + e.hora_termino) as data_termino, e.local, e.publico_alvo, @@ -32,7 +34,10 @@ from eventos_evento e inner join contatos_municipio m on m.codigo_ibge = o.municipio_id inner join contatos_unidadefederativa uf on uf.codigo_ibge = m.uf_id inner join eventos_tipoevento t on t.id = e.tipo_evento_id; -grant select on viw_eventos to sigi_qs; +if exists (select 1 from pg_roles where rolname = 'sigi_qs') then + grant select on viw_eventos to sigi_qs; +end if; +END $$ """ SQL_REVERSE_STMT = "DROP VIEW viw_eventos;" @@ -43,6 +48,4 @@ class Migration(migrations.Migration): ("eventos", "0061_alter_evento_data_inicio_alter_evento_data_termino"), ] - operations = [ - migrations.RunSQL(sql=SQL_STMT, reverse_sql=SQL_REVERSE_STMT) - ] + operations = [migrations.RunSQL(sql=SQL_STMT, reverse_sql=SQL_REVERSE_STMT)] diff --git a/sigi/apps/eventos/migrations/0066_viw_eventos_paricipante.py b/sigi/apps/eventos/migrations/0066_viw_eventos_paricipante.py index 6a40dee..18b262c 100644 --- a/sigi/apps/eventos/migrations/0066_viw_eventos_paricipante.py +++ b/sigi/apps/eventos/migrations/0066_viw_eventos_paricipante.py @@ -3,6 +3,8 @@ from django.db import migrations SQL_STMT = """ +DO $$ +BEGIN create view viw_eventos_participante as select ep.evento_id as id_evento, ep.casa_legislativa_id as id_casa, @@ -11,7 +13,10 @@ select ep.evento_id as id_evento, ep.email, ep.local_trabalho from eventos_participante ep; -grant select on viw_eventos_participante to sigi_qs; +if exists (select 1 from pg_roles where rolname = 'sigi_qs') then + grant select on viw_eventos_participante to sigi_qs; +end if; +END $$ """ SQL_REVERSE_STMT = "DROP VIEW viw_eventos_participante;" @@ -22,6 +27,4 @@ class Migration(migrations.Migration): ("eventos", "0065_participantes_visitas"), ] - operations = [ - migrations.RunSQL(sql=SQL_STMT, reverse_sql=SQL_REVERSE_STMT) - ] + operations = [migrations.RunSQL(sql=SQL_STMT, reverse_sql=SQL_REVERSE_STMT)]