Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
13.3 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: remote_apps.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const deleteRemoteAppOAuthClient = `-- name: DeleteRemoteAppOAuthClient :exec
DELETE FROM remote_app_oauth_client WHERE id = $1
`

// The authorization server no longer knows the client; the next connect
// registers a new one.
func (q *Queries) DeleteRemoteAppOAuthClient(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteRemoteAppOAuthClient, id)
	return err
}

const getActiveRemoteAppConnection = `-- name: GetActiveRemoteAppConnection :one
SELECT id, app_slug, scope, owner_id, connected_by, oauth_client_id, access_token, refresh_token, expires_at, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM remote_app_connection
WHERE app_slug = $1 AND scope = $2 AND owner_id = $3 AND status = 'active'
  AND (workspace_id = $4::uuid OR workspace_id IS NULL)
ORDER BY (workspace_id IS NULL)
LIMIT 1
`

type GetActiveRemoteAppConnectionParams struct {
	AppSlug     string      `json:"app_slug"`
	Scope       string      `json:"scope"`
	OwnerID     pgtype.UUID `json:"owner_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) GetActiveRemoteAppConnection(ctx context.Context, arg GetActiveRemoteAppConnectionParams) (RemoteAppConnection, error) {
	row := q.db.QueryRow(ctx, getActiveRemoteAppConnection,
		arg.AppSlug,
		arg.Scope,
		arg.OwnerID,
		arg.WorkspaceID,
	)
	var i RemoteAppConnection
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.Scope,
		&i.OwnerID,
		&i.ConnectedBy,
		&i.OauthClientID,
		&i.AccessToken,
		&i.RefreshToken,
		&i.ExpiresAt,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const getRemoteAppConnection = `-- name: GetRemoteAppConnection :one
SELECT id, app_slug, scope, owner_id, connected_by, oauth_client_id, access_token, refresh_token, expires_at, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM remote_app_connection
WHERE id = $1
`

func (q *Queries) GetRemoteAppConnection(ctx context.Context, id pgtype.UUID) (RemoteAppConnection, error) {
	row := q.db.QueryRow(ctx, getRemoteAppConnection, id)
	var i RemoteAppConnection
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.Scope,
		&i.OwnerID,
		&i.ConnectedBy,
		&i.OauthClientID,
		&i.AccessToken,
		&i.RefreshToken,
		&i.ExpiresAt,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const getRemoteAppConnectionForOwner = `-- name: GetRemoteAppConnectionForOwner :one
SELECT id, app_slug, scope, owner_id, connected_by, oauth_client_id, access_token, refresh_token, expires_at, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM remote_app_connection
WHERE id = $1 AND scope = $2 AND owner_id = $3
`

type GetRemoteAppConnectionForOwnerParams struct {
	ID      pgtype.UUID `json:"id"`
	Scope   string      `json:"scope"`
	OwnerID pgtype.UUID `json:"owner_id"`
}

// Owner-scoped, so an account cannot be changed by guessing its id.
func (q *Queries) GetRemoteAppConnectionForOwner(ctx context.Context, arg GetRemoteAppConnectionForOwnerParams) (RemoteAppConnection, error) {
	row := q.db.QueryRow(ctx, getRemoteAppConnectionForOwner, arg.ID, arg.Scope, arg.OwnerID)
	var i RemoteAppConnection
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.Scope,
		&i.OwnerID,
		&i.ConnectedBy,
		&i.OauthClientID,
		&i.AccessToken,
		&i.RefreshToken,
		&i.ExpiresAt,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const getRemoteAppOAuthClient = `-- name: GetRemoteAppOAuthClient :one

SELECT id, app_slug, redirect_uri, authorization_endpoint, token_endpoint, scope, client_id, client_secret, token_endpoint_auth_method, created_at FROM remote_app_oauth_client
WHERE app_slug = $1 AND redirect_uri = $2
`

type GetRemoteAppOAuthClientParams struct {
	AppSlug     string `json:"app_slug"`
	RedirectUri string `json:"redirect_uri"`
}

// =====================
// Remote MCP app OAuth clients
// =====================
func (q *Queries) GetRemoteAppOAuthClient(ctx context.Context, arg GetRemoteAppOAuthClientParams) (RemoteAppOauthClient, error) {
	row := q.db.QueryRow(ctx, getRemoteAppOAuthClient, arg.AppSlug, arg.RedirectUri)
	var i RemoteAppOauthClient
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.RedirectUri,
		&i.AuthorizationEndpoint,
		&i.TokenEndpoint,
		&i.Scope,
		&i.ClientID,
		&i.ClientSecret,
		&i.TokenEndpointAuthMethod,
		&i.CreatedAt,
	)
	return i, err
}

const getRemoteAppOAuthClientByID = `-- name: GetRemoteAppOAuthClientByID :one
SELECT id, app_slug, redirect_uri, authorization_endpoint, token_endpoint, scope, client_id, client_secret, token_endpoint_auth_method, created_at FROM remote_app_oauth_client
WHERE id = $1
`

func (q *Queries) GetRemoteAppOAuthClientByID(ctx context.Context, id pgtype.UUID) (RemoteAppOauthClient, error) {
	row := q.db.QueryRow(ctx, getRemoteAppOAuthClientByID, id)
	var i RemoteAppOauthClient
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.RedirectUri,
		&i.AuthorizationEndpoint,
		&i.TokenEndpoint,
		&i.Scope,
		&i.ClientID,
		&i.ClientSecret,
		&i.TokenEndpointAuthMethod,
		&i.CreatedAt,
	)
	return i, err
}

const insertRemoteAppOAuthClient = `-- name: InsertRemoteAppOAuthClient :one
INSERT INTO remote_app_oauth_client (
    app_slug, redirect_uri, authorization_endpoint, token_endpoint, scope,
    client_id, client_secret, token_endpoint_auth_method
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8
)
ON CONFLICT (app_slug, redirect_uri) DO NOTHING
RETURNING id, app_slug, redirect_uri, authorization_endpoint, token_endpoint, scope, client_id, client_secret, token_endpoint_auth_method, created_at
`

type InsertRemoteAppOAuthClientParams struct {
	AppSlug                 string `json:"app_slug"`
	RedirectUri             string `json:"redirect_uri"`
	AuthorizationEndpoint   string `json:"authorization_endpoint"`
	TokenEndpoint           string `json:"token_endpoint"`
	Scope                   string `json:"scope"`
	ClientID                string `json:"client_id"`
	ClientSecret            []byte `json:"client_secret"`
	TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"`
}

// A concurrent first connect may have registered already; the caller reads
// that row when this returns no rows.
func (q *Queries) InsertRemoteAppOAuthClient(ctx context.Context, arg InsertRemoteAppOAuthClientParams) (RemoteAppOauthClient, error) {
	row := q.db.QueryRow(ctx, insertRemoteAppOAuthClient,
		arg.AppSlug,
		arg.RedirectUri,
		arg.AuthorizationEndpoint,
		arg.TokenEndpoint,
		arg.Scope,
		arg.ClientID,
		arg.ClientSecret,
		arg.TokenEndpointAuthMethod,
	)
	var i RemoteAppOauthClient
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.RedirectUri,
		&i.AuthorizationEndpoint,
		&i.TokenEndpoint,
		&i.Scope,
		&i.ClientID,
		&i.ClientSecret,
		&i.TokenEndpointAuthMethod,
		&i.CreatedAt,
	)
	return i, err
}

const listRemoteAppConnectionsForOwner = `-- name: ListRemoteAppConnectionsForOwner :many
SELECT id, app_slug, scope, owner_id, connected_by, oauth_client_id, access_token, refresh_token, expires_at, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM remote_app_connection
WHERE scope = $1 AND owner_id = $2 AND status <> 'revoked'
  AND (workspace_id = $3::uuid OR workspace_id IS NULL)
ORDER BY (workspace_id IS NULL), connected_at DESC
`

type ListRemoteAppConnectionsForOwnerParams struct {
	Scope       string      `json:"scope"`
	OwnerID     pgtype.UUID `json:"owner_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

// Every account the owner still sees in one workspace: active and expired,
// not disconnected. Accounts made before they belonged to a workspace count
// in every workspace.
func (q *Queries) ListRemoteAppConnectionsForOwner(ctx context.Context, arg ListRemoteAppConnectionsForOwnerParams) ([]RemoteAppConnection, error) {
	rows, err := q.db.Query(ctx, listRemoteAppConnectionsForOwner, arg.Scope, arg.OwnerID, arg.WorkspaceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []RemoteAppConnection{}
	for rows.Next() {
		var i RemoteAppConnection
		if err := rows.Scan(
			&i.ID,
			&i.AppSlug,
			&i.Scope,
			&i.OwnerID,
			&i.ConnectedBy,
			&i.OauthClientID,
			&i.AccessToken,
			&i.RefreshToken,
			&i.ExpiresAt,
			&i.Status,
			&i.ConnectedAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lockRemoteAppConnection = `-- name: LockRemoteAppConnection :one
SELECT id, app_slug, scope, owner_id, connected_by, oauth_client_id, access_token, refresh_token, expires_at, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM remote_app_connection
WHERE id = $1
FOR UPDATE
`

// Serializes token refresh: a refresh token is single-use, so two refreshes
// of the same account must not race.
func (q *Queries) LockRemoteAppConnection(ctx context.Context, id pgtype.UUID) (RemoteAppConnection, error) {
	row := q.db.QueryRow(ctx, lockRemoteAppConnection, id)
	var i RemoteAppConnection
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.Scope,
		&i.OwnerID,
		&i.ConnectedBy,
		&i.OauthClientID,
		&i.AccessToken,
		&i.RefreshToken,
		&i.ExpiresAt,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const setRemoteAppConnectionStatus = `-- name: SetRemoteAppConnectionStatus :exec
UPDATE remote_app_connection
SET status = $2,
    access_token = CASE WHEN $2::text = 'active' THEN access_token END,
    refresh_token = CASE WHEN $2::text = 'active' THEN refresh_token END,
    updated_at = now()
WHERE id = $1
`

type SetRemoteAppConnectionStatusParams struct {
	ID     pgtype.UUID `json:"id"`
	Status string      `json:"status"`
}

// Leaving active drops the tokens: an expired account needs a new sign-in and
// a revoked one must not keep a usable credential.
func (q *Queries) SetRemoteAppConnectionStatus(ctx context.Context, arg SetRemoteAppConnectionStatusParams) error {
	_, err := q.db.Exec(ctx, setRemoteAppConnectionStatus, arg.ID, arg.Status)
	return err
}

const touchRemoteAppConnection = `-- name: TouchRemoteAppConnection :exec
UPDATE remote_app_connection
SET last_used_at = now()
WHERE id = $1
`

func (q *Queries) TouchRemoteAppConnection(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, touchRemoteAppConnection, id)
	return err
}

const updateRemoteAppConnectionTokens = `-- name: UpdateRemoteAppConnectionTokens :exec
UPDATE remote_app_connection
SET access_token = $2, refresh_token = $3, expires_at = $4, updated_at = now()
WHERE id = $1
`

type UpdateRemoteAppConnectionTokensParams struct {
	ID           pgtype.UUID        `json:"id"`
	AccessToken  []byte             `json:"access_token"`
	RefreshToken []byte             `json:"refresh_token"`
	ExpiresAt    pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) UpdateRemoteAppConnectionTokens(ctx context.Context, arg UpdateRemoteAppConnectionTokensParams) error {
	_, err := q.db.Exec(ctx, updateRemoteAppConnectionTokens,
		arg.ID,
		arg.AccessToken,
		arg.RefreshToken,
		arg.ExpiresAt,
	)
	return err
}

const upsertRemoteAppConnection = `-- name: UpsertRemoteAppConnection :one

INSERT INTO remote_app_connection (
    app_slug, scope, owner_id, connected_by, oauth_client_id,
    access_token, refresh_token, expires_at, status, workspace_id
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, 'active', $9
)
ON CONFLICT (scope, owner_id, app_slug, workspace_id) DO UPDATE SET
    connected_by    = EXCLUDED.connected_by,
    oauth_client_id = EXCLUDED.oauth_client_id,
    access_token    = EXCLUDED.access_token,
    refresh_token   = EXCLUDED.refresh_token,
    expires_at      = EXCLUDED.expires_at,
    status          = 'active',
    connected_at    = now(),
    updated_at      = now()
RETURNING id, app_slug, scope, owner_id, connected_by, oauth_client_id, access_token, refresh_token, expires_at, status, connected_at, last_used_at, created_at, updated_at, workspace_id
`

type UpsertRemoteAppConnectionParams struct {
	AppSlug       string             `json:"app_slug"`
	Scope         string             `json:"scope"`
	OwnerID       pgtype.UUID        `json:"owner_id"`
	ConnectedBy   pgtype.UUID        `json:"connected_by"`
	OauthClientID pgtype.UUID        `json:"oauth_client_id"`
	AccessToken   []byte             `json:"access_token"`
	RefreshToken  []byte             `json:"refresh_token"`
	ExpiresAt     pgtype.Timestamptz `json:"expires_at"`
	WorkspaceID   pgtype.UUID        `json:"workspace_id"`
}

// =====================
// Remote MCP app connections
// =====================
func (q *Queries) UpsertRemoteAppConnection(ctx context.Context, arg UpsertRemoteAppConnectionParams) (RemoteAppConnection, error) {
	row := q.db.QueryRow(ctx, upsertRemoteAppConnection,
		arg.AppSlug,
		arg.Scope,
		arg.OwnerID,
		arg.ConnectedBy,
		arg.OauthClientID,
		arg.AccessToken,
		arg.RefreshToken,
		arg.ExpiresAt,
		arg.WorkspaceID,
	)
	var i RemoteAppConnection
	err := row.Scan(
		&i.ID,
		&i.AppSlug,
		&i.Scope,
		&i.OwnerID,
		&i.ConnectedBy,
		&i.OauthClientID,
		&i.AccessToken,
		&i.RefreshToken,
		&i.ExpiresAt,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}