259 lines
6.4 KiB
Go
259 lines
6.4 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 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 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 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 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
|
|
}
|