# Generated by Django migration for PR review critical issue #1
# This migration adds CheckConstraints to ensure annotations and relationships
# in a structural_set must have structural=True.
#
# This addresses a data integrity issue where the query_optimizer assumes all
# annotations in a StructuralAnnotationSet have structural=True (see
# opencontractserver/annotations/query_optimizer.py:207-209), but without
# database-level enforcement this assumption could be violated.

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("annotations", "0049_alter_structuralannotationset_pawls_parse_file_and_more"),
    ]

    operations = [
        # Add constraint to Annotation model
        migrations.AddConstraint(
            model_name="annotation",
            constraint=models.CheckConstraint(
                check=models.Q(structural_set__isnull=True) | models.Q(structural=True),
                name="structural_set_requires_structural_flag",
                violation_error_message="Annotations in a structural_set must have structural=True",
            ),
        ),
        # Add constraint to Relationship model
        migrations.AddConstraint(
            model_name="relationship",
            constraint=models.CheckConstraint(
                check=models.Q(structural_set__isnull=True) | models.Q(structural=True),
                name="rel_structural_set_requires_structural_flag",
                violation_error_message="Relationships in a structural_set must have structural=True",
            ),
        ),
    ]
