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

@@ -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
}