# Generated by Django 4.2.28 on 2026-03-24 14:56

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


class Migration(migrations.Migration):
    dependencies = [
        ('db', '0047_alter_worker_activation_token'),
    ]

    operations = [
        migrations.AddField(
            model_name='token',
            name='_activating_worker_id',
            field=models.BigIntegerField(
                db_column='activating_worker_id',
                editable=False,
                help_text='Mirror of Worker.activation_token; only for use in constraints',
                null=True,
            ),
        ),
        migrations.AddField(
            model_name='token',
            name='_worker_id',
            field=models.BigIntegerField(
                db_column='worker_id',
                editable=False,
                help_text='Mirror of Worker.token; only for use in constraints',
                null=True,
            ),
        ),
        migrations.AddField(
            model_name='token',
            name='token_type',
            field=models.CharField(
                blank=True,
                choices=[
                    ('worker-activation', 'Worker activation'),
                    ('worker', 'Worker'),
                    ('user', 'User'),
                ],
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name='worker',
            trigger=pgtrigger.compiler.Trigger(
                name='db_worker_sync_unset_token',
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."token_id" IS NOT NULL)',
                    func="UPDATE db_token SET worker_id = NULL WHERE db_token.id = OLD.token_id; IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF;",
                    hash='f7dcb58150544aad199c222db2908e04c91da36c',
                    operation='UPDATE OF "token_id" OR DELETE',
                    pgid='pgtrigger_db_worker_sync_unset_token_577a2',
                    table='db_worker',
                    when='BEFORE',
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name='worker',
            trigger=pgtrigger.compiler.Trigger(
                name='db_worker_sync_unset_activation_token',
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."activation_token_id" IS NOT NULL)',
                    func="UPDATE db_token SET activating_worker_id = NULL WHERE db_token.id = OLD.activation_token_id; IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF;",
                    hash='94dbb738ac99d0e3258c71c6dc750ab63cdce39c',
                    operation='UPDATE OF "activation_token_id" OR DELETE',
                    pgid='pgtrigger_db_worker_sync_unset_activation_token_4362c',
                    table='db_worker',
                    when='BEFORE',
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name='worker',
            trigger=pgtrigger.compiler.Trigger(
                name='db_worker_sync_set_token',
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (NEW."token_id" IS NOT NULL)',
                    func='UPDATE db_token SET worker_id = NEW.id WHERE db_token.id = NEW.token_id; RETURN NEW;',
                    hash='41a37b3ee15b5d073e01597eb49200b33923bb6c',
                    operation='INSERT OR UPDATE OF "token_id"',
                    pgid='pgtrigger_db_worker_sync_set_token_a1f77',
                    table='db_worker',
                    when='AFTER',
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name='worker',
            trigger=pgtrigger.compiler.Trigger(
                name='db_worker_sync_set_activation_token',
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (NEW."activation_token_id" IS NOT NULL)',
                    func='UPDATE db_token SET activating_worker_id = NEW.id WHERE db_token.id = NEW.activation_token_id; RETURN NEW;',
                    hash='2c21750cfe3932454d16ed0100b9971d4c687d55',
                    operation='INSERT OR UPDATE OF "activation_token_id"',
                    pgid='pgtrigger_db_worker_sync_set_activation_token_dae25',
                    table='db_worker',
                    when='AFTER',
                ),
            ),
        ),
    ]
