-- Remove container_id from apps table
-- Container is now looked up via Docker label (upaas.id) instead of stored in database

-- SQLite doesn't support DROP COLUMN before version 3.35.0 (2021-03-12)
-- Use table rebuild for broader compatibility

-- Create new table without container_id
CREATE TABLE apps_new (
    id TEXT PRIMARY KEY,
    name TEXT UNIQUE NOT NULL,
    repo_url TEXT NOT NULL,
    branch TEXT NOT NULL DEFAULT 'main',
    dockerfile_path TEXT DEFAULT 'Dockerfile',
    webhook_secret TEXT NOT NULL,
    ssh_private_key TEXT NOT NULL,
    ssh_public_key TEXT NOT NULL,
    image_id TEXT,
    status TEXT DEFAULT 'pending',
    docker_network TEXT,
    ntfy_topic TEXT,
    slack_webhook TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Copy data (excluding container_id)
INSERT INTO apps_new (
    id, name, repo_url, branch, dockerfile_path, webhook_secret,
    ssh_private_key, ssh_public_key, image_id, status,
    docker_network, ntfy_topic, slack_webhook, created_at, updated_at
)
SELECT
    id, name, repo_url, branch, dockerfile_path, webhook_secret,
    ssh_private_key, ssh_public_key, image_id, status,
    docker_network, ntfy_topic, slack_webhook, created_at, updated_at
FROM apps;

-- Drop old table and rename new one
DROP TABLE apps;
ALTER TABLE apps_new RENAME TO apps;

-- Recreate indexes
CREATE INDEX idx_apps_status ON apps(status);
CREATE INDEX idx_apps_webhook_secret ON apps(webhook_secret);
