# Generated by hand on 2025-12-29
# Creates Machine, Binary, NetworkInterface, and Process tables using raw SQL

from django.db import migrations


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.RunSQL(
            # Forward SQL
            sql="""
                -- Create machine_machine table
                CREATE TABLE IF NOT EXISTS machine_machine (
                    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,

                    guid VARCHAR(64) NOT NULL UNIQUE,
                    hostname VARCHAR(63) NOT NULL,
                    hw_in_docker BOOLEAN NOT NULL DEFAULT 0,
                    hw_in_vm BOOLEAN NOT NULL DEFAULT 0,
                    hw_manufacturer VARCHAR(63) NOT NULL,
                    hw_product VARCHAR(63) NOT NULL,
                    hw_uuid VARCHAR(255) NOT NULL,

                    os_arch VARCHAR(15) NOT NULL,
                    os_family VARCHAR(15) NOT NULL,
                    os_platform VARCHAR(63) NOT NULL,
                    os_release VARCHAR(63) NOT NULL,
                    os_kernel VARCHAR(255) NOT NULL,

                    stats TEXT,
                    config TEXT
                );
                CREATE INDEX IF NOT EXISTS machine_machine_guid_idx ON machine_machine(guid);

                -- Create machine_networkinterface table
                CREATE TABLE IF NOT EXISTS machine_networkinterface (
                    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,

                    machine_id TEXT NOT NULL,
                    iface VARCHAR(15) NOT NULL,
                    ip_public VARCHAR(39) NOT NULL,
                    ip_local VARCHAR(39) NOT NULL,
                    mac_address VARCHAR(17) NOT NULL,
                    dns_server VARCHAR(39) NOT NULL,
                    hostname VARCHAR(256) NOT NULL,
                    isp VARCHAR(256) NOT NULL,
                    city VARCHAR(100) NOT NULL,
                    region VARCHAR(100) NOT NULL,
                    country VARCHAR(100) NOT NULL,

                    FOREIGN KEY (machine_id) REFERENCES machine_machine(id) ON DELETE CASCADE
                );
                CREATE INDEX IF NOT EXISTS machine_networkinterface_machine_id_idx ON machine_networkinterface(machine_id);

                -- Create machine_binary table
                CREATE TABLE IF NOT EXISTS machine_binary (
                    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,

                    machine_id TEXT NOT NULL,
                    name VARCHAR(63) NOT NULL,
                    binproviders VARCHAR(127) NOT NULL DEFAULT 'env',
                    overrides TEXT NOT NULL DEFAULT '{}',

                    binprovider VARCHAR(31) NOT NULL DEFAULT '',
                    abspath VARCHAR(255) NOT NULL DEFAULT '',
                    version VARCHAR(32) NOT NULL DEFAULT '',
                    sha256 VARCHAR(64) NOT NULL DEFAULT '',

                    status VARCHAR(16) NOT NULL DEFAULT 'queued',
                    retry_at DATETIME,
                    output_dir VARCHAR(255) NOT NULL DEFAULT '',

                    FOREIGN KEY (machine_id) REFERENCES machine_machine(id) ON DELETE CASCADE,
                    UNIQUE(machine_id, name, abspath, version, sha256)
                );
                CREATE INDEX IF NOT EXISTS machine_binary_machine_id_idx ON machine_binary(machine_id);
                CREATE INDEX IF NOT EXISTS machine_binary_name_idx ON machine_binary(name);
                CREATE INDEX IF NOT EXISTS machine_binary_status_idx ON machine_binary(status);
                CREATE INDEX IF NOT EXISTS machine_binary_retry_at_idx ON machine_binary(retry_at);

                -- Create machine_process table
                CREATE TABLE IF NOT EXISTS machine_process (
                    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,

                    machine_id TEXT NOT NULL,
                    binary_id TEXT,
                    network_interface_id TEXT,

                    cmd TEXT NOT NULL,
                    pwd VARCHAR(256),
                    env TEXT,
                    stdin TEXT,
                    timeout INTEGER NOT NULL DEFAULT 60,

                    pid INTEGER,
                    started_at DATETIME,
                    ended_at DATETIME,
                    exit_code INTEGER,
                    stdout TEXT NOT NULL DEFAULT '',
                    stderr TEXT NOT NULL DEFAULT '',

                    status VARCHAR(15) NOT NULL DEFAULT 'queued',
                    retry_at DATETIME,

                    FOREIGN KEY (machine_id) REFERENCES machine_machine(id) ON DELETE CASCADE,
                    FOREIGN KEY (binary_id) REFERENCES machine_binary(id) ON DELETE SET NULL,
                    FOREIGN KEY (network_interface_id) REFERENCES machine_networkinterface(id) ON DELETE SET NULL
                );
                CREATE INDEX IF NOT EXISTS machine_process_status_idx ON machine_process(status);
                CREATE INDEX IF NOT EXISTS machine_process_retry_at_idx ON machine_process(retry_at);
                CREATE INDEX IF NOT EXISTS machine_process_machine_id_idx ON machine_process(machine_id);
            """,
            # Reverse SQL
            reverse_sql="""
                DROP TABLE IF EXISTS machine_process;
                DROP TABLE IF EXISTS machine_binary;
                DROP TABLE IF EXISTS machine_networkinterface;
                DROP TABLE IF EXISTS machine_machine;
            """
        ),
    ]
