Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions packages/app/migrations/0001_app_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE SCHEMA IF NOT EXISTS app;

CREATE TABLE IF NOT EXISTS app.schema_migrations (version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE app.tenants (id uuid PRIMARY KEY, name text, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE app.auth_users (id uuid PRIMARY KEY, email text NOT NULL UNIQUE, email_verified boolean NOT NULL DEFAULT false, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE app.auth_accounts (id uuid PRIMARY KEY, user_id uuid NOT NULL REFERENCES app.auth_users(id) ON DELETE CASCADE, provider text NOT NULL, provider_account_id text NOT NULL, password_hash text, UNIQUE(provider, provider_account_id));
CREATE TABLE app.auth_reset_tokens (digest text PRIMARY KEY, user_id uuid NOT NULL REFERENCES app.auth_users(id) ON DELETE CASCADE, expires_at timestamptz NOT NULL, used_at timestamptz);
CREATE TABLE app.auth_sessions (id uuid PRIMARY KEY, user_id uuid NOT NULL REFERENCES app.auth_users(id) ON DELETE CASCADE, expires_at timestamptz NOT NULL, revoked_at timestamptz);
CREATE TABLE app.members (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, user_id uuid NOT NULL REFERENCES app.auth_users(id) ON DELETE CASCADE, email text NOT NULL, role text NOT NULL CHECK (role IN ('owner','member')), session_version integer NOT NULL DEFAULT 1 CHECK (session_version > 0), created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,user_id));
CREATE TABLE app.access_tokens (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, id uuid NOT NULL, name text NOT NULL, digest text NOT NULL UNIQUE, user_id uuid NOT NULL, session_version integer NOT NULL, scopes jsonb NOT NULL DEFAULT '[]', created_at timestamptz NOT NULL DEFAULT now(), expires_at timestamptz NOT NULL, last_used_at timestamptz, revoked_at timestamptz, PRIMARY KEY (tenant_id,id), FOREIGN KEY (tenant_id,user_id) REFERENCES app.members(tenant_id,user_id) ON DELETE CASCADE);

CREATE TABLE app.connections (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, kind text NOT NULL, encrypted_value text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,kind));
CREATE TABLE app.github_installations (installation_id bigint PRIMARY KEY, tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, account_login text NOT NULL, account_type text NOT NULL, state text NOT NULL CHECK (state IN ('active','suspended')), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (tenant_id,installation_id));
CREATE TABLE app.github_setup_states (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, nonce text NOT NULL, expires_at timestamptz NOT NULL, used_at timestamptz, PRIMARY KEY (tenant_id,nonce));

CREATE TABLE app.jobs (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, id uuid NOT NULL, name text NOT NULL, slug text NOT NULL, encrypted_prompt_template text NOT NULL, encrypted_run_name_template text NOT NULL DEFAULT '', execution_target jsonb NOT NULL, concurrency_limit integer NOT NULL CHECK (concurrency_limit >= 1), enabled boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), UNIQUE (tenant_id,slug));
CREATE TABLE app.triggers (tenant_id uuid NOT NULL, id uuid NOT NULL, job_id uuid NOT NULL, kind text NOT NULL CHECK (kind IN ('manual','schedule','webhook','jobLifecycle')), slug text NOT NULL, enabled boolean NOT NULL DEFAULT true, config jsonb NOT NULL DEFAULT '{}', position integer NOT NULL DEFAULT 0, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), UNIQUE (tenant_id,job_id,slug), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE);
CREATE TABLE app.invocations (tenant_id uuid NOT NULL, id uuid NOT NULL, job_id uuid NOT NULL, source text NOT NULL CHECK (source IN ('manual','schedule','webhook','jobLifecycle')), claim_key text NOT NULL, trigger_id uuid NOT NULL, context jsonb NOT NULL, occurrence jsonb, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), UNIQUE (tenant_id,job_id,claim_key), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,trigger_id) REFERENCES app.triggers(tenant_id,id));
CREATE TABLE app.job_runs (tenant_id uuid NOT NULL, id uuid NOT NULL, job_id uuid NOT NULL, invocation_id uuid NOT NULL, state text NOT NULL CHECK (state IN ('queued','starting','running','blocked','stopping','succeeded','failed','stopped')), encrypted_prompt text NOT NULL, run_name text NOT NULL DEFAULT '', launch_lease_expires_at timestamptz, next_poll_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), started_at timestamptz, PRIMARY KEY (tenant_id,id), UNIQUE (tenant_id,invocation_id), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,invocation_id) REFERENCES app.invocations(tenant_id,id) ON DELETE CASCADE);
CREATE INDEX job_runs_queue ON app.job_runs (state,created_at) WHERE state='queued';
CREATE INDEX job_runs_poll_due ON app.job_runs (next_poll_at) WHERE state IN ('running','blocked','stopping');
CREATE INDEX job_runs_launch_lease ON app.job_runs (launch_lease_expires_at) WHERE state='starting';

CREATE TABLE app.runs (tenant_id uuid NOT NULL, id uuid NOT NULL, job_id uuid NOT NULL, invocation_id uuid NOT NULL, provider text NOT NULL, issue_id text NOT NULL, issue_url text, issue_title text NOT NULL DEFAULT '', run_name text NOT NULL DEFAULT '', agent_name text NOT NULL, workspace_name text NOT NULL DEFAULT '', agent_kind text NOT NULL DEFAULT '', state text NOT NULL, execution_backend_kind text NOT NULL DEFAULT '', execution_handle jsonb, execution_capabilities jsonb NOT NULL DEFAULT '[]', destination_url text, result text, output_captured boolean NOT NULL DEFAULT false, claim_released boolean NOT NULL DEFAULT false, vm_cleanup_attempt integer NOT NULL DEFAULT 0, cleanup_next_at timestamptz, vm_cleanup_complete boolean NOT NULL DEFAULT false, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), UNIQUE (tenant_id,invocation_id), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,invocation_id) REFERENCES app.invocations(tenant_id,id) ON DELETE CASCADE);
CREATE INDEX runs_order ON app.runs (tenant_id,created_at DESC,id DESC);
CREATE INDEX runs_state_order ON app.runs (tenant_id,state,created_at DESC,id DESC);
CREATE INDEX runs_cleanup_due ON app.runs (cleanup_next_at) WHERE NOT vm_cleanup_complete;
CREATE INDEX runs_issue_trgm ON app.runs USING gin (issue_id gin_trgm_ops);
CREATE INDEX runs_name_trgm ON app.runs USING gin (run_name gin_trgm_ops);
CREATE TABLE app.run_transcripts (tenant_id uuid NOT NULL, run_id uuid NOT NULL, transcript text NOT NULL, byte_size integer NOT NULL CHECK (byte_size >= 0), search_vector tsvector GENERATED ALWAYS AS (to_tsvector('english',transcript)) STORED, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,run_id), FOREIGN KEY (tenant_id,run_id) REFERENCES app.runs(tenant_id,id) ON DELETE CASCADE);
CREATE INDEX run_transcripts_search ON app.run_transcripts USING gin (search_vector);
CREATE TABLE app.run_activity (tenant_id uuid NOT NULL, id uuid NOT NULL, run_id uuid NOT NULL, action text NOT NULL, detail text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), FOREIGN KEY (tenant_id,run_id) REFERENCES app.runs(tenant_id,id) ON DELETE CASCADE);
CREATE TABLE app.active_claims (tenant_id uuid NOT NULL, job_id uuid NOT NULL, claim_key text NOT NULL, run_id uuid NOT NULL, expires_at timestamptz, PRIMARY KEY (tenant_id,job_id,claim_key), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,run_id) REFERENCES app.runs(tenant_id,id) ON DELETE CASCADE);

CREATE TABLE app.schedule_state (tenant_id uuid NOT NULL, trigger_id uuid NOT NULL, job_id uuid NOT NULL, next_run_at timestamptz, last_triggered_at timestamptz, PRIMARY KEY (tenant_id,trigger_id), FOREIGN KEY (tenant_id,trigger_id) REFERENCES app.triggers(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE);
CREATE INDEX schedules_due ON app.schedule_state (next_run_at) WHERE next_run_at IS NOT NULL;
CREATE TABLE app.automatic_wakes (tenant_id uuid NOT NULL, job_id uuid NOT NULL, trailing_pending boolean NOT NULL DEFAULT false, summary jsonb NOT NULL DEFAULT '{}', updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,job_id), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE);
CREATE TABLE app.lifecycle_deliveries (tenant_id uuid NOT NULL, trigger_id uuid NOT NULL, source_run_id uuid NOT NULL, terminal_state text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,trigger_id,source_run_id,terminal_state), FOREIGN KEY (tenant_id,trigger_id) REFERENCES app.triggers(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,source_run_id) REFERENCES app.runs(tenant_id,id) ON DELETE CASCADE);
CREATE TABLE app.job_events (tenant_id uuid NOT NULL, id uuid NOT NULL, job_id uuid NOT NULL, provider text NOT NULL, delivery_id text NOT NULL, outcome text NOT NULL, detail text NOT NULL, received_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE);
CREATE INDEX job_events_by_job ON app.job_events (tenant_id,job_id,received_at DESC);

CREATE TABLE app.webhook_deliveries (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, id uuid NOT NULL, provider text NOT NULL, delivery_id text NOT NULL, event_type text NOT NULL DEFAULT 'unknown', event_action text NOT NULL DEFAULT 'unknown', outcome text NOT NULL, detail text NOT NULL, received_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), UNIQUE (tenant_id,provider,delivery_id));
CREATE TABLE app.webhook_delivery_events (tenant_id uuid NOT NULL, id uuid NOT NULL, delivery_id uuid NOT NULL, job_id uuid, outcome text NOT NULL, detail text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (tenant_id,id), FOREIGN KEY (tenant_id,delivery_id) REFERENCES app.webhook_deliveries(tenant_id,id) ON DELETE CASCADE, FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE SET NULL);
CREATE TABLE app.pending_verifications (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, id uuid NOT NULL, job_id uuid, delivery_id text NOT NULL, installation_id bigint NOT NULL, repository_id bigint NOT NULL, repository_owner text NOT NULL, repository_name text NOT NULL, pull_number integer NOT NULL, attempt integer NOT NULL DEFAULT 0, next_attempt_at timestamptz NOT NULL, payload jsonb NOT NULL, PRIMARY KEY (tenant_id,id), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE);
CREATE INDEX pending_verifications_due ON app.pending_verifications (next_attempt_at);
CREATE TABLE app.tail_fingerprints (tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE, job_id uuid NOT NULL, fingerprint text NOT NULL, window_started_at timestamptz NOT NULL, count integer NOT NULL CHECK (count >= 0), PRIMARY KEY (tenant_id,job_id,fingerprint), FOREIGN KEY (tenant_id,job_id) REFERENCES app.jobs(tenant_id,id) ON DELETE CASCADE);
CREATE TABLE app.wake_hints (key text PRIMARY KEY DEFAULT 'global' CHECK (key='global'), not_before timestamptz NOT NULL, updated_at timestamptz NOT NULL DEFAULT now());

CREATE OR REPLACE FUNCTION app.next_wake_at() RETURNS timestamptz LANGUAGE sql STABLE AS $$
SELECT min(candidate) FROM (
SELECT min(not_before) candidate FROM app.wake_hints
UNION ALL SELECT min(next_run_at) FROM app.schedule_state
UNION ALL SELECT min(next_attempt_at) FROM app.pending_verifications
UNION ALL SELECT min(launch_lease_expires_at) FROM app.job_runs WHERE state='starting'
UNION ALL SELECT min(next_poll_at) FROM app.job_runs WHERE state IN ('running','blocked','stopping')
UNION ALL SELECT min(cleanup_next_at) FROM app.runs WHERE NOT vm_cleanup_complete
) wakes
$$;

INSERT INTO app.schema_migrations(version) VALUES ('0001_app_schema') ON CONFLICT DO NOTHING;
Loading
Loading