# Generated by Django 5.2.9 on 2026-02-03 15:39

import django.contrib.postgres.constraints
import django.contrib.postgres.fields.ranges
from django.db import migrations
from psycopg.types.range import DateRange

from wbfdm.models import InstrumentListThroughModel
import django.contrib.postgres.constraints
from django.db import migrations, models

def migrate_timespan(apps, schema_editor):
    InstrumentListThroughModel = apps.get_model('wbfdm.InstrumentListThroughModel')
    objs = []
    for rel in InstrumentListThroughModel.objects.all():
        rel.timespan = DateRange(rel.from_date, rel.to_date)
        objs.append(rel)

    InstrumentListThroughModel.objects.bulk_update(objs, ["timespan"])
class Migration(migrations.Migration):

    dependencies = [
        ('io', '0009_alter_importedobjectproviderrelationship_unique_together_and_more'),
        ('wbfdm', '0037_relatedinstrumentthroughmodel_unique_instrument_relationship_and_more'),
        ('currency', '0002_alter_currencyfxrates_unique_together_and_more'),
        ('geography', '0002_geography_geography_geography_tree_i739a'),
        ('tags', '0002_alter_tag_unique_together_tag_unique_tag'),
    ]

    operations = [

        migrations.AddField(
            model_name='instrumentlistthroughmodel',
            name='timespan',
            field=django.contrib.postgres.fields.ranges.DateRangeField(default=None, verbose_name='Timespan', null=True, blank=True),
            preserve_default=False,
        ),
        migrations.RunPython(migrate_timespan),
        migrations.RemoveField(
            model_name='instrumentlistthroughmodel',
            name='from_date',
        ),
        migrations.RemoveField(
            model_name='instrumentlistthroughmodel',
            name='to_date',
        ),
        migrations.AddConstraint(
            model_name='instrumentlistthroughmodel',
            constraint=django.contrib.postgres.constraints.ExclusionConstraint(expressions=[('timespan', '&&'), ('instrument', '='), ('instrument_list', '=')], name='exclude_overlapping_instrument_list'),
        ),
        migrations.AlterField(
            model_name='instrumentlistthroughmodel',
            name='timespan',
            field=django.contrib.postgres.fields.ranges.DateRangeField(default=None, verbose_name='Timespan'),
            preserve_default=False,
        ),
        migrations.RemoveConstraint(
            model_name='instrumentlistthroughmodel',
            name='unique_instrument_per_list',
        ),
        migrations.RemoveConstraint(
            model_name='instrumentlistthroughmodel',
            name='exclude_overlapping_instrument_list',
        ),
        migrations.AddField(
            model_name='instrumentlist',
            name='constituent_loader_path',
            field=models.CharField(blank=True,
                                   help_text='If set, load the Instrument queryset returned by this function and set them as the new instrument list constituent',
                                   max_length=512, null=True),
        ),
        migrations.AddConstraint(
            model_name='instrumentlistthroughmodel',
            constraint=django.contrib.postgres.constraints.ExclusionConstraint(
                condition=models.Q(('instrument__isnull', False)),
                expressions=[('timespan', '&&'), ('instrument', '='), ('instrument_list', '=')],
                name='exclude_overlapping_instrument_list'),
        ),
        migrations.AddField(
            model_name='instrumentlist',
            name='constituent_loader_kwargs',
            field=models.JSONField(default=dict, blank=True),
        ),
        migrations.AddField(
            model_name='instrument',
            name='is_preferred_primary',
            field=models.BooleanField(default=False),
        ),
        migrations.AddConstraint(
            model_name='instrument',
            constraint=models.UniqueConstraint(condition=models.Q(('is_preferred_primary', True)),
                                               fields=('parent', 'is_preferred_primary'),
                                               name='unique_instrument_preferred_primary'),
        ),
    ]
