mirror of https://github.com/interlegis/sapl.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
2.2 KiB
54 lines
2.2 KiB
# Generated by Django 2.2.28 on 2026-08-16 17:36
|
|
|
|
from django.db import migrations, models
|
|
from django.db.models import Count
|
|
|
|
|
|
def remove_votos_duplicados(apps, schema_editor):
|
|
"""Preserva o voto canônico antes de criar os índices únicos."""
|
|
VotoParlamentar = apps.get_model('sessao', 'VotoParlamentar')
|
|
|
|
for materia_field in ('ordem_id', 'expediente_id'):
|
|
# order_by() limpa o Meta.ordering ('id'), que no Django 2.2 entraria
|
|
# no GROUP BY e deixaria todo grupo com n=1.
|
|
duplicadas = (VotoParlamentar.objects
|
|
.exclude(**{materia_field: None})
|
|
.order_by()
|
|
.values('parlamentar_id', materia_field)
|
|
.annotate(n=Count('id'))
|
|
.filter(n__gt=1))
|
|
for chave in duplicadas:
|
|
votos_duplicados = list(VotoParlamentar.objects.filter(
|
|
parlamentar_id=chave['parlamentar_id'],
|
|
**{materia_field: chave[materia_field]}))
|
|
voto_preservado = max(
|
|
votos_duplicados,
|
|
key=lambda voto: (
|
|
voto.votacao_id is not None,
|
|
voto.data_hora is not None,
|
|
voto.data_hora,
|
|
voto.id,
|
|
))
|
|
ids_removidos = [voto.id for voto in votos_duplicados
|
|
if voto.id != voto_preservado.id]
|
|
VotoParlamentar.objects.filter(id__in=ids_removidos).delete()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('sessao', '0070_presenca_unica_por_sessao'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(
|
|
remove_votos_duplicados, migrations.RunPython.noop),
|
|
migrations.AddConstraint(
|
|
model_name='votoparlamentar',
|
|
constraint=models.UniqueConstraint(condition=models.Q(ordem__isnull=False), fields=('parlamentar', 'ordem'), name='sessao_votoparlamentar_unique_parlamentar_ordem'),
|
|
),
|
|
migrations.AddConstraint(
|
|
model_name='votoparlamentar',
|
|
constraint=models.UniqueConstraint(condition=models.Q(expediente__isnull=False), fields=('parlamentar', 'expediente'), name='sessao_votoparlamentar_unique_parlamentar_expediente'),
|
|
),
|
|
]
|
|
|