Skip to content

Commit 45bf999

Browse files
committed
db/sqlc: kvstores schemas & queries
In this commit, we define the schemas and queries that are needed to implement the firewalldb's KVStores in SQL.
1 parent 1e257d1 commit 45bf999

File tree

7 files changed

+437
-1
lines changed

7 files changed

+437
-1
lines changed

db/migrations.go

+1-1
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 = 2
25+
LatestMigrationVersion = 3
2626
)
2727

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

db/sqlc/kvstores.sql.go

+260
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Drop indexes first.
2+
DROP INDEX IF EXISTS kvstores_lookup_idx;
3+
4+
-- Drop tables in reverse dependency order.
5+
DROP TABLE IF EXISTS kvstores;
6+
DROP TABLE IF EXISTS features;
7+
DROP TABLE IF EXISTS rules;
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- rules holds the unique names of the various rules that are used
2+
-- known to lit and used by the firewall db.
3+
CREATE TABLE IF NOT EXISTS rules (
4+
-- The auto incrementing primary key.
5+
id INTEGER PRIMARY KEY,
6+
7+
-- The unique name of the rule.
8+
name TEXT NOT NULL UNIQUE
9+
);
10+
11+
-- features holds the unique names of the various features that
12+
-- kvstores can be associated with.
13+
CREATE TABLE IF NOT EXISTS features (
14+
-- The auto incrementing primary key.
15+
id INTEGER PRIMARY KEY,
16+
17+
-- The unique name of the feature.
18+
name TEXT NOT NULL UNIQUE
19+
);
20+
21+
-- kvstores houses key-value pairs under various namespaces determined
22+
-- by the rule name, session ID, and feature name.
23+
CREATE TABLE IF NOT EXISTS kvstores (
24+
-- The auto incrementing primary key.
25+
id INTEGER PRIMARY KEY,
26+
27+
-- Whether this record is part of the permanent store.
28+
-- If false, it will be deleted on start-up.
29+
perm BOOLEAN NOT NULL,
30+
31+
-- The rule that this kv_store belongs to.
32+
-- If only the rule name is set, then this kv_store is a global
33+
-- kv_store.
34+
rule_id BIGINT REFERENCES rules(id) NOT NULL,
35+
36+
-- The session ID that this kv_store belongs to.
37+
-- If this is set, then this kv_store is a session-specific
38+
-- kv_store for the given rule.
39+
session_id BIGINT REFERENCES sessions(id) ON DELETE CASCADE,
40+
41+
-- The feature name that this kv_store belongs to.
42+
-- If this is set, then this kv_store is a feature-specific
43+
-- kvstore under the given session ID and rule name.
44+
-- If this is set, then session_id must also be set.
45+
feature_id BIGINT REFERENCES features(id),
46+
47+
-- The key of the entry.
48+
entry_key TEXT NOT NULL,
49+
50+
-- The value of the entry.
51+
value BLOB NOT NULL
52+
);
53+
54+
CREATE UNIQUE INDEX IF NOT EXISTS kvstores_lookup_idx
55+
ON kvstores (entry_key, rule_id, perm, session_id, feature_id);

db/sqlc/models.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/querier.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)