fix(database): B-1 schema integrity and migration fixes
- Fix migration 024 self-insert and bad column reference (F-B1-1, F-B1-2) Uses existing enabled/auto_run columns instead of non-existent deprecated - Abort server on migration failure instead of warning (F-B1-11) main.go now calls log.Fatalf, prints [INFO] only on success - Fix migration 018 scanner_config filename suffix (F-B1-3) Renumbered to 027 with .up.sql suffix - Remove GRANT to non-existent role in scanner_config (F-B1-4) - Resolve duplicate migration numbers 009 and 012 (F-B1-13) Renamed to 009b and 012b for unique lexical sorting - Add IF NOT EXISTS to all non-idempotent migrations (F-B1-15) Fixed: 011, 012, 017, 023, 023a - Replace N+1 dashboard stats loop with GetAllUpdateStats (F-B1-6) Single aggregate query replaces per-agent loop - Add composite index on agent_commands(status, sent_at) (F-B1-5) New migration 028 with partial index for timeout service - Add background refresh token cleanup goroutine (F-B1-10) 24-hour ticker calls CleanupExpiredTokens - ETHOS log format in migration runner (no emojis) All 55 tests pass (41 server + 14 agent). No regressions. See docs/B1_Fix_Implementation.md and DEV-025 through DEV-028. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
-- Agents table
|
||||
CREATE TABLE agents (
|
||||
CREATE TABLE IF NOT EXISTS agents (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
hostname VARCHAR(255) NOT NULL,
|
||||
os_type VARCHAR(50) NOT NULL CHECK (os_type IN ('windows', 'linux', 'macos')),
|
||||
@@ -16,12 +16,12 @@ CREATE TABLE agents (
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_agents_status ON agents(status);
|
||||
CREATE INDEX idx_agents_os_type ON agents(os_type);
|
||||
CREATE INDEX idx_agents_last_seen ON agents(last_seen);
|
||||
CREATE INDEX IF NOT EXISTS idx_agents_status ON agents(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_agents_os_type ON agents(os_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_agents_last_seen ON agents(last_seen);
|
||||
|
||||
-- Agent specs
|
||||
CREATE TABLE agent_specs (
|
||||
CREATE TABLE IF NOT EXISTS agent_specs (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
agent_id UUID REFERENCES agents(id) ON DELETE CASCADE,
|
||||
cpu_model VARCHAR(255),
|
||||
@@ -36,10 +36,10 @@ CREATE TABLE agent_specs (
|
||||
collected_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_agent_specs_agent_id ON agent_specs(agent_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_agent_specs_agent_id ON agent_specs(agent_id);
|
||||
|
||||
-- Update packages
|
||||
CREATE TABLE update_packages (
|
||||
CREATE TABLE IF NOT EXISTS update_packages (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
agent_id UUID REFERENCES agents(id) ON DELETE CASCADE,
|
||||
package_type VARCHAR(50) NOT NULL,
|
||||
@@ -63,14 +63,14 @@ CREATE TABLE update_packages (
|
||||
UNIQUE(agent_id, package_type, package_name, available_version)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_updates_status ON update_packages(status);
|
||||
CREATE INDEX idx_updates_agent ON update_packages(agent_id);
|
||||
CREATE INDEX idx_updates_severity ON update_packages(severity);
|
||||
CREATE INDEX idx_updates_package_type ON update_packages(package_type);
|
||||
CREATE INDEX idx_updates_composite ON update_packages(status, severity, agent_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_updates_status ON update_packages(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_updates_agent ON update_packages(agent_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_updates_severity ON update_packages(severity);
|
||||
CREATE INDEX IF NOT EXISTS idx_updates_package_type ON update_packages(package_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_updates_composite ON update_packages(status, severity, agent_id);
|
||||
|
||||
-- Update logs
|
||||
CREATE TABLE update_logs (
|
||||
CREATE TABLE IF NOT EXISTS update_logs (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
agent_id UUID REFERENCES agents(id) ON DELETE CASCADE,
|
||||
update_package_id UUID REFERENCES update_packages(id) ON DELETE SET NULL,
|
||||
@@ -83,21 +83,21 @@ CREATE TABLE update_logs (
|
||||
executed_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_logs_agent ON update_logs(agent_id);
|
||||
CREATE INDEX idx_logs_result ON update_logs(result);
|
||||
CREATE INDEX idx_logs_executed_at ON update_logs(executed_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_agent ON update_logs(agent_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_result ON update_logs(result);
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_executed_at ON update_logs(executed_at DESC);
|
||||
|
||||
-- Agent tags
|
||||
CREATE TABLE agent_tags (
|
||||
CREATE TABLE IF NOT EXISTS agent_tags (
|
||||
agent_id UUID REFERENCES agents(id) ON DELETE CASCADE,
|
||||
tag VARCHAR(100) NOT NULL,
|
||||
PRIMARY KEY (agent_id, tag)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_agent_tags_tag ON agent_tags(tag);
|
||||
CREATE INDEX IF NOT EXISTS idx_agent_tags_tag ON agent_tags(tag);
|
||||
|
||||
-- Users (for authentication)
|
||||
CREATE TABLE users (
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
username VARCHAR(255) UNIQUE NOT NULL,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
@@ -107,11 +107,11 @@ CREATE TABLE users (
|
||||
last_login TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_users_username ON users(username);
|
||||
CREATE INDEX idx_users_email ON users(email);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
|
||||
|
||||
-- Commands queue (for agent orchestration)
|
||||
CREATE TABLE agent_commands (
|
||||
CREATE TABLE IF NOT EXISTS agent_commands (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
agent_id UUID REFERENCES agents(id) ON DELETE CASCADE,
|
||||
command_type VARCHAR(50) NOT NULL,
|
||||
@@ -123,5 +123,5 @@ CREATE TABLE agent_commands (
|
||||
result JSONB
|
||||
);
|
||||
|
||||
CREATE INDEX idx_commands_agent_status ON agent_commands(agent_id, status);
|
||||
CREATE INDEX idx_commands_created_at ON agent_commands(created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_commands_agent_status ON agent_commands(agent_id, status);
|
||||
CREATE INDEX IF NOT EXISTS idx_commands_created_at ON agent_commands(created_at DESC);
|
||||
|
||||
Reference in New Issue
Block a user