-- Worker runtime support. -- -- container_aliases is the durable memory the background discovery job needs to -- be idempotent across restarts: reconciliation.ReconcileContainers must be able -- to compare the current runtime snapshot against the previous one to keep a -- stable entity identity across container recreation, and lifecycle event -- derivation must compare the previous observed state/health/restart count to -- decide whether anything actually changed. Both inputs are per runtime alias, -- not per entity, so they cannot be expressed with entity_aliases (which has no -- runtime identity or observation columns). -- -- A runtime alias that stops being observed is tombstoned, never deleted, which -- is what keeps a temporarily unhealthy source from erasing inventory. CREATE TABLE container_aliases ( source_id uuid NOT NULL REFERENCES data_sources(id) ON DELETE CASCADE, runtime_id text NOT NULL CHECK (length(runtime_id) BETWEEN 1 AND 255), entity_id uuid NOT NULL REFERENCES entities(id) ON DELETE CASCADE, name text NOT NULL CHECK (length(name) BETWEEN 1 AND 255), project text NOT NULL DEFAULT '' CHECK (length(project) <= 255), service text NOT NULL DEFAULT '' CHECK (length(service) <= 255), image_digest text NOT NULL DEFAULT '' CHECK (length(image_digest) <= 255), observed_state text NOT NULL DEFAULT '' CHECK (length(observed_state) <= 64), observed_health text NOT NULL DEFAULT '' CHECK (length(observed_health) <= 64), restart_count integer NOT NULL DEFAULT 0 CHECK (restart_count >= 0), intentional_stop boolean NOT NULL DEFAULT false, first_seen_at timestamptz NOT NULL, last_seen_at timestamptz NOT NULL, tombstoned_at timestamptz, PRIMARY KEY (source_id, runtime_id) ); CREATE INDEX container_aliases_entity_idx ON container_aliases (entity_id, last_seen_at DESC); CREATE INDEX container_aliases_active_idx ON container_aliases (source_id, last_seen_at DESC) WHERE tombstoned_at IS NULL; -- The system status endpoint reports each background job's last outcome by -- reading the newest job_runs row per job_type. job_runs_status_idx leads with -- status, so it cannot serve that lookup; this index can. CREATE INDEX job_runs_recent_idx ON job_runs (job_type, scheduled_at DESC, id ASC);