# Generated by Django 4.2.28 on 2026-03-24 18:20

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


class Migration(migrations.Migration):
    dependencies = [
        ('db', '0049_token_worker_data'),
    ]

    operations = [
        migrations.AlterField(
            model_name='token',
            name='token_type',
            field=models.CharField(
                choices=[
                    ('worker-activation', 'Worker activation'),
                    ('worker', 'Worker'),
                    ('user', 'User'),
                ]
            ),
        ),
        migrations.AddConstraint(
            model_name='token',
            constraint=models.CheckConstraint(
                check=models.Q(
                    ('token_type__in', ('worker-activation', 'worker', 'user'))
                ),
                name='db_token_valid_type',
            ),
        ),
        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 = 'user' AND NEW.user_id IS NULL THEN RAISE EXCEPTION 'User token % has no user', NEW.id; ELSE RETURN NULL; END IF;",
                    hash='4b8d9585c5a2f20c3cfedadf18dab484ec219d56',
                    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='token',
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(
                        ('_worker_id__isnull', True),
                        ('token_type', 'worker-activation'),
                        ('user__isnull', True),
                    ),
                    models.Q(
                        ('_activating_worker_id__isnull', True),
                        ('token_type', 'worker'),
                        ('user__isnull', True),
                    ),
                    models.Q(
                        ('_activating_worker_id__isnull', True),
                        ('_worker_id__isnull', True),
                        ('token_type', 'user'),
                    ),
                    _connector='OR',
                ),
                name='db_token_consistent_type',
            ),
        ),
    ]
