Refactor database interface and migration functions

This commit is contained in:
2025-11-30 14:21:04 +01:00
parent fec989fee2
commit 7272aa26ec
7 changed files with 75 additions and 528 deletions

View File

@@ -6,10 +6,6 @@ CREATE TABLE IF NOT EXISTS instances (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
-- Backend configuration
backend_type TEXT NOT NULL CHECK(backend_type IN ('llama_cpp', 'mlx_lm', 'vllm')),
backend_config_json TEXT NOT NULL, -- Backend-specific options (150+ fields for llama_cpp, etc.)
-- Instance state
status TEXT NOT NULL CHECK(status IN ('stopped', 'running', 'failed', 'restarting', 'shutting_down')) DEFAULT 'stopped',
@@ -17,27 +13,14 @@ CREATE TABLE IF NOT EXISTS instances (
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
-- Common instance options (extracted from Instance.Options)
-- NOT NULL with defaults to match config behavior (nil pointers use these defaults)
auto_restart INTEGER NOT NULL DEFAULT 0, -- Boolean: Enable automatic restart on failure
max_restarts INTEGER NOT NULL DEFAULT -1, -- Maximum restart attempts (-1 = unlimited)
restart_delay INTEGER NOT NULL DEFAULT 0, -- Delay between restarts in seconds
on_demand_start INTEGER NOT NULL DEFAULT 0, -- Boolean: Enable on-demand instance start
idle_timeout INTEGER NOT NULL DEFAULT 0, -- Idle timeout in minutes before auto-stop
docker_enabled INTEGER NOT NULL DEFAULT 0, -- Boolean: Run instance in Docker container
command_override TEXT, -- Custom command to override default backend command (nullable)
-- All instance options stored as a single JSON blob
options_json TEXT NOT NULL,
-- JSON fields for complex structures (nullable - empty when not set)
nodes TEXT, -- JSON array of node names for remote instances
environment TEXT, -- JSON map of environment variables
-- Future extensibility hook
owner_user_id TEXT NULL -- Future: OIDC user ID for ownership
);
-- Future: OIDC user ID for ownership
owner_user_id TEXT NULL
-- -----------------------------------------------------------------------------
-- Indexes for performance
-- -----------------------------------------------------------------------------
CREATE UNIQUE INDEX IF NOT EXISTS idx_instances_name ON instances(name);
CREATE INDEX IF NOT EXISTS idx_instances_status ON instances(status);
CREATE INDEX IF NOT EXISTS idx_instances_backend_type ON instances(backend_type);