Refactor db pkg
This commit is contained in:
+110
-108
@@ -52,9 +52,8 @@ func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (*User,
|
||||
u User
|
||||
createdAt string
|
||||
updatedAt string
|
||||
err error
|
||||
)
|
||||
if err := row.Scan(
|
||||
err := row.Scan(
|
||||
&createdAt,
|
||||
&u.DeactivatedAt,
|
||||
&u.Id,
|
||||
@@ -62,7 +61,8 @@ func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (*User,
|
||||
&u.Password,
|
||||
&u.Role,
|
||||
&updatedAt,
|
||||
); err != nil {
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"InsertUser: %w", err,
|
||||
)
|
||||
@@ -457,208 +457,210 @@ func (q *Queries) NextSequence(ctx context.Context, conversationId string) (int,
|
||||
return seq + 1, nil
|
||||
}
|
||||
|
||||
const insertApproval = `
|
||||
INSERT INTO approvals (id, message_id, tool_call_id)
|
||||
VALUES (?, ?, ?)
|
||||
const insertExecution = `
|
||||
INSERT INTO executions (id, message_id, status, tool_call_id)
|
||||
VALUES (?, ?, ?, ?)
|
||||
RETURNING created_at, id, message_id, status, tool_call_id, updated_at
|
||||
`
|
||||
|
||||
// InsertApprovalParams holds arguments for InsertApproval.
|
||||
type InsertApprovalParams struct {
|
||||
// InsertExecutionParams holds arguments for InsertExecution.
|
||||
type InsertExecutionParams struct {
|
||||
MessageId string
|
||||
Status ExecutionStatus
|
||||
ToolCallId string
|
||||
}
|
||||
|
||||
// InsertApproval creates a new pending approval.
|
||||
func (q *Queries) InsertApproval(ctx context.Context, arg InsertApprovalParams) (*Approval, error) {
|
||||
// InsertExecution creates a new execution row.
|
||||
func (q *Queries) InsertExecution(ctx context.Context, arg InsertExecutionParams) (*Execution, error) {
|
||||
row := q.db.QueryRowContext(
|
||||
ctx,
|
||||
insertApproval,
|
||||
insertExecution,
|
||||
uuid.New().String(),
|
||||
arg.MessageId,
|
||||
arg.Status,
|
||||
arg.ToolCallId,
|
||||
)
|
||||
|
||||
var (
|
||||
a Approval
|
||||
createdAt string
|
||||
updatedAt string
|
||||
err error
|
||||
)
|
||||
if err = row.Scan(
|
||||
&createdAt,
|
||||
&a.Id,
|
||||
&a.MessageId,
|
||||
&a.Status,
|
||||
&a.ToolCallId,
|
||||
&updatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"InsertApproval: %w", err,
|
||||
)
|
||||
}
|
||||
if a.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("InsertApproval: parse created_at: %w", err)
|
||||
}
|
||||
if a.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("InsertApproval: parse updated_at: %w", err)
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
const selectApprovalId = `
|
||||
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
||||
FROM approvals
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
// SelectApprovalId returns a single approval by ID.
|
||||
func (q *Queries) SelectApprovalId(ctx context.Context, id string) (*Approval, error) {
|
||||
row := q.db.QueryRowContext(ctx, selectApprovalId, id)
|
||||
|
||||
var (
|
||||
a Approval
|
||||
e Execution
|
||||
createdAt string
|
||||
updatedAt string
|
||||
)
|
||||
err := row.Scan(
|
||||
&createdAt,
|
||||
&a.Id,
|
||||
&a.MessageId,
|
||||
&a.Status,
|
||||
&a.ToolCallId,
|
||||
&e.Id,
|
||||
&e.MessageId,
|
||||
&e.Status,
|
||||
&e.ToolCallId,
|
||||
&updatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalId: %w", err)
|
||||
return nil, fmt.Errorf(
|
||||
"InsertExecution: %w", err,
|
||||
)
|
||||
}
|
||||
if a.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalId: parse created_at: %w", err)
|
||||
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("InsertExecution: parse created_at: %w", err)
|
||||
}
|
||||
if a.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalId: parse updated_at: %w", err)
|
||||
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("InsertExecution: parse updated_at: %w", err)
|
||||
}
|
||||
return &a, nil
|
||||
return &e, nil
|
||||
}
|
||||
|
||||
const selectApprovalsMessageId = `
|
||||
const selectExecutionId = `
|
||||
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
||||
FROM approvals
|
||||
FROM executions
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
// SelectExecutionId returns a single execution row by ID.
|
||||
func (q *Queries) SelectExecutionId(ctx context.Context, id string) (*Execution, error) {
|
||||
row := q.db.QueryRowContext(ctx, selectExecutionId, id)
|
||||
|
||||
var (
|
||||
e Execution
|
||||
createdAt string
|
||||
updatedAt string
|
||||
)
|
||||
err := row.Scan(
|
||||
&createdAt,
|
||||
&e.Id,
|
||||
&e.MessageId,
|
||||
&e.Status,
|
||||
&e.ToolCallId,
|
||||
&updatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SelectExecutionId: %w", err)
|
||||
}
|
||||
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectExecutionId: parse created_at: %w", err)
|
||||
}
|
||||
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectExecutionId: parse updated_at: %w", err)
|
||||
}
|
||||
return &e, nil
|
||||
}
|
||||
|
||||
const selectExecutionsMessageId = `
|
||||
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
||||
FROM executions
|
||||
WHERE message_id = ?
|
||||
ORDER BY created_at
|
||||
`
|
||||
|
||||
// SelectApprovalsMessageId returns all approvals for a message,
|
||||
// SelectExecutionsMessageId returns all execution rows for a message,
|
||||
// ordered by creation time.
|
||||
func (q *Queries) SelectApprovalsMessageId(ctx context.Context, messageId string) ([]Approval, error) {
|
||||
rows, err := q.db.QueryContext(ctx, selectApprovalsMessageId, messageId)
|
||||
func (q *Queries) SelectExecutionsMessageId(ctx context.Context, messageId string) ([]Execution, error) {
|
||||
rows, err := q.db.QueryContext(ctx, selectExecutionsMessageId, messageId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalsMessageId: %w", err)
|
||||
return nil, fmt.Errorf("SelectExecutionsMessageId: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var approvals []Approval
|
||||
var executions []Execution
|
||||
for rows.Next() {
|
||||
var (
|
||||
a Approval
|
||||
e Execution
|
||||
createdAt string
|
||||
updatedAt string
|
||||
)
|
||||
if err := rows.Scan(
|
||||
&createdAt,
|
||||
&a.Id,
|
||||
&a.MessageId,
|
||||
&a.Status,
|
||||
&a.ToolCallId,
|
||||
&e.Id,
|
||||
&e.MessageId,
|
||||
&e.Status,
|
||||
&e.ToolCallId,
|
||||
&updatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalsMessageId: %w", err)
|
||||
return nil, fmt.Errorf("SelectExecutionsMessageId: %w", err)
|
||||
}
|
||||
if a.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalsMessageId: parse created_at: %w", err)
|
||||
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectExecutionsMessageId: parse created_at: %w", err)
|
||||
}
|
||||
if a.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalsMessageId: parse updated_at: %w", err)
|
||||
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectExecutionsMessageId: parse updated_at: %w", err)
|
||||
}
|
||||
approvals = append(approvals, a)
|
||||
executions = append(executions, e)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("SelectApprovalsMessageId: %w", err)
|
||||
return nil, fmt.Errorf("SelectExecutionsMessageId: %w", err)
|
||||
}
|
||||
return approvals, nil
|
||||
return executions, nil
|
||||
}
|
||||
|
||||
const selectPendingApprovals = `
|
||||
const selectPendingExecutions = `
|
||||
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
||||
FROM approvals
|
||||
FROM executions
|
||||
WHERE status = 'pending'
|
||||
ORDER BY created_at
|
||||
`
|
||||
|
||||
// SelectPendingApprovals returns all pending approvals across all
|
||||
// SelectPendingExecutions returns all pending execution rows across all
|
||||
// conversations, ordered by creation time. This serves as the global
|
||||
// inbox for the UI.
|
||||
func (q *Queries) SelectPendingApprovals(ctx context.Context) ([]Approval, error) {
|
||||
rows, err := q.db.QueryContext(ctx, selectPendingApprovals)
|
||||
func (q *Queries) SelectPendingExecutions(ctx context.Context) ([]Execution, error) {
|
||||
rows, err := q.db.QueryContext(ctx, selectPendingExecutions)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingApprovals: %w", err)
|
||||
return nil, fmt.Errorf("SelectPendingExecutions: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var approvals []Approval
|
||||
var executions []Execution
|
||||
for rows.Next() {
|
||||
var (
|
||||
a Approval
|
||||
e Execution
|
||||
createdAt string
|
||||
updatedAt string
|
||||
)
|
||||
if err := rows.Scan(
|
||||
&createdAt,
|
||||
&a.Id,
|
||||
&a.MessageId,
|
||||
&a.Status,
|
||||
&a.ToolCallId,
|
||||
&e.Id,
|
||||
&e.MessageId,
|
||||
&e.Status,
|
||||
&e.ToolCallId,
|
||||
&updatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingApprovals: %w", err)
|
||||
return nil, fmt.Errorf("SelectPendingExecutions: %w", err)
|
||||
}
|
||||
if a.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingApprovals: parse created_at: %w", err)
|
||||
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingExecutions: parse created_at: %w", err)
|
||||
}
|
||||
if a.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingApprovals: parse updated_at: %w", err)
|
||||
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingExecutions: parse updated_at: %w", err)
|
||||
}
|
||||
approvals = append(approvals, a)
|
||||
executions = append(executions, e)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("SelectPendingApprovals: %w", err)
|
||||
return nil, fmt.Errorf("SelectPendingExecutions: %w", err)
|
||||
}
|
||||
return approvals, nil
|
||||
return executions, nil
|
||||
}
|
||||
|
||||
const updateApprovalStatus = `
|
||||
UPDATE approvals
|
||||
const updateExecutionStatus = `
|
||||
UPDATE executions
|
||||
SET status = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
// UpdateApprovalStatusParams holds arguments for UpdateApprovalStatus.
|
||||
type UpdateApprovalStatusParams struct {
|
||||
// UpdateExecutionStatusParams holds arguments for UpdateExecutionStatus.
|
||||
type UpdateExecutionStatusParams struct {
|
||||
Id string
|
||||
Status ApprovalStatus
|
||||
Status ExecutionStatus
|
||||
}
|
||||
|
||||
// UpdateApprovalStatus transitions an approval's status and bumps
|
||||
// UpdateExecutionStatus transitions an execution row's status and bumps
|
||||
// updated_at. The caller is responsible for valid transitions.
|
||||
func (q *Queries) UpdateApprovalStatus(ctx context.Context, arg UpdateApprovalStatusParams) error {
|
||||
func (q *Queries) UpdateExecutionStatus(ctx context.Context, arg UpdateExecutionStatusParams) error {
|
||||
_, err := q.db.ExecContext(
|
||||
ctx,
|
||||
updateApprovalStatus,
|
||||
updateExecutionStatus,
|
||||
arg.Status,
|
||||
arg.Id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("UpdateApprovalStatus: %w", err)
|
||||
return fmt.Errorf("UpdateExecutionStatus: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user