# Generated by Django 5.2.7 on 2026-04-17 22:42

import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations, models

import debusine.artifacts.models


class Migration(migrations.Migration):
    dependencies = [
        ('db', '0052_collectionrelation_cascade'),
    ]

    operations = [
        pgtrigger.migrations.RemoveTrigger(
            model_name='token',
            name='db_token_required_type_parameters',
        ),
        migrations.RemoveConstraint(
            model_name='asset',
            name='db_asset_workspace_not_null',
        ),
        migrations.RemoveConstraint(
            model_name='token',
            name='db_token_consistent_type',
        ),
        migrations.AddField(
            model_name='token',
            name='work_request',
            field=models.OneToOneField(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name='work_request_token',
                to='db.workrequest',
            ),
        ),
        migrations.AlterField(
            model_name='collection',
            name='child_artifacts',
            field=models.ManyToManyField(
                related_name='parent_collections',
                through='db.CollectionItem',
                through_fields=('parent_collection', 'artifact'),
                to='db.artifact',
            ),
        ),
        migrations.AlterField(
            model_name='collection',
            name='child_collections',
            field=models.ManyToManyField(
                related_name='parent_collections',
                through='db.CollectionItem',
                through_fields=('parent_collection', 'collection'),
                to='db.collection',
            ),
        ),
        migrations.AlterField(
            model_name='token',
            name='token_type',
            field=models.CharField(
                choices=[
                    ('worker-activation', 'Worker activation'),
                    ('worker', 'Worker'),
                    ('work-request', 'Work request'),
                    ('user', 'User'),
                ]
            ),
        ),
        migrations.AlterField(
            model_name='workspace',
            name='inherits',
            field=models.ManyToManyField(
                related_name='inherited_by',
                through='db.WorkspaceChain',
                through_fields=('child', 'parent'),
                to='db.workspace',
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name='token',
            trigger=pgtrigger.compiler.Trigger(
                name='db_token_required_type_parameters',
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    constraint='CONSTRAINT',
                    func=(
                        "SELECT * FROM db_token INTO NEW WHERE id = "
                        "db_token.id; IF NEW.token_type = 'worker-activation' "
                        "AND NEW.activating_worker_id IS NULL THEN RAISE "
                        "EXCEPTION 'Worker activation token % has no "
                        "activating worker', NEW.id; ELSIF NEW.token_type = "
                        "'worker' AND NEW.worker_id IS NULL THEN RAISE "
                        "EXCEPTION 'Worker token % has no worker', NEW.id; "
                        "ELSIF NEW.token_type = 'work-request' AND "
                        "NEW.work_request_id IS NULL THEN RAISE EXCEPTION "
                        "'Work request token % has no work request', NEW.id; "
                        "ELSIF NEW.token_type = 'user' AND NEW.user_id IS "
                        "NULL THEN RAISE EXCEPTION 'User token % has no user', "
                        "NEW.id; ELSE RETURN NULL; END IF;"
                    ),
                    hash='253d5833247a1e947d8b4e43a82d7f744ac2ff89',
                    operation='INSERT OR UPDATE',
                    pgid='pgtrigger_db_token_required_type_parameters_a5603',
                    table='db_token',
                    timing='DEFERRABLE INITIALLY DEFERRED',
                    when='AFTER',
                ),
            ),
        ),
        migrations.AddConstraint(
            model_name='asset',
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(
                        (
                            'category__in',
                            {
                                debusine.assets.models.AssetCategory[
                                    'APT_AUTHENTICATION'
                                ],
                                debusine.assets.models.AssetCategory[
                                    'SIGNING_KEY'
                                ],
                            },
                        ),
                        _negated=True,
                    ),
                    ('workspace__isnull', False),
                    _connector='OR',
                ),
                name='db_asset_workspace_not_null',
            ),
        ),
        migrations.AddConstraint(
            model_name='token',
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(
                        ('_worker_id__isnull', True),
                        ('token_type', 'worker-activation'),
                        ('user__isnull', True),
                        ('work_request_id__isnull', True),
                    ),
                    models.Q(
                        ('_activating_worker_id__isnull', True),
                        ('token_type', 'worker'),
                        ('user__isnull', True),
                        ('work_request_id__isnull', True),
                    ),
                    models.Q(
                        ('_activating_worker_id__isnull', True),
                        ('_worker_id__isnull', True),
                        ('token_type', 'work-request'),
                        ('user__isnull', True),
                    ),
                    models.Q(
                        ('_activating_worker_id__isnull', True),
                        ('_worker_id__isnull', True),
                        ('token_type', 'user'),
                        ('work_request_id__isnull', True),
                    ),
                    _connector='OR',
                ),
                name='db_token_consistent_type',
            ),
        ),
    ]
