diff options
| author | soryu <soryu@soryu.co> | 2026-02-03 22:01:29 +0000 |
|---|---|---|
| committer | soryu <soryu@soryu.co> | 2026-02-03 22:01:37 +0000 |
| commit | cf0a25af1d2834bfe6c5ea892ce5769936e5a673 (patch) | |
| tree | 476ba326ac1752281a441b5c17d2b3be4b23a2a9 /makima/migrations/20260203000000_create_chains.sql | |
| parent | 8361916ce67f3d2ba191ebf27cb50e79cb42e39c (diff) | |
| download | soryu-cf0a25af1d2834bfe6c5ea892ce5769936e5a673.tar.gz soryu-cf0a25af1d2834bfe6c5ea892ce5769936e5a673.zip | |
Add makima chain mechanism
Diffstat (limited to 'makima/migrations/20260203000000_create_chains.sql')
| -rw-r--r-- | makima/migrations/20260203000000_create_chains.sql | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/makima/migrations/20260203000000_create_chains.sql b/makima/migrations/20260203000000_create_chains.sql new file mode 100644 index 0000000..7811a45 --- /dev/null +++ b/makima/migrations/20260203000000_create_chains.sql @@ -0,0 +1,60 @@ +-- Chains table - DAG of contracts for multi-contract orchestration +-- Fits Makima's control theme - she controls through invisible chains +CREATE TABLE IF NOT EXISTS chains ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + owner_id UUID NOT NULL REFERENCES owners(id) ON DELETE CASCADE, + name VARCHAR(255) NOT NULL, + description TEXT, + status VARCHAR(32) NOT NULL DEFAULT 'active', -- active/completed/archived + -- Loop control for iterative execution + loop_enabled BOOLEAN NOT NULL DEFAULT false, + loop_max_iterations INTEGER DEFAULT 10, + loop_current_iteration INTEGER DEFAULT 0, + loop_progress_check TEXT, -- Prompt/criteria for evaluating progress + -- Repository reference (optional - contracts may have their own repos) + repository_url VARCHAR(512), + local_path VARCHAR(512), + -- Versioning for optimistic locking + version INTEGER NOT NULL DEFAULT 1, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX idx_chains_owner_id ON chains(owner_id); +CREATE INDEX idx_chains_status ON chains(status); + +-- Chain contracts - links contracts to chains with DAG dependency info +-- The depends_on array forms the DAG edges (directed acyclic graph) +CREATE TABLE IF NOT EXISTS chain_contracts ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + chain_id UUID NOT NULL REFERENCES chains(id) ON DELETE CASCADE, + contract_id UUID NOT NULL REFERENCES contracts(id) ON DELETE CASCADE, + -- DAG edges: contract IDs this contract depends on (must complete before this starts) + depends_on UUID[] DEFAULT '{}', + -- Order for display/processing (topological sort order) + order_index INTEGER NOT NULL DEFAULT 0, + -- Position for GUI editor + editor_x FLOAT DEFAULT 0, + editor_y FLOAT DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + UNIQUE(chain_id, contract_id) +); + +CREATE INDEX idx_chain_contracts_chain_id ON chain_contracts(chain_id); +CREATE INDEX idx_chain_contracts_contract_id ON chain_contracts(contract_id); + +-- Add chain_id to contracts table for reverse lookup +ALTER TABLE contracts ADD COLUMN IF NOT EXISTS chain_id UUID REFERENCES chains(id) ON DELETE SET NULL; +CREATE INDEX IF NOT EXISTS idx_contracts_chain_id ON contracts(chain_id); + +-- Chain events for audit trail +CREATE TABLE IF NOT EXISTS chain_events ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + chain_id UUID NOT NULL REFERENCES chains(id) ON DELETE CASCADE, + event_type VARCHAR(64) NOT NULL, -- created, contract_added, contract_completed, loop_iteration, completed + contract_id UUID REFERENCES contracts(id) ON DELETE SET NULL, + event_data JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX idx_chain_events_chain_id ON chain_events(chain_id); |
