db: add actions schemas and queries

In this commit we define the schema for the `actions` table along with
various queries we will need for interacting with the table. NOTE: we
will also add some of our own queries manually in commits to follow.
This commit is contained in:
Elle Mouton 2025-05-13 14:00:35 +02:00
parent bd704c41e3
commit 65e4309f9c
No known key found for this signature in database
GPG key ID: D7D916376026F177
7 changed files with 170 additions and 1 deletions

View file

@ -22,7 +22,7 @@ const (
// daemon.
//
// NOTE: This MUST be updated when a new migration is added.
LatestMigrationVersion = 4
LatestMigrationVersion = 5
)
// MigrationTarget is a functional option that can be passed to applyMigrations

78
db/sqlc/actions.sql.go Normal file
View file

@ -0,0 +1,78 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: actions.sql
package sqlc
import (
"context"
"database/sql"
"time"
)
const insertAction = `-- name: InsertAction :one
INSERT INTO actions (
session_id, account_id, macaroon_identifier, actor_name, feature_name, action_trigger,
intent, structured_json_data, rpc_method, rpc_params_json, created_at,
action_state, error_reason
) VALUES (
$1, $2, $3, $4, $5, $6,
$7, $8, $9, $10, $11, $12, $13
) RETURNING id
`
type InsertActionParams struct {
SessionID sql.NullInt64
AccountID sql.NullInt64
MacaroonIdentifier []byte
ActorName sql.NullString
FeatureName sql.NullString
ActionTrigger sql.NullString
Intent sql.NullString
StructuredJsonData []byte
RpcMethod string
RpcParamsJson []byte
CreatedAt time.Time
ActionState int16
ErrorReason sql.NullString
}
func (q *Queries) InsertAction(ctx context.Context, arg InsertActionParams) (int64, error) {
row := q.db.QueryRowContext(ctx, insertAction,
arg.SessionID,
arg.AccountID,
arg.MacaroonIdentifier,
arg.ActorName,
arg.FeatureName,
arg.ActionTrigger,
arg.Intent,
arg.StructuredJsonData,
arg.RpcMethod,
arg.RpcParamsJson,
arg.CreatedAt,
arg.ActionState,
arg.ErrorReason,
)
var id int64
err := row.Scan(&id)
return id, err
}
const setActionState = `-- name: SetActionState :exec
UPDATE actions
SET action_state = $1,
error_reason = $2
WHERE id = $3
`
type SetActionStateParams struct {
ActionState int16
ErrorReason sql.NullString
ID int64
}
func (q *Queries) SetActionState(ctx context.Context, arg SetActionStateParams) error {
_, err := q.db.ExecContext(ctx, setActionState, arg.ActionState, arg.ErrorReason, arg.ID)
return err
}

View file

@ -0,0 +1,5 @@
DROP INDEX IF NOT EXISTS actions_state_idx;
DROP INDEX IF NOT EXISTS actions_session_id_idx;
DROP INDEX IF NOT EXISTS actions_feature_name_idx;
DROP INDEX IF NOT EXISTS actions_created_at_idx;
DROP TABLE IF EXISTS actions;

View file

@ -0,0 +1,52 @@
CREATE TABLE IF NOT EXISTS actions(
-- The ID of the action.
id INTEGER PRIMARY KEY,
-- The session ID of the session that this action is associated with.
-- This may be null for actions that are not coupled to a session.
session_id BIGINT REFERENCES sessions(id) ON DELETE CASCADE,
-- The account ID of the account that this action is associated with.
-- This may be null for actions that are not coupled to an account.
account_id BIGINT REFERENCES accounts(id) ON DELETE CASCADE,
-- An ID derived from the macaroon used to perform the action.
macaroon_identifier BLOB,
-- The name of the entity who performed the action.
actor_name TEXT,
-- The name of the feature that the action is being performed by.
feature_name TEXT,
-- Meta info detailing what caused this action to be executed.
action_trigger TEXT,
-- Meta info detailing what the intended outcome of this action will be.
intent TEXT,
-- Extra structured JSON data that an actor can send along with the
-- action as json.
structured_json_data BLOB,
-- The method URI that was called.
rpc_method TEXT NOT NULL,
-- The method parameters of the request in JSON form.
rpc_params_json BLOB,
-- The time at which this action was created.
created_at TIMESTAMP NOT NULL,
-- The current state of the action.
action_state SMALLINT NOT NULL,
-- Human-readable reason for why the action failed.
-- It will only be set if state is ActionStateError (3).
error_reason TEXT
);
CREATE INDEX IF NOT EXISTS actions_state_idx ON actions(action_state);
CREATE INDEX IF NOT EXISTS actions_session_id_idx ON actions(session_id);
CREATE INDEX IF NOT EXISTS actions_feature_name_idx ON actions(feature_name);
CREATE INDEX IF NOT EXISTS actions_created_at_idx ON actions(created_at);

View file

@ -37,6 +37,23 @@ type AccountPayment struct {
FullAmountMsat int64
}
type Action struct {
ID int64
SessionID sql.NullInt64
AccountID sql.NullInt64
MacaroonIdentifier []byte
ActorName sql.NullString
FeatureName sql.NullString
ActionTrigger sql.NullString
Intent sql.NullString
StructuredJsonData []byte
RpcMethod string
RpcParamsJson []byte
CreatedAt time.Time
ActionState int16
ErrorReason sql.NullString
}
type Feature struct {
ID int64
Name string

View file

@ -46,6 +46,7 @@ type Querier interface {
GetSessionPrivacyFlags(ctx context.Context, sessionID int64) ([]SessionPrivacyFlag, error)
GetSessionsInGroup(ctx context.Context, groupID sql.NullInt64) ([]Session, error)
InsertAccount(ctx context.Context, arg InsertAccountParams) (int64, error)
InsertAction(ctx context.Context, arg InsertActionParams) (int64, error)
InsertKVStoreRecord(ctx context.Context, arg InsertKVStoreRecordParams) error
InsertPrivacyPair(ctx context.Context, arg InsertPrivacyPairParams) error
InsertSession(ctx context.Context, arg InsertSessionParams) (int64, error)
@ -60,6 +61,7 @@ type Querier interface {
ListSessionsByState(ctx context.Context, state int16) ([]Session, error)
ListSessionsByType(ctx context.Context, type_ int16) ([]Session, error)
SetAccountIndex(ctx context.Context, arg SetAccountIndexParams) error
SetActionState(ctx context.Context, arg SetActionStateParams) error
SetSessionGroupID(ctx context.Context, arg SetSessionGroupIDParams) error
SetSessionRemotePublicKey(ctx context.Context, arg SetSessionRemotePublicKeyParams) error
SetSessionRevokedAt(ctx context.Context, arg SetSessionRevokedAtParams) error

View file

@ -0,0 +1,15 @@
-- name: InsertAction :one
INSERT INTO actions (
session_id, account_id, macaroon_identifier, actor_name, feature_name, action_trigger,
intent, structured_json_data, rpc_method, rpc_params_json, created_at,
action_state, error_reason
) VALUES (
$1, $2, $3, $4, $5, $6,
$7, $8, $9, $10, $11, $12, $13
) RETURNING id;
-- name: SetActionState :exec
UPDATE actions
SET action_state = $1,
error_reason = $2
WHERE id = $3;