# Generated by Django 5.2.10 on 2026-01-20 00:54

from django.db import migrations, models


def prune_duplicate_global_servers(apps, schema_editor):
    FTPServer = apps.get_model("ftp", "FTPServer")
    global_servers = FTPServer.objects.filter(node__isnull=True).order_by("id")
    server_to_keep = global_servers.first()
    if server_to_keep:
        global_servers.exclude(pk=server_to_keep.pk).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("ftp", "0003_remove_ftpfolder_ftp_ftpfolder_owner_exclusive_and_more"),
        ("nodes", "0019_alter_netmessage_lcd_channel_type"),
    ]

    operations = [
        migrations.RunPython(prune_duplicate_global_servers, migrations.RunPython.noop),
        migrations.AddConstraint(
            model_name="ftpserver",
            constraint=models.UniqueConstraint(
                condition=models.Q(("node__isnull", True)),
                fields=("node",),
                name="ftpserver_unique_global",
            ),
        ),
    ]
