# Generated by Django 5.2.9 on 2026-01-29 02:58

import common.encoder.encoder
import django.db.models.deletion
import uuid_utils.compat
from django.db import migrations, models

old = [
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/md2docx/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/mcp_output/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/feishubot/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/mongo/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/OFD_Parse/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/html_to_pdf/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/feishu_datasource/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/quotation_generation_agent/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/html2pdf/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/sqlbot_ai/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/metaso/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/registry/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/smtp_email/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/wecomrobot/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/paperx/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/case_inquire/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/data_analysis_assistant/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/crm_intelligent_search/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/case_collection/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/knowledge_self_assessment/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/minerU_util/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/invoice_recognition/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/fragment/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/contract_review/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/echart_to_svg/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/extract/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/big_order/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/crm_intelligent_recording/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/dingrobot/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/timestamp/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/knowledge_workflow/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/household_registration_policy_qa_assistant/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/httputils/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/html_compression/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/crm_customer_entry_ai_agent/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/anspire/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/application_template/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/baidu-translate/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/website_translation/logo.png",
]
new = [
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_md2docx/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_mcp_output/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_feishubot/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_mongo/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_OFD_Parse/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_html_to_pdf/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/db_feishu_datasource/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_quotation_generation_agent/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_html2pdf/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_sqlbot_ai/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_metaso/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_registry/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_smtp_email/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_wecomrobot/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_paperx/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_case_inquire/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_data_analysis_assistant/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_crm_intelligent_search/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_case_collection/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_knowledge_self_assessment/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_minerU_util/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_invoice_recognition/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_fragment/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_contract_review/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_echart_to_svg/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_extract/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_big_order/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_crm_intelligent_recording/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_dingrobot/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_timestamp/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/kbwf_knowledge_workflow/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_household_registration_policy_qa_assistant/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_httputils/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_html_compression/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_crm_customer_entry_ai_agent/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_anspire/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_application_template/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/tool_baidu-translate/logo.png",
    "https://raw.githubusercontent.com/however-yir/nebula-kb/main/appstore/assets/app_website_translation/logo.png",
]

def _replace_tool_icons(apps, schema_editor):
    if len(old) != len(new):
        raise ValueError("`old` 与 `new` 长度不一致，无法一一替换")

    Tool = apps.get_model("tools", "Tool")
    mapping = dict(zip(old, new))

    # 逐个 update，避免依赖数据库对 CASE/WHEN 的兼容差异
    for old_icon, new_icon in mapping.items():
        Tool.objects.filter(icon=old_icon).update(icon=new_icon)

class Migration(migrations.Migration):

    dependencies = [
        ('tools', '0004_alter_tool_tool_type'),
    ]

    operations = [
        migrations.CreateModel(
            name='ToolRecord',
            fields=[
                ('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
                ('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
                ('id', models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
                ('source_type', models.CharField(choices=[('APPLICATION', 'Application'), ('KNOWLEDGE', 'Knowledge'), ('TRIGGER', 'Trigger')], default='APPLICATION', max_length=256, verbose_name='触发器任务类型')),
                ('source_id', models.UUIDField(verbose_name='资源id')),
                ('meta', models.JSONField(default=dict, encoder=common.encoder.encoder.SystemEncoder)),
                ('state', models.CharField(choices=[('PENDING', 'Pending'), ('STARTED', 'Started'), ('SUCCESS', 'Success'), ('FAILURE', 'Failure'), ('REVOKE', 'Revoke'), ('REVOKED', 'Revoked')], default='STARTED', max_length=20, verbose_name='状态')),
                ('run_time', models.FloatField(default=0, verbose_name='运行时长')),
                ('tool', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='tools.tool')),
                ('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
            ],
            options={
                'db_table': 'tool_record',
            },
        ),
        migrations.RunPython(_replace_tool_icons, reverse_code=migrations.RunPython.noop),
    ]
