feat(org): Postgres tenant tables, org-aware store and resumable default upgrade (archie-core-e5fn)

This commit is contained in:
sam committed 2026-09-26 11:43:10 +10:00
1 parent 9b581a1326
commit 643d2ed665
19 files changed
+1322 -94

No files matched your search

+2
View File
@@ -31,6 +31,8 @@ var (
ErrInvalidWorkspace = errors.New("org: invalid workspace")
ErrInvalidAssignment = errors.New("org: invalid agent assignment")
ErrOrgNotFound = errors.New("org: org not found")
ErrOrgExists = errors.New("org: org already exists")
ErrWorkspaceExists = errors.New("org: workspace already exists")
ErrMembershipExists = errors.New("org: membership already exists")
// ErrUpgradeIncomplete reports that the resumable default org/workspace
// upgrade has not finished (docs/prds/orgs-and-access.md, "Upgrading
@@ -0,0 +1,100 @@
-- Orgs and workspaces: the tenant boundary (docs/prds/orgs-and-access.md,
-- step 1). The default org/workspace upgrade itself is a resumable data
-- upgrade recorded in org_upgrades and run by the State Store at boot
-- (internal/infrastructure/postgres/orgs.go), not part of the schema.
--
-- IDs are caller-provided slugs: they name the tenant in logs, keys and
-- cross-store references, and no slugs-as-uuids indirection is needed.
-- org_id/workspace_id columns here default to 'default': every writer that
-- is still org-unaware (the daemon, the binding editor) writes records of
-- the default org, which is exactly a single-operator install's semantics.
-- The default upgrade stamps any empty value left over and records the
-- phase ledger.
-- +goose Up
CREATE TABLE orgs (
id text PRIMARY KEY,
name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE workspaces (
id text NOT NULL,
org_id text NOT NULL REFERENCES orgs(id),
name text NOT NULL,
environment text NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (org_id, id)
);
-- A membership gives an identity a role in an org, or in one workspace of it
-- when workspace_id is set. Unique on the coalesced workspace so an org-wide
-- membership cannot be duplicated: Postgres treats NULLs as distinct in a
-- unique index, so the PK cannot carry that rule.
CREATE TABLE memberships (
identity_id text NOT NULL REFERENCES identities(id),
org_id text NOT NULL REFERENCES orgs(id),
workspace_id text,
role text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX idx_memberships_identity ON memberships
(identity_id, org_id, COALESCE(workspace_id, ''));
-- An agent or service identity is assigned to exactly one org.
CREATE TABLE org_agents (
identity_id text PRIMARY KEY REFERENCES identities(id),
org_id text NOT NULL REFERENCES orgs(id),
created_at timestamptz NOT NULL DEFAULT now()
);
-- org_upgrades records that a phase of the resumable default org/workspace
-- upgrade committed. Written in the same transaction as the phase's stamping,
-- so a phase is either recorded or entirely absent.
CREATE TABLE org_upgrades (
phase text PRIMARY KEY CHECK (phase IN ('state_store', 'edastore')),
completed_at timestamptz NOT NULL DEFAULT now()
);
-- org_id and workspace_id on every owned record. The control-plane resource
-- documents are org-owned, not workspace-owned; run events and transitions
-- are workspace-owned children of runs (tasks).
ALTER TABLE tasks ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE tasks ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE events ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE events ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE transitions ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE transitions ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE resources ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE resource_history ADD COLUMN org_id text NOT NULL DEFAULT 'default';
-- Task uniqueness moves from (owner, repo, issue_number) to the record's
-- identity: two orgs may work the same issue under their own identities.
-- The migration backfill gives every existing row the same org, so the new
-- constraint holds wherever the old one did.
ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_owner_repo_issue_number_key;
ALTER TABLE tasks ADD CONSTRAINT tasks_org_identity_owner_repo_number_key
UNIQUE (org_id, identity, owner, repo, issue_number);
-- +goose Down
DROP INDEX idx_memberships_identity;
DROP TABLE org_agents;
DROP TABLE memberships;
DROP TABLE workspaces;
DROP TABLE orgs;
DROP TABLE org_upgrades;
ALTER TABLE tasks DROP CONSTRAINT IF EXISTS tasks_org_identity_owner_repo_number_key;
ALTER TABLE tasks ADD CONSTRAINT tasks_owner_repo_issue_number_key UNIQUE (owner, repo, issue_number);
ALTER TABLE resource_history DROP COLUMN org_id;
ALTER TABLE resources DROP COLUMN org_id;
ALTER TABLE transitions DROP COLUMN workspace_id;
ALTER TABLE transitions DROP COLUMN org_id;
ALTER TABLE events DROP COLUMN workspace_id;
ALTER TABLE events DROP COLUMN org_id;
ALTER TABLE tasks DROP COLUMN workspace_id;
ALTER TABLE tasks DROP COLUMN org_id;
@@ -0,0 +1,36 @@
-- org_id and workspace_id on the event-capture records (the second phase of
-- the tenant boundary; the State Store tables carried them in 0012). Sources,
-- event types, mappings, bindings, captures and the tool_calls transcript are
-- workspace-owned; the dispatch ledgers are children of those records and
-- carry none. Like 0012, the default backfill is what an org-unaware writer
-- produces (docs/prds/orgs-and-access.md, step 1).
-- +goose Up
ALTER TABLE bindings ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE bindings ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE mappings ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE mappings ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE captures ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE captures ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE sources ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE sources ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE event_types ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE event_types ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
ALTER TABLE tool_calls ADD COLUMN org_id text NOT NULL DEFAULT 'default';
ALTER TABLE tool_calls ADD COLUMN workspace_id text NOT NULL DEFAULT 'default';
-- +goose Down
ALTER TABLE tool_calls DROP COLUMN workspace_id;
ALTER TABLE tool_calls DROP COLUMN org_id;
ALTER TABLE event_types DROP COLUMN workspace_id;
ALTER TABLE event_types DROP COLUMN org_id;
ALTER TABLE sources DROP COLUMN workspace_id;
ALTER TABLE sources DROP COLUMN org_id;
ALTER TABLE captures DROP COLUMN workspace_id;
ALTER TABLE captures DROP COLUMN org_id;
ALTER TABLE mappings DROP COLUMN workspace_id;
ALTER TABLE mappings DROP COLUMN org_id;
ALTER TABLE bindings DROP COLUMN workspace_id;
ALTER TABLE bindings DROP COLUMN org_id;
+240
View File
@@ -0,0 +1,240 @@
// Orgs, workspaces, memberships and agent assignments: the Postgres
// implementation of internal/domain/org's contracts, plus the resumable
// default org/workspace upgrade the State Store runs at boot
// (docs/prds/orgs-and-access.md, "Upgrading existing installs").
//
// The upgrade refuses nothing itself; the refusal to serve scoped calls
// until it has finished is enforced by the boot order -- the State Store
// runs this upgrade before its gRPC listener starts, and a failed upgrade
// fails the boot (fail closed) rather than serving records with no org.
package postgres
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
var (
_ org.Repository = (*Store)(nil)
_ org.Upgrader = (*Store)(nil)
)
// orgFromRow maps the generated org row to the domain org.
func orgFromRow(o postgresdb.Org) org.Org {
return org.Org{ID: org.OrgID(o.ID), Name: o.Name}
}
// CreateOrg inserts a new org; re-creating an existing ID is ErrOrgExists.
func (s *Store) CreateOrg(ctx context.Context, value org.Org) (org.Org, error) {
if err := value.Validate(); err != nil {
return org.Org{}, err
}
if err := s.queries().InsertOrg(ctx, postgresdb.InsertOrgParams{
ID: string(value.ID), Name: value.Name,
}); err != nil {
if isUniqueViolation(err) {
return org.Org{}, fmt.Errorf("%w: %s", org.ErrOrgExists, value.ID)
}
return org.Org{}, err
}
return value, nil
}
// GetOrg returns one org by ID, or ErrOrgNotFound.
func (s *Store) GetOrg(ctx context.Context, id org.OrgID) (org.Org, error) {
o, err := s.queries().GetOrg(ctx, string(id))
if errors.Is(err, pgx.ErrNoRows) {
return org.Org{}, org.ErrOrgNotFound
}
if err != nil {
return org.Org{}, err
}
return orgFromRow(o), nil
}
// ListOrgs returns every org, oldest first.
func (s *Store) ListOrgs(ctx context.Context) ([]org.Org, error) {
rows, err := s.queries().ListOrgs(ctx)
if err != nil {
return nil, err
}
out := make([]org.Org, 0, len(rows))
for _, r := range rows {
out = append(out, orgFromRow(r))
}
return out, nil
}
// CreateWorkspace inserts a workspace into an existing org; an unknown org is
// ErrOrgNotFound.
func (s *Store) CreateWorkspace(ctx context.Context, value org.Workspace) (org.Workspace, error) {
if err := value.Validate(); err != nil {
return org.Workspace{}, err
}
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
return org.Workspace{}, err
}
if err := s.queries().InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{
ID: string(value.ID), OrgID: string(value.OrgID),
Name: value.Name, Environment: value.Environment,
}); err != nil {
if isUniqueViolation(err) {
return org.Workspace{}, fmt.Errorf("%w: %s/%s", org.ErrWorkspaceExists, value.OrgID, value.ID)
}
return org.Workspace{}, err
}
return value, nil
}
// workspaceFromRow maps the generated workspace row to the domain workspace.
func workspaceFromRow(w postgresdb.Workspace) org.Workspace {
return org.Workspace{
ID: org.WorkspaceID(w.ID), OrgID: org.OrgID(w.OrgID),
Name: w.Name, Environment: w.Environment,
}
}
// ListWorkspaces returns an org's workspaces, oldest first.
func (s *Store) ListWorkspaces(ctx context.Context, id org.OrgID) ([]org.Workspace, error) {
rows, err := s.queries().ListWorkspaces(ctx, string(id))
if err != nil {
return nil, err
}
out := make([]org.Workspace, 0, len(rows))
for _, r := range rows {
out = append(out, workspaceFromRow(r))
}
return out, nil
}
// EnsureMembership grants an identity a role in an org, or in one workspace
// of it; an identical existing membership is not an error.
func (s *Store) EnsureMembership(ctx context.Context, value org.Membership) error {
if err := value.Validate(); err != nil {
return err
}
var workspace pgtype.Text
if value.WorkspaceID != "" {
workspace = pgtype.Text{String: string(value.WorkspaceID), Valid: true}
}
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
return err
}
return s.queries().EnsureMembership(ctx, postgresdb.EnsureMembershipParams{
IdentityID: string(value.IdentityID),
OrgID: string(value.OrgID),
WorkspaceID: workspace,
Role: string(value.Role),
})
}
// AssignAgent records the one org an agent or service identity serves,
// replacing any prior assignment for that identity.
func (s *Store) AssignAgent(ctx context.Context, value org.AgentAssignment) error {
if err := value.Validate(); err != nil {
return err
}
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
return err
}
return s.queries().UpsertAgentAssignment(ctx, postgresdb.UpsertAgentAssignmentParams{
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID),
})
}
// OrgForIdentity returns the org an identity serves: the assigned org for an
// agent, the org of one of its memberships for a person, and the default org
// for an identity in no org.
func (s *Store) OrgForIdentity(ctx context.Context, id identity.IdentityID) (org.OrgID, error) {
resolved, err := s.queries().ResolveIdentityOrg(ctx, string(id))
if err != nil {
return "", err
}
return org.OrgID(resolved), nil
}
// UpgradeDefaultOrg runs the resumable default org/workspace upgrade: the
// State Store phase first, the event-capture phase second, each in one
// transaction with its ledger row so a phase is either recorded or entirely
// absent, and each skipped on a later start once recorded.
func (s *Store) UpgradeDefaultOrg(ctx context.Context) error {
type phase struct {
name string
stamps []func(context.Context, *postgresdb.Queries) (int64, error)
}
phases := []phase{
{name: "state_store", stamps: []func(context.Context, *postgresdb.Queries) (int64, error){
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampStateStoreTasks(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampStateStoreEvents(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) {
return q.StampStateStoreTransitions(ctx)
},
func(ctx context.Context, q *postgresdb.Queries) (int64, error) {
return q.StampStateStoreResources(ctx)
},
func(ctx context.Context, q *postgresdb.Queries) (int64, error) {
return q.StampStateStoreResourceHistory(ctx)
},
}},
{name: "edastore", stamps: []func(context.Context, *postgresdb.Queries) (int64, error){
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreBindings(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreMappings(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreCaptures(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreSources(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreEventTypes(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreToolCalls(ctx) },
}},
}
for _, p := range phases {
if _, err := s.queries().GetOrgUpgradePhase(ctx, p.name); err == nil {
continue // recorded: the phase committed on an earlier start
} else if !errors.Is(err, pgx.ErrNoRows) {
return err
}
if err := s.runUpgradePhase(ctx, p.name, p.stamps); err != nil {
return fmt.Errorf("org upgrade phase %s: %w", p.name, err)
}
}
return nil
}
// runUpgradePhase stamps one phase's tables and records its ledger row in one
// transaction, after making sure the default org and workspace exist and
// every agent identity belongs to the default org. An interrupted phase
// leaves no ledger row, so the next start re-runs it from the beginning --
// every stamp is idempotent on rows the upgrade has not covered yet.
func (s *Store) runUpgradePhase(ctx context.Context, name string, stamps []func(context.Context, *postgresdb.Queries) (int64, error)) error {
tx, err := s.pool.Begin(ctx)
if err != nil {
return err
}
defer func() { _ = tx.Rollback(ctx) }()
q := postgresdb.New(tx)
if err := q.EnsureDefaultOrg(ctx); err != nil {
return err
}
if err := q.EnsureDefaultWorkspace(ctx); err != nil {
return err
}
if err := q.AssignDefaultOrgIdentities(ctx); err != nil {
return err
}
for _, stamp := range stamps {
if _, err := stamp(ctx, q); err != nil {
return err
}
}
if err := q.InsertOrgUpgradePhase(ctx, name); err != nil {
return err
}
return tx.Commit(ctx)
}
@@ -0,0 +1,183 @@
package postgres
import (
"errors"
"testing"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// newStoreForOrg is a migrated pool with the Store built over it.
func newStoreForOrg(t *testing.T) *Store {
t.Helper()
pool, _ := migrated(t)
return New(pool)
}
// seedAgent inserts one bot identity to assign.
func seedAgent(t *testing.T, s *Store, name string) identity.Identity {
t.Helper()
value, err := identity.New(identity.StableID(name), identity.KindBot, name)
if err != nil {
t.Fatalf("identity.New(%s): %v", name, err)
}
created, err := s.Create(t.Context(), value, identity.Audit{
ActorID: identity.SystemID, Source: "test", RequestID: "seed:" + name,
})
if err != nil {
t.Fatalf("Create identity %s: %v", name, err)
}
return created
}
func TestOrgRepository(t *testing.T) {
s := newStoreForOrg(t)
ctx := t.Context()
if _, err := s.CreateOrg(ctx, org.Org{ID: "soc", Name: "SOC"}); err != nil {
t.Fatalf("CreateOrg: %v", err)
}
if _, err := s.CreateOrg(ctx, org.Org{ID: "soc", Name: "dup"}); !errors.Is(err, org.ErrOrgExists) {
t.Fatalf("duplicate CreateOrg = %v, want ErrOrgExists", err)
}
if _, err := s.CreateOrg(ctx, org.Org{ID: "bad", Name: " "}); !errors.Is(err, org.ErrInvalidOrg) {
t.Fatalf("blank-name CreateOrg = %v, want ErrInvalidOrg", err)
}
got, err := s.GetOrg(ctx, "soc")
if err != nil {
t.Fatalf("GetOrg: %v", err)
}
if got.Name != "SOC" {
t.Fatalf("GetOrg = %+v, want name SOC", got)
}
if _, err := s.GetOrg(ctx, "missing"); !errors.Is(err, org.ErrOrgNotFound) {
t.Fatalf("GetOrg(missing) = %v, want ErrOrgNotFound", err)
}
ws, err := s.CreateWorkspace(ctx, org.Workspace{ID: "net", OrgID: "soc", Name: "networking-prd", Environment: "production"})
if err != nil {
t.Fatalf("CreateWorkspace: %v", err)
}
if _, err := s.CreateWorkspace(ctx, ws); !errors.Is(err, org.ErrWorkspaceExists) {
t.Fatalf("duplicate CreateWorkspace = %v, want ErrWorkspaceExists", err)
}
if _, err := s.CreateWorkspace(ctx, org.Workspace{ID: "x", OrgID: "missing", Name: "x"}); !errors.Is(err, org.ErrOrgNotFound) {
t.Fatalf("CreateWorkspace(unknown org) = %v, want ErrOrgNotFound", err)
}
workspaces, err := s.ListWorkspaces(ctx, "soc")
if err != nil || len(workspaces) != 1 || workspaces[0].ID != "net" {
t.Fatalf("ListWorkspaces = %+v, %v; want one net", workspaces, err)
}
member := seedAgent(t, s, "responder")
if err := s.EnsureMembership(ctx, org.Membership{IdentityID: member.ID, OrgID: "soc", Role: org.RoleOwner}); err != nil {
t.Fatalf("EnsureMembership: %v", err)
}
if err := s.EnsureMembership(ctx, org.Membership{IdentityID: member.ID, OrgID: "soc", Role: org.RoleOwner}); err != nil {
t.Fatalf("repeated EnsureMembership: %v", err)
}
if err := s.EnsureMembership(ctx, org.Membership{OrgID: "soc", Role: org.RoleOwner}); !errors.Is(err, org.ErrInvalidMembership) {
t.Fatalf("EnsureMembership(no identity) = %v, want ErrInvalidMembership", err)
}
missing := seedAgent(t, s, "ghost")
if err := s.EnsureMembership(ctx, org.Membership{IdentityID: missing.ID, OrgID: "missing", Role: org.RoleOwner}); !errors.Is(err, org.ErrOrgNotFound) {
t.Fatalf("EnsureMembership(unknown org) = %v, want ErrOrgNotFound", err)
}
agent := seedAgent(t, s, "archie")
if err := s.AssignAgent(ctx, org.AgentAssignment{IdentityID: agent.ID, OrgID: "soc"}); err != nil {
t.Fatalf("AssignAgent: %v", err)
}
if err := s.AssignAgent(ctx, org.AgentAssignment{IdentityID: agent.ID, OrgID: "missing"}); !errors.Is(err, org.ErrOrgNotFound) {
t.Fatalf("AssignAgent(unknown org) = %v, want ErrOrgNotFound", err)
}
resolved, err := s.OrgForIdentity(ctx, agent.ID)
if err != nil || resolved != "soc" {
t.Fatalf("OrgForIdentity(agent) = %s, %v; want soc", resolved, err)
}
// An identity with only an org-wide membership resolves through it.
// A person with only an org-wide membership resolves through it.
person := seedAgent(t, s, "operator")
if err := s.EnsureMembership(ctx, org.Membership{IdentityID: person.ID, OrgID: "soc", Role: org.RoleOwner}); err != nil {
t.Fatalf("EnsureMembership: %v", err)
}
resolved, err = s.OrgForIdentity(ctx, person.ID)
if err != nil || resolved != "soc" {
t.Fatalf("OrgForIdentity(member) = %s, %v; want soc", resolved, err)
}
// An identity in no org resolves to the default org.
resolved, err = s.OrgForIdentity(ctx, "nobody")
if err != nil || resolved != org.DefaultOrgID {
t.Fatalf("OrgForIdentity(no org) = %s, %v; want default", resolved, err)
}
}
func TestUpgradeDefaultOrg(t *testing.T) {
s := newStoreForOrg(t)
ctx := t.Context()
// Records written before the upgrade: one with an empty org (the shape a
// pre-column row lands in), one stamped by the default backfill.
agent := seedAgent(t, s, "archie")
if err := s.queries().InsertOrgUpgradePhase(ctx, "state_store"); err != nil {
t.Fatalf("pre-insert ledger row: %v", err)
}
// First run: completes both phases and is recorded.
if err := s.UpgradeDefaultOrg(ctx); err != nil {
t.Fatalf("UpgradeDefaultOrg: %v", err)
}
for _, phase := range []string{"state_store", "edastore"} {
if _, err := s.queries().GetOrgUpgradePhase(ctx, phase); err != nil {
t.Fatalf("phase %s not recorded: %v", phase, err)
}
}
if _, err := s.GetOrg(ctx, org.DefaultOrgID); err != nil {
t.Fatalf("default org missing after upgrade: %v", err)
}
workspaces, err := s.ListWorkspaces(ctx, org.DefaultOrgID)
if err != nil || len(workspaces) != 1 || workspaces[0].ID != org.DefaultWorkspaceID {
t.Fatalf("default workspace missing after upgrade: %+v, %v", workspaces, err)
}
resolved, err := s.OrgForIdentity(ctx, agent.ID)
if err != nil || resolved != org.DefaultOrgID {
t.Fatalf("agent org after upgrade = %s, %v; want default", resolved, err)
}
// A second run is a no-op: recorded phases are never re-run.
if err := s.UpgradeDefaultOrg(ctx); err != nil {
t.Fatalf("second UpgradeDefaultOrg: %v", err)
}
}
func TestUpgradeStampsEmptyOrg(t *testing.T) {
pool, _ := migrated(t)
s := New(pool)
ctx := t.Context()
// Rows with an empty org, as a write between migration and upgrade would
// leave them: one on each phase's side of the ledger.
for _, stmt := range []string{
"INSERT INTO resources (kind, value, version, updated_at, org_id) VALUES ('workflow:legacy', 'v', 1, now(), '')",
"INSERT INTO bindings (id, name, source, secret) VALUES ('b1', 'b', 'src', '')",
"UPDATE bindings SET org_id = '' WHERE id = 'b1'",
} {
if _, err := pool.Exec(ctx, stmt); err != nil {
t.Fatalf("seed %q: %v", stmt, err)
}
}
if err := s.UpgradeDefaultOrg(ctx); err != nil {
t.Fatalf("UpgradeDefaultOrg: %v", err)
}
var stamped string
if err := pool.QueryRow(ctx, "SELECT org_id FROM resources WHERE kind = 'workflow:legacy'").Scan(&stamped); err != nil || stamped != "default" {
t.Fatalf("resource org after upgrade = %q, %v; want default", stamped, err)
}
if err := pool.QueryRow(ctx, "SELECT org_id, workspace_id FROM bindings WHERE id = 'b1'").Scan(&stamped, &stamped); err != nil || stamped != "default" {
t.Fatalf("binding org/workspace after upgrade = %q, %v; want default", stamped, err)
}
}
@@ -142,7 +142,7 @@ func (q *Queries) DeriveSources(ctx context.Context) error {
}
const eventTypesForSource = `-- name: EventTypesForSource :many
SELECT id, source, name, rule, schema, created_at, updated_at
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types WHERE source = $1 ORDER BY name
`
@@ -163,6 +163,8 @@ func (q *Queries) EventTypesForSource(ctx context.Context, source string) ([]Eve
&i.Schema,
&i.CreatedAt,
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -223,9 +225,19 @@ SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE id = $1
`
func (q *Queries) GetEventType(ctx context.Context, id string) (EventType, error) {
type GetEventTypeRow struct {
ID string
Source string
Name string
Rule string
Schema string
CreatedAt time.Time
UpdatedAt time.Time
}
func (q *Queries) GetEventType(ctx context.Context, id string) (GetEventTypeRow, error) {
row := q.db.QueryRow(ctx, getEventType, id)
var i EventType
var i GetEventTypeRow
err := row.Scan(
&i.ID,
&i.Source,
@@ -239,7 +251,7 @@ func (q *Queries) GetEventType(ctx context.Context, id string) (EventType, error
}
const getMapping = `-- name: GetMapping :one
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type,
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE id = $1
@@ -262,6 +274,8 @@ func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, err
&i.Mapping.CreatedAt,
&i.Mapping.UpdatedAt,
&i.Mapping.EventType,
&i.Mapping.OrgID,
&i.Mapping.WorkspaceID,
&i.MatchCount,
&i.LastMatchedAt,
)
@@ -269,7 +283,7 @@ func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, err
}
const getSource = `-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at FROM sources WHERE path = $1
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1
`
func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
@@ -281,6 +295,8 @@ func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
&i.Secret,
&i.CreatedAt,
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
@@ -565,7 +581,7 @@ func (q *Queries) ListBindings(ctx context.Context) ([]ListBindingsRow, error) {
}
const listCaptures = `-- name: ListCaptures :many
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id
FROM captures
ORDER BY received_at DESC
LIMIT $1
@@ -591,6 +607,8 @@ func (q *Queries) ListCaptures(ctx context.Context, limit int32) ([]Capture, err
&i.ReceivedAt,
&i.Unsigned,
&i.EventType,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -603,7 +621,7 @@ func (q *Queries) ListCaptures(ctx context.Context, limit int32) ([]Capture, err
}
const listEventTypes = `-- name: ListEventTypes :many
SELECT id, source, name, rule, schema, created_at, updated_at
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types ORDER BY source, name
`
@@ -624,6 +642,8 @@ func (q *Queries) ListEventTypes(ctx context.Context) ([]EventType, error) {
&i.Schema,
&i.CreatedAt,
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -636,7 +656,7 @@ func (q *Queries) ListEventTypes(ctx context.Context) ([]EventType, error) {
}
const listMappings = `-- name: ListMappings :many
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type,
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings ORDER BY created_at DESC
@@ -665,6 +685,8 @@ func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
&i.Mapping.CreatedAt,
&i.Mapping.UpdatedAt,
&i.Mapping.EventType,
&i.Mapping.OrgID,
&i.Mapping.WorkspaceID,
&i.MatchCount,
&i.LastMatchedAt,
); err != nil {
@@ -679,7 +701,7 @@ func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
}
const listSources = `-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at FROM sources ORDER BY created_at DESC, path
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path
`
func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
@@ -697,6 +719,8 @@ func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
&i.Secret,
&i.CreatedAt,
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -709,7 +733,7 @@ func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
}
const listUndispatchedCaptures = `-- name: ListUndispatchedCaptures :many
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id
FROM captures c
WHERE c.source = ANY($1::text[])
AND c.event_type <> ''
@@ -750,6 +774,8 @@ func (q *Queries) ListUndispatchedCaptures(ctx context.Context, arg ListUndispat
&i.ReceivedAt,
&i.Unsigned,
&i.EventType,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -67,7 +67,7 @@ func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64
}
const listEventsAfter = `-- name: ListEventsAfter :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
FROM events
WHERE at > $1 OR (at = $1 AND id > $2)
ORDER BY at, id
@@ -104,6 +104,8 @@ func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams
&i.PrincipalID,
&i.Detail,
&i.Data,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -116,7 +118,7 @@ func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams
}
const listEventsFromBeginning = `-- name: ListEventsFromBeginning :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
FROM events
ORDER BY at, id
LIMIT $1
@@ -146,6 +148,8 @@ func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]E
&i.PrincipalID,
&i.Detail,
&i.Data,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -203,7 +207,7 @@ func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
}
const taskEventsByID = `-- name: TaskEventsByID :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
FROM events
WHERE task_id = $1
ORDER BY id
@@ -233,6 +237,8 @@ func (q *Queries) TaskEventsByID(ctx context.Context, taskID int64) ([]Event, er
&i.PrincipalID,
&i.Detail,
&i.Data,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -6,6 +6,8 @@ package postgresdb
import (
"time"
"github.com/jackc/pgx/v5/pgtype"
)
type ApplyStatus struct {
@@ -17,21 +19,23 @@ type ApplyStatus struct {
}
type Binding struct {
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
Secret string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
Secret string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
WorkspaceID string
}
type BindingDispatch struct {
@@ -53,6 +57,8 @@ type Capture struct {
ReceivedAt time.Time
Unsigned bool
EventType string
OrgID string
WorkspaceID string
}
type ChannelStatus struct {
@@ -87,16 +93,20 @@ type Event struct {
PrincipalID string
Detail string
Data string
OrgID string
WorkspaceID string
}
type EventType struct {
ID string
Source string
Name string
Rule string
Schema string
CreatedAt time.Time
UpdatedAt time.Time
ID string
Source string
Name string
Rule string
Schema string
CreatedAt time.Time
UpdatedAt time.Time
OrgID string
WorkspaceID string
}
type Identity struct {
@@ -153,13 +163,15 @@ type InstalledPackageRequirement struct {
}
type Mapping struct {
ID string
Name string
SourceHint string
Fields string
CreatedAt time.Time
UpdatedAt time.Time
EventType string
ID string
Name string
SourceHint string
Fields string
CreatedAt time.Time
UpdatedAt time.Time
EventType string
OrgID string
WorkspaceID string
}
type MappingMatch struct {
@@ -168,6 +180,14 @@ type MappingMatch struct {
MatchedAt time.Time
}
type Membership struct {
IdentityID string
OrgID string
WorkspaceID pgtype.Text
Role string
CreatedAt time.Time
}
type Message struct {
ID int64
MessageID string
@@ -181,6 +201,24 @@ type Message struct {
Search interface{}
}
type Org struct {
ID string
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
type OrgAgent struct {
IdentityID string
OrgID string
CreatedAt time.Time
}
type OrgUpgrade struct {
Phase string
CompletedAt time.Time
}
type PlaybookDispatch struct {
PlaybookID string
PlaybookVersion string
@@ -194,6 +232,7 @@ type Resource struct {
Value []byte
Version int64
UpdatedAt time.Time
OrgID string
}
type ResourceHistory struct {
@@ -207,6 +246,7 @@ type ResourceHistory struct {
ExpectedVersion int64
CurrentVersion int64
At time.Time
OrgID string
}
type Session struct {
@@ -223,11 +263,13 @@ type Session struct {
}
type Source struct {
Path string
Signing string
Secret string
CreatedAt time.Time
UpdatedAt time.Time
Path string
Signing string
Secret string
CreatedAt time.Time
UpdatedAt time.Time
OrgID string
WorkspaceID string
}
type SysAudit struct {
@@ -279,27 +321,33 @@ type Task struct {
UpdatedAt time.Time
ReviewCursor int64
Inputs string
OrgID string
WorkspaceID string
}
type ToolCall struct {
ID string
TaskID int64
Attempt int64
Tool string
Args string
Result string
Error string
DurationMs int64
CalledAt time.Time
ID string
TaskID int64
Attempt int64
Tool string
Args string
Result string
Error string
DurationMs int64
CalledAt time.Time
OrgID string
WorkspaceID string
}
type Transition struct {
ID int64
TaskID int64
At time.Time
FromStatus string
ToStatus string
Detail string
ID int64
TaskID int64
At time.Time
FromStatus string
ToStatus string
Detail string
OrgID string
WorkspaceID string
}
type Turn struct {
@@ -318,3 +366,12 @@ type Turn struct {
CreatedAt int64
UpdatedAt int64
}
type Workspace struct {
ID string
OrgID string
Name string
Environment string
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -0,0 +1,216 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: orgs.sql
package postgresdb
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const ensureMembership = `-- name: EnsureMembership :exec
INSERT INTO memberships (identity_id, org_id, workspace_id, role)
VALUES ($1, $2, $3, $4)
ON CONFLICT DO NOTHING
`
type EnsureMembershipParams struct {
IdentityID string
OrgID string
WorkspaceID pgtype.Text
Role string
}
func (q *Queries) EnsureMembership(ctx context.Context, arg EnsureMembershipParams) error {
_, err := q.db.Exec(ctx, ensureMembership,
arg.IdentityID,
arg.OrgID,
arg.WorkspaceID,
arg.Role,
)
return err
}
const getOrg = `-- name: GetOrg :one
SELECT id, name, created_at, updated_at FROM orgs WHERE id = $1
`
func (q *Queries) GetOrg(ctx context.Context, id string) (Org, error) {
row := q.db.QueryRow(ctx, getOrg, id)
var i Org
err := row.Scan(
&i.ID,
&i.Name,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const insertOrg = `-- name: InsertOrg :exec
INSERT INTO orgs (id, name) VALUES ($1, $2)
`
type InsertOrgParams struct {
ID string
Name string
}
// Orgs, workspaces, memberships and agent assignments: the durable half of
// the tenant boundary contract (internal/domain/org). IDs are caller-provided
// slugs; workspaces are unique per org, memberships per identity+org+workspace
// (schema 0012), and an agent assignment is unique per identity.
func (q *Queries) InsertOrg(ctx context.Context, arg InsertOrgParams) error {
_, err := q.db.Exec(ctx, insertOrg, arg.ID, arg.Name)
return err
}
const insertWorkspace = `-- name: InsertWorkspace :exec
INSERT INTO workspaces (id, org_id, name, environment) VALUES ($1, $2, $3, $4)
`
type InsertWorkspaceParams struct {
ID string
OrgID string
Name string
Environment string
}
func (q *Queries) InsertWorkspace(ctx context.Context, arg InsertWorkspaceParams) error {
_, err := q.db.Exec(ctx, insertWorkspace,
arg.ID,
arg.OrgID,
arg.Name,
arg.Environment,
)
return err
}
const listMembershipsByIdentity = `-- name: ListMembershipsByIdentity :many
SELECT identity_id, org_id, workspace_id, role, created_at
FROM memberships WHERE identity_id = $1 ORDER BY created_at, org_id
`
func (q *Queries) ListMembershipsByIdentity(ctx context.Context, identityID string) ([]Membership, error) {
rows, err := q.db.Query(ctx, listMembershipsByIdentity, identityID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Membership
for rows.Next() {
var i Membership
if err := rows.Scan(
&i.IdentityID,
&i.OrgID,
&i.WorkspaceID,
&i.Role,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listOrgs = `-- name: ListOrgs :many
SELECT id, name, created_at, updated_at FROM orgs ORDER BY created_at, id
`
func (q *Queries) ListOrgs(ctx context.Context) ([]Org, error) {
rows, err := q.db.Query(ctx, listOrgs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Org
for rows.Next() {
var i Org
if err := rows.Scan(
&i.ID,
&i.Name,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listWorkspaces = `-- name: ListWorkspaces :many
SELECT id, org_id, name, environment, created_at, updated_at
FROM workspaces WHERE org_id = $1 ORDER BY created_at, id
`
func (q *Queries) ListWorkspaces(ctx context.Context, orgID string) ([]Workspace, error) {
rows, err := q.db.Query(ctx, listWorkspaces, orgID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Workspace
for rows.Next() {
var i Workspace
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.Environment,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const resolveIdentityOrg = `-- name: ResolveIdentityOrg :one
SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'default'
)::text AS org_id
`
// The assigned org wins for an agent; otherwise the identity's oldest
// membership decides; an identity in no org belongs to the default org.
func (q *Queries) ResolveIdentityOrg(ctx context.Context, identityID string) (string, error) {
row := q.db.QueryRow(ctx, resolveIdentityOrg, identityID)
var org_id string
err := row.Scan(&org_id)
return org_id, err
}
const upsertAgentAssignment = `-- name: UpsertAgentAssignment :exec
INSERT INTO org_agents (identity_id, org_id) VALUES ($1, $2)
ON CONFLICT (identity_id) DO UPDATE SET org_id = excluded.org_id
`
type UpsertAgentAssignmentParams struct {
IdentityID string
OrgID string
}
func (q *Queries) UpsertAgentAssignment(ctx context.Context, arg UpsertAgentAssignmentParams) error {
_, err := q.db.Exec(ctx, upsertAgentAssignment, arg.IdentityID, arg.OrgID)
return err
}
@@ -0,0 +1,204 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: orgupgrade.sql
package postgresdb
import (
"context"
"time"
)
const assignDefaultOrgIdentities = `-- name: AssignDefaultOrgIdentities :exec
INSERT INTO org_agents (identity_id, org_id)
SELECT id, 'default' FROM identities WHERE kind IN ('bot', 'service_account')
ON CONFLICT (identity_id) DO NOTHING
`
// Every existing agent and service identity belongs to the default org.
func (q *Queries) AssignDefaultOrgIdentities(ctx context.Context) error {
_, err := q.db.Exec(ctx, assignDefaultOrgIdentities)
return err
}
const ensureDefaultOrg = `-- name: EnsureDefaultOrg :exec
INSERT INTO orgs (id, name) VALUES ('default', 'Default')
ON CONFLICT (id) DO NOTHING
`
func (q *Queries) EnsureDefaultOrg(ctx context.Context) error {
_, err := q.db.Exec(ctx, ensureDefaultOrg)
return err
}
const ensureDefaultWorkspace = `-- name: EnsureDefaultWorkspace :exec
INSERT INTO workspaces (id, org_id, name) VALUES ('default', 'default', 'Default')
ON CONFLICT (org_id, id) DO NOTHING
`
func (q *Queries) EnsureDefaultWorkspace(ctx context.Context) error {
_, err := q.db.Exec(ctx, ensureDefaultWorkspace)
return err
}
const getOrgUpgradePhase = `-- name: GetOrgUpgradePhase :one
SELECT completed_at FROM org_upgrades WHERE phase = $1
`
// The resumable default org/workspace upgrade
// (docs/prds/orgs-and-access.md, "Upgrading existing installs"). Each phase
// runs in one transaction with its ledger row, so a phase is either recorded
// or entirely absent; a recorded phase is skipped on the next start.
func (q *Queries) GetOrgUpgradePhase(ctx context.Context, phase string) (time.Time, error) {
row := q.db.QueryRow(ctx, getOrgUpgradePhase, phase)
var completed_at time.Time
err := row.Scan(&completed_at)
return completed_at, err
}
const insertOrgUpgradePhase = `-- name: InsertOrgUpgradePhase :exec
INSERT INTO org_upgrades (phase) VALUES ($1)
`
func (q *Queries) InsertOrgUpgradePhase(ctx context.Context, phase string) error {
_, err := q.db.Exec(ctx, insertOrgUpgradePhase, phase)
return err
}
const stampEdastoreBindings = `-- name: StampEdastoreBindings :execrows
UPDATE bindings SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampEdastoreBindings(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampEdastoreBindings)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampEdastoreCaptures = `-- name: StampEdastoreCaptures :execrows
UPDATE captures SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampEdastoreCaptures(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampEdastoreCaptures)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampEdastoreEventTypes = `-- name: StampEdastoreEventTypes :execrows
UPDATE event_types SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampEdastoreEventTypes(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampEdastoreEventTypes)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampEdastoreMappings = `-- name: StampEdastoreMappings :execrows
UPDATE mappings SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampEdastoreMappings(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampEdastoreMappings)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampEdastoreSources = `-- name: StampEdastoreSources :execrows
UPDATE sources SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampEdastoreSources(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampEdastoreSources)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampEdastoreToolCalls = `-- name: StampEdastoreToolCalls :execrows
UPDATE tool_calls SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampEdastoreToolCalls(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampEdastoreToolCalls)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampStateStoreEvents = `-- name: StampStateStoreEvents :execrows
UPDATE events SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampStateStoreEvents(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampStateStoreEvents)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampStateStoreResourceHistory = `-- name: StampStateStoreResourceHistory :execrows
UPDATE resource_history SET org_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampStateStoreResourceHistory(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampStateStoreResourceHistory)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampStateStoreResources = `-- name: StampStateStoreResources :execrows
UPDATE resources SET org_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampStateStoreResources(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampStateStoreResources)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampStateStoreTasks = `-- name: StampStateStoreTasks :execrows
UPDATE tasks SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
// The phase stamps: empty values are rows written before the default
// backfills took over, and re-stamping is harmless because the predicates
// only ever match what the upgrade has not covered yet.
func (q *Queries) StampStateStoreTasks(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampStateStoreTasks)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampStateStoreTransitions = `-- name: StampStateStoreTransitions :execrows
UPDATE transitions SET org_id = 'default', workspace_id = 'default' WHERE org_id = ''
`
func (q *Queries) StampStateStoreTransitions(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, stampStateStoreTransitions)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
@@ -13,7 +13,7 @@ import (
const insertResource = `-- name: InsertResource :one
INSERT INTO resources (kind, value, version, updated_at)
VALUES ($1, $2, 1, $3)
RETURNING kind, value, version, updated_at
RETURNING kind, value, version, updated_at, org_id
`
type InsertResourceParams struct {
@@ -30,6 +30,7 @@ func (q *Queries) InsertResource(ctx context.Context, arg InsertResourceParams)
&i.Value,
&i.Version,
&i.UpdatedAt,
&i.OrgID,
)
return i, err
}
@@ -108,7 +109,7 @@ func (q *Queries) LockResourceWrite(ctx context.Context, hashtextextended string
}
const resourceByKind = `-- name: ResourceByKind :one
SELECT kind, value, version, updated_at FROM resources WHERE kind = $1
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1
`
func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, error) {
@@ -119,6 +120,7 @@ func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, er
&i.Value,
&i.Version,
&i.UpdatedAt,
&i.OrgID,
)
return i, err
}
@@ -235,7 +237,7 @@ const updateResource = `-- name: UpdateResource :one
UPDATE resources
SET value = $2, version = version + 1, updated_at = $3
WHERE kind = $1 AND version = $4
RETURNING kind, value, version, updated_at
RETURNING kind, value, version, updated_at, org_id
`
type UpdateResourceParams struct {
@@ -258,6 +260,7 @@ func (q *Queries) UpdateResource(ctx context.Context, arg UpdateResourceParams)
&i.Value,
&i.Version,
&i.UpdatedAt,
&i.OrgID,
)
return i, err
}
@@ -48,7 +48,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation
const claimByIssue = `-- name: ClaimByIssue :one
UPDATE tasks SET status = 'running', attempt = attempt + 1, updated_at = now()
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id
`
type ClaimByIssueParams struct {
@@ -95,6 +95,8 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
@@ -109,7 +111,7 @@ WHERE id = (
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id
`
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
@@ -153,6 +155,8 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
@@ -200,9 +204,18 @@ func (q *Queries) CountTasksByStatus(ctx context.Context) ([]CountTasksByStatusR
}
const enqueueIssue = `-- name: EnqueueIssue :execrows
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (owner, repo, issue_number) DO NOTHING
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity, org_id, workspace_id)
VALUES (
$1, $2, $3, $4, $5, $6, $7,
COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $7),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $7
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'default'
),
'default'
)
ON CONFLICT (org_id, identity, owner, repo, issue_number) DO NOTHING
`
type EnqueueIssueParams struct {
@@ -215,6 +228,10 @@ type EnqueueIssueParams struct {
Identity string
}
// The org is derived from the identity at the one place a task row is
// written (an agent's assignment, else a membership, else the default org);
// uniqueness follows the record's identity: two orgs may work the same
// issue under their own identities, one may not work it twice.
func (q *Queries) EnqueueIssue(ctx context.Context, arg EnqueueIssueParams) (int64, error) {
result, err := q.db.Exec(ctx, enqueueIssue,
arg.Owner,
@@ -243,7 +260,7 @@ VALUES (
), $3) + 1,
$4, $5, 'chat', $6, 'chat', $7
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id
`
type InsertChatTaskParams struct {
@@ -306,6 +323,8 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
@@ -580,7 +599,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
}
const taskByID = `-- name: TaskByID :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs FROM tasks WHERE id = $1
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id FROM tasks WHERE id = $1
`
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
@@ -621,12 +640,14 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
const taskByIssue = `-- name: TaskByIssue :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
`
type TaskByIssueParams struct {
@@ -673,12 +694,14 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
const taskByPR = `-- name: TaskByPR :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
`
type TaskByPRParams struct {
@@ -734,6 +757,8 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
@@ -5,7 +5,7 @@ INSERT INTO captures (id, source, remote_addr, content_type, headers, body, auth
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
-- name: ListCaptures :many
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id
FROM captures
ORDER BY received_at DESC
LIMIT $1;
@@ -14,7 +14,7 @@ LIMIT $1;
-- A capture is undispatched while some armed binding on its source, whose
-- mapping belongs to the capture's event type, has not dispatched it. An
-- unidentified capture has no event type, so it is never listed.
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id
FROM captures c
WHERE c.source = ANY(@sources::text[])
AND c.event_type <> ''
@@ -118,11 +118,11 @@ SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE id = $1;
-- name: ListEventTypes :many
SELECT id, source, name, rule, schema, created_at, updated_at
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types ORDER BY source, name;
-- name: EventTypesForSource :many
SELECT id, source, name, rule, schema, created_at, updated_at
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types WHERE source = $1 ORDER BY name;
-- name: UpdateEventType :execrows
@@ -139,10 +139,10 @@ SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text))
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3);
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at FROM sources WHERE path = $1;
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1;
-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at FROM sources ORDER BY created_at DESC, path;
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path;
-- name: SetSourceSigning :execrows
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
@@ -17,20 +17,20 @@ FROM append_lock
RETURNING id;
-- name: ListEventsFromBeginning :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
FROM events
ORDER BY at, id
LIMIT $1;
-- name: ListEventsAfter :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
FROM events
WHERE at > $1 OR (at = $1 AND id > $2)
ORDER BY at, id
LIMIT $3;
-- name: TaskEventsByID :many
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
FROM events
WHERE task_id = $1
ORDER BY id;
@@ -0,0 +1,43 @@
-- Orgs, workspaces, memberships and agent assignments: the durable half of
-- the tenant boundary contract (internal/domain/org). IDs are caller-provided
-- slugs; workspaces are unique per org, memberships per identity+org+workspace
-- (schema 0012), and an agent assignment is unique per identity.
-- name: InsertOrg :exec
INSERT INTO orgs (id, name) VALUES ($1, $2);
-- name: GetOrg :one
SELECT id, name, created_at, updated_at FROM orgs WHERE id = $1;
-- name: ListOrgs :many
SELECT id, name, created_at, updated_at FROM orgs ORDER BY created_at, id;
-- name: InsertWorkspace :exec
INSERT INTO workspaces (id, org_id, name, environment) VALUES ($1, $2, $3, $4);
-- name: ListWorkspaces :many
SELECT id, org_id, name, environment, created_at, updated_at
FROM workspaces WHERE org_id = $1 ORDER BY created_at, id;
-- name: EnsureMembership :exec
INSERT INTO memberships (identity_id, org_id, workspace_id, role)
VALUES ($1, $2, $3, $4)
ON CONFLICT DO NOTHING;
-- name: ListMembershipsByIdentity :many
SELECT identity_id, org_id, workspace_id, role, created_at
FROM memberships WHERE identity_id = $1 ORDER BY created_at, org_id;
-- name: UpsertAgentAssignment :exec
INSERT INTO org_agents (identity_id, org_id) VALUES ($1, $2)
ON CONFLICT (identity_id) DO UPDATE SET org_id = excluded.org_id;
-- name: ResolveIdentityOrg :one
-- The assigned org wins for an agent; otherwise the identity's oldest
-- membership decides; an identity in no org belongs to the default org.
SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'default'
)::text AS org_id;
@@ -0,0 +1,61 @@
-- The resumable default org/workspace upgrade
-- (docs/prds/orgs-and-access.md, "Upgrading existing installs"). Each phase
-- runs in one transaction with its ledger row, so a phase is either recorded
-- or entirely absent; a recorded phase is skipped on the next start.
-- name: GetOrgUpgradePhase :one
SELECT completed_at FROM org_upgrades WHERE phase = $1;
-- name: InsertOrgUpgradePhase :exec
INSERT INTO org_upgrades (phase) VALUES ($1);
-- name: EnsureDefaultOrg :exec
INSERT INTO orgs (id, name) VALUES ('default', 'Default')
ON CONFLICT (id) DO NOTHING;
-- name: EnsureDefaultWorkspace :exec
INSERT INTO workspaces (id, org_id, name) VALUES ('default', 'default', 'Default')
ON CONFLICT (org_id, id) DO NOTHING;
-- name: AssignDefaultOrgIdentities :exec
-- Every existing agent and service identity belongs to the default org.
INSERT INTO org_agents (identity_id, org_id)
SELECT id, 'default' FROM identities WHERE kind IN ('bot', 'service_account')
ON CONFLICT (identity_id) DO NOTHING;
-- The phase stamps: empty values are rows written before the default
-- backfills took over, and re-stamping is harmless because the predicates
-- only ever match what the upgrade has not covered yet.
-- name: StampStateStoreTasks :execrows
UPDATE tasks SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampStateStoreEvents :execrows
UPDATE events SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampStateStoreTransitions :execrows
UPDATE transitions SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampStateStoreResources :execrows
UPDATE resources SET org_id = 'default' WHERE org_id = '';
-- name: StampStateStoreResourceHistory :execrows
UPDATE resource_history SET org_id = 'default' WHERE org_id = '';
-- name: StampEdastoreBindings :execrows
UPDATE bindings SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampEdastoreMappings :execrows
UPDATE mappings SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampEdastoreCaptures :execrows
UPDATE captures SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampEdastoreSources :execrows
UPDATE sources SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampEdastoreEventTypes :execrows
UPDATE event_types SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
-- name: StampEdastoreToolCalls :execrows
UPDATE tool_calls SET org_id = 'default', workspace_id = 'default' WHERE org_id = '';
@@ -5,7 +5,7 @@
SELECT pg_advisory_xact_lock(hashtextextended($1, 0));
-- name: ResourceByKind :one
SELECT kind, value, version, updated_at FROM resources WHERE kind = $1;
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1;
-- name: ResourceByRequest :one
SELECT kind, value, version, actor, source, request_id, expected_version,
@@ -27,13 +27,13 @@ SELECT version FROM resources WHERE kind = $1;
-- name: InsertResource :one
INSERT INTO resources (kind, value, version, updated_at)
VALUES ($1, $2, 1, $3)
RETURNING kind, value, version, updated_at;
RETURNING kind, value, version, updated_at, org_id;
-- name: UpdateResource :one
UPDATE resources
SET value = $2, version = version + 1, updated_at = $3
WHERE kind = $1 AND version = $4
RETURNING kind, value, version, updated_at;
RETURNING kind, value, version, updated_at, org_id;
-- name: InsertResourceHistory :one
INSERT INTO resource_history (
@@ -94,9 +94,22 @@ WHERE status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed');
UPDATE tasks SET status = 'queued', updated_at = now() WHERE status = 'running';
-- name: EnqueueIssue :execrows
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (owner, repo, issue_number) DO NOTHING;
-- The org is derived from the identity at the one place a task row is
-- written (an agent's assignment, else a membership, else the default org);
-- uniqueness follows the record's identity: two orgs may work the same
-- issue under their own identities, one may not work it twice.
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity, org_id, workspace_id)
VALUES (
$1, $2, $3, $4, $5, $6, $7,
COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $7),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $7
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'default'
),
'default'
)
ON CONFLICT (org_id, identity, owner, repo, issue_number) DO NOTHING;
-- name: ClaimByIssue :one
UPDATE tasks SET status = 'running', attempt = attempt + 1, updated_at = now()
@@ -99,17 +99,30 @@ func TestChatIssueNumberAllocatorIsPerRepo(t *testing.T) {
}
}
// The issue-number uniqueness the SQLite schema carried as a table constraint
// survives the move.
// Task uniqueness follows the record's identity: the same owner/repo/issue
// under the same org and identity is refused, and the constraint that carries
// that rule survives the move.
func TestTaskIssueNumberIsUnique(t *testing.T) {
pool, q := migrated(t)
task := insertChatTask(t, q)
_, err := pool.Exec(t.Context(), `
INSERT INTO tasks (owner, repo, issue_number) VALUES ($1, $2, $3)`,
task.Owner, task.Repo, task.IssueNumber)
INSERT INTO tasks (owner, repo, issue_number, identity, org_id)
VALUES ($1, $2, $3, $4, 'default')`,
task.Owner, task.Repo, task.IssueNumber, task.Identity)
if err == nil {
t.Fatal("a second task for the same owner/repo/issue was accepted, want the unique constraint refused it")
t.Fatal("a second task for the same org/identity/owner/repo/issue was accepted, want the unique constraint refused it")
}
// A different identity may work the same issue: that is its own record.
if task.Identity != "other" {
_, err = pool.Exec(t.Context(), `
INSERT INTO tasks (owner, repo, issue_number, identity, org_id)
VALUES ($1, $2, $3, 'other', 'default')`,
task.Owner, task.Repo, task.IssueNumber)
if err != nil {
t.Fatalf("a second task under another identity was refused: %v", err)
}
}
}