Skip to content

Commit 65e4309

Browse files
committed
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.
1 parent bd704c4 commit 65e4309

File tree

7 files changed

+170
-1
lines changed

7 files changed

+170
-1
lines changed

db/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
// daemon.
2323
//
2424
// NOTE: This MUST be updated when a new migration is added.
25-
LatestMigrationVersion = 4
25+
LatestMigrationVersion = 5
2626
)
2727

2828
// MigrationTarget is a functional option that can be passed to applyMigrations

db/sqlc/actions.sql.go

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP INDEX IF NOT EXISTS actions_state_idx;
2+
DROP INDEX IF NOT EXISTS actions_session_id_idx;
3+
DROP INDEX IF NOT EXISTS actions_feature_name_idx;
4+
DROP INDEX IF NOT EXISTS actions_created_at_idx;
5+
DROP TABLE IF EXISTS actions;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
CREATE TABLE IF NOT EXISTS actions(
2+
-- The ID of the action.
3+
id INTEGER PRIMARY KEY,
4+
5+
-- The session ID of the session that this action is associated with.
6+
-- This may be null for actions that are not coupled to a session.
7+
session_id BIGINT REFERENCES sessions(id) ON DELETE CASCADE,
8+
9+
-- The account ID of the account that this action is associated with.
10+
-- This may be null for actions that are not coupled to an account.
11+
account_id BIGINT REFERENCES accounts(id) ON DELETE CASCADE,
12+
13+
-- An ID derived from the macaroon used to perform the action.
14+
macaroon_identifier BLOB,
15+
16+
-- The name of the entity who performed the action.
17+
actor_name TEXT,
18+
19+
-- The name of the feature that the action is being performed by.
20+
feature_name TEXT,
21+
22+
-- Meta info detailing what caused this action to be executed.
23+
action_trigger TEXT,
24+
25+
-- Meta info detailing what the intended outcome of this action will be.
26+
intent TEXT,
27+
28+
-- Extra structured JSON data that an actor can send along with the
29+
-- action as json.
30+
structured_json_data BLOB,
31+
32+
-- The method URI that was called.
33+
rpc_method TEXT NOT NULL,
34+
35+
-- The method parameters of the request in JSON form.
36+
rpc_params_json BLOB,
37+
38+
-- The time at which this action was created.
39+
created_at TIMESTAMP NOT NULL,
40+
41+
-- The current state of the action.
42+
action_state SMALLINT NOT NULL,
43+
44+
-- Human-readable reason for why the action failed.
45+
-- It will only be set if state is ActionStateError (3).
46+
error_reason TEXT
47+
);
48+
49+
CREATE INDEX IF NOT EXISTS actions_state_idx ON actions(action_state);
50+
CREATE INDEX IF NOT EXISTS actions_session_id_idx ON actions(session_id);
51+
CREATE INDEX IF NOT EXISTS actions_feature_name_idx ON actions(feature_name);
52+
CREATE INDEX IF NOT EXISTS actions_created_at_idx ON actions(created_at);

db/sqlc/models.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/querier.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/queries/actions.sql

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

0 commit comments

Comments
 (0)