399 lines
10 KiB
Go
399 lines
10 KiB
Go
// 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 agentAssignmentOrg = `-- name: AgentAssignmentOrg :one
|
|
SELECT org_id FROM org_agents WHERE identity_id = $1
|
|
`
|
|
|
|
func (q *Queries) AgentAssignmentOrg(ctx context.Context, identityID string) (string, error) {
|
|
row := q.db.QueryRow(ctx, agentAssignmentOrg, identityID)
|
|
var org_id string
|
|
err := row.Scan(&org_id)
|
|
return org_id, err
|
|
}
|
|
|
|
const ensureAgentOrgMembership = `-- name: EnsureAgentOrgMembership :exec
|
|
INSERT INTO memberships (identity_id, org_id, role)
|
|
SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a
|
|
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO NOTHING
|
|
`
|
|
|
|
// Every agent identity serves its org with the shipped developer role: an
|
|
// agent is granted access the way a user is
|
|
// (docs/prds/orgs-and-access.md, "Identity"). Idempotent, so an operator's
|
|
// later role change survives a restart, and the shipped seed never
|
|
// overwrites it.
|
|
func (q *Queries) EnsureAgentOrgMembership(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, ensureAgentOrgMembership)
|
|
return err
|
|
}
|
|
|
|
const ensureAgentOrgMembershipFor = `-- name: EnsureAgentOrgMembershipFor :exec
|
|
INSERT INTO memberships (identity_id, org_id, role)
|
|
SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a
|
|
WHERE a.identity_id = $1
|
|
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO NOTHING
|
|
`
|
|
|
|
// Same grant as EnsureAgentOrgMembership for one identity: an agent assigned
|
|
// to an org after boot (AssignAgent) still gets the developer membership there.
|
|
func (q *Queries) EnsureAgentOrgMembershipFor(ctx context.Context, identityID string) error {
|
|
_, err := q.db.Exec(ctx, ensureAgentOrgMembershipFor, identityID)
|
|
return err
|
|
}
|
|
|
|
const ensureMembership = `-- name: EnsureMembership :exec
|
|
INSERT INTO memberships (identity_id, org_id, workspace_id, role)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO UPDATE SET role = excluded.role
|
|
`
|
|
|
|
type EnsureMembershipParams struct {
|
|
IdentityID string
|
|
OrgID string
|
|
WorkspaceID pgtype.Text
|
|
Role string
|
|
}
|
|
|
|
// Grant or change a role: a membership that already exists moves to the
|
|
// supplied role, so the same call is both create and update.
|
|
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 listMembers = `-- name: ListMembers :many
|
|
SELECT m.identity_id, i.kind, i.display_name, m.workspace_id, m.role
|
|
FROM memberships m
|
|
JOIN identities i ON i.id = m.identity_id
|
|
WHERE m.org_id = $1
|
|
ORDER BY m.created_at, m.identity_id, m.workspace_id
|
|
`
|
|
|
|
type ListMembersRow struct {
|
|
IdentityID string
|
|
Kind string
|
|
DisplayName string
|
|
WorkspaceID pgtype.Text
|
|
Role string
|
|
}
|
|
|
|
func (q *Queries) ListMembers(ctx context.Context, orgID string) ([]ListMembersRow, error) {
|
|
rows, err := q.db.Query(ctx, listMembers, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListMembersRow
|
|
for rows.Next() {
|
|
var i ListMembersRow
|
|
if err := rows.Scan(
|
|
&i.IdentityID,
|
|
&i.Kind,
|
|
&i.DisplayName,
|
|
&i.WorkspaceID,
|
|
&i.Role,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
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 listOrgAgents = `-- name: ListOrgAgents :many
|
|
SELECT identity_id, org_id FROM org_agents WHERE org_id = $1 ORDER BY identity_id
|
|
`
|
|
|
|
type ListOrgAgentsRow struct {
|
|
IdentityID string
|
|
OrgID string
|
|
}
|
|
|
|
func (q *Queries) ListOrgAgents(ctx context.Context, orgID string) ([]ListOrgAgentsRow, error) {
|
|
rows, err := q.db.Query(ctx, listOrgAgents, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListOrgAgentsRow
|
|
for rows.Next() {
|
|
var i ListOrgAgentsRow
|
|
if err := rows.Scan(&i.IdentityID, &i.OrgID); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listOrgOwners = `-- name: ListOrgOwners :many
|
|
SELECT identity_id FROM memberships
|
|
WHERE org_id = $1 AND workspace_id IS NULL AND role = 'owner'
|
|
ORDER BY identity_id
|
|
`
|
|
|
|
func (q *Queries) ListOrgOwners(ctx context.Context, orgID string) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listOrgOwners, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []string
|
|
for rows.Next() {
|
|
var identity_id string
|
|
if err := rows.Scan(&identity_id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, identity_id)
|
|
}
|
|
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 membershipRole = `-- name: MembershipRole :one
|
|
SELECT role FROM memberships
|
|
WHERE identity_id = $1 AND org_id = $2
|
|
AND COALESCE(workspace_id, '') = $3::text
|
|
`
|
|
|
|
type MembershipRoleParams struct {
|
|
IdentityID string
|
|
OrgID string
|
|
WorkspaceID string
|
|
}
|
|
|
|
// The current role of the membership EnsureMembership/RemoveMembership targets,
|
|
// read in the same transaction so the audit row carries both sides.
|
|
func (q *Queries) MembershipRole(ctx context.Context, arg MembershipRoleParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, membershipRole, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
|
var role string
|
|
err := row.Scan(&role)
|
|
return role, err
|
|
}
|
|
|
|
const removeMembership = `-- name: RemoveMembership :execrows
|
|
DELETE FROM memberships
|
|
WHERE identity_id = $1 AND org_id = $2 AND COALESCE(workspace_id, '') = $3::text
|
|
`
|
|
|
|
type RemoveMembershipParams struct {
|
|
IdentityID string
|
|
OrgID string
|
|
WorkspaceID string
|
|
}
|
|
|
|
// The workspace is matched through the same COALESCE the unique index uses, so
|
|
// an org-wide membership (NULL) and the empty string are the same target.
|
|
func (q *Queries) RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, removeMembership, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), 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),
|
|
'org-sys'
|
|
)::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
|
|
}
|