# Generated by Django 5.2.13 on 2026-06-20 20:42

from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps


def copy_tags_from_most_recent_worker(
    apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
    """Set WorkerPool dynamic_metadata from the most recently active worker."""
    WorkerPool = apps.get_model("db", "WorkerPool")

    for pool in WorkerPool.objects.filter(dynamic_metadata={}):
        latest_worker = pool.worker_set.order_by(
            "-dynamic_metadata_updated_at"
        ).first()
        if latest_worker is not None:
            pool.dynamic_metadata = latest_worker.dynamic_metadata
            pool.save()


class Migration(migrations.Migration):
    dependencies = [
        ('db', '0063_workflowtemplate_restricted'),
    ]
    operations = [
        migrations.RemoveField(
            model_name='workerpool',
            name='tags',
        ),
        migrations.AddField(
            model_name='workerpool',
            name='static_metadata',
            field=models.JSONField(default=dict, blank=True),
        ),
        migrations.AddField(
            model_name='workerpool',
            name='dynamic_metadata',
            field=models.JSONField(default=dict, blank=True),
        ),
        migrations.RunPython(
            copy_tags_from_most_recent_worker, migrations.RunPython.noop
        ),
    ]
