# Generated by hand on 2025-12-29
# Creates Crawl and CrawlSchedule tables using raw SQL

from django.db import migrations


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('auth', '0012_alter_user_first_name_max_length'),
    ]

    operations = [
        migrations.RunSQL(
            # Forward SQL
            sql="""
                -- Create crawls_crawl table
                CREATE TABLE IF NOT EXISTS crawls_crawl (
                    id TEXT PRIMARY KEY NOT NULL,
                    created_at DATETIME NOT NULL,
                    modified_at DATETIME NOT NULL,
                    num_uses_succeeded INTEGER NOT NULL DEFAULT 0,
                    num_uses_failed INTEGER NOT NULL DEFAULT 0,

                    urls TEXT NOT NULL,
                    config TEXT,
                    max_depth INTEGER NOT NULL DEFAULT 0,
                    tags_str VARCHAR(1024) NOT NULL DEFAULT '',
                    persona_id TEXT,
                    label VARCHAR(64) NOT NULL DEFAULT '',
                    notes TEXT NOT NULL DEFAULT '',
                    output_dir VARCHAR(512) NOT NULL DEFAULT '',

                    status VARCHAR(15) NOT NULL DEFAULT 'queued',
                    retry_at DATETIME,
                    created_by_id INTEGER NOT NULL,
                    schedule_id TEXT,

                    FOREIGN KEY (created_by_id) REFERENCES auth_user(id) ON DELETE CASCADE,
                    FOREIGN KEY (schedule_id) REFERENCES crawls_crawlschedule(id) ON DELETE SET NULL
                );
                CREATE INDEX IF NOT EXISTS crawls_crawl_status_idx ON crawls_crawl(status);
                CREATE INDEX IF NOT EXISTS crawls_crawl_retry_at_idx ON crawls_crawl(retry_at);
                CREATE INDEX IF NOT EXISTS crawls_crawl_created_at_idx ON crawls_crawl(created_at);
                CREATE INDEX IF NOT EXISTS crawls_crawl_created_by_id_idx ON crawls_crawl(created_by_id);

                -- Create crawls_crawlschedule table
                CREATE TABLE IF NOT EXISTS crawls_crawlschedule (
                    id TEXT PRIMARY KEY NOT NULL,
                    created_at DATETIME NOT NULL,
                    modified_at DATETIME NOT NULL,
                    num_uses_succeeded INTEGER NOT NULL DEFAULT 0,
                    num_uses_failed INTEGER NOT NULL DEFAULT 0,

                    schedule VARCHAR(64) NOT NULL,
                    is_enabled BOOLEAN NOT NULL DEFAULT 1,
                    label VARCHAR(64) NOT NULL DEFAULT '',
                    notes TEXT NOT NULL DEFAULT '',

                    template_id TEXT NOT NULL,
                    created_by_id INTEGER NOT NULL,

                    FOREIGN KEY (template_id) REFERENCES crawls_crawl(id) ON DELETE CASCADE,
                    FOREIGN KEY (created_by_id) REFERENCES auth_user(id) ON DELETE CASCADE
                );
                CREATE INDEX IF NOT EXISTS crawls_crawlschedule_created_at_idx ON crawls_crawlschedule(created_at);
                CREATE INDEX IF NOT EXISTS crawls_crawlschedule_created_by_id_idx ON crawls_crawlschedule(created_by_id);
            """,
            # Reverse SQL
            reverse_sql="""
                DROP TABLE IF EXISTS crawls_crawl;
                DROP TABLE IF EXISTS crawls_crawlschedule;
            """
        ),
    ]
