Skip to content

Commit ccfe14f

Browse files
committed
chore: add db schema with sqlc config
This commit adds the DB schema and the needed sqlc config to generate the code needed to interact with this schema. Change-Id: b8cc15ee3f847ddbe7f2224a1bd03036 Relates-To: prometheus#4315
1 parent a505530 commit ccfe14f

File tree

11 files changed

+487
-0
lines changed

11 files changed

+487
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
!/circle.yml
2020
!/.travis.yml
2121
!/.promu.yml
22+
!/sqlc.yaml
2223
!/api/v2/openapi.yaml
2324
!.github/workflows/*.yml

analytics/internal/db.go

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

analytics/internal/models.go

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

analytics/internal/queries.sql.go

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

analytics/queries.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- name: PlaceHolder :exec
2+
select
3+
1;

analytics/schema.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
CREATE TYPE "alert_states" AS ENUM (
2+
'unprocessed',
3+
'active',
4+
'firing',
5+
'suppressed',
6+
'deleted'
7+
);
8+
9+
CREATE TYPE "suppressed_reason" AS ENUM ('silenced', 'inhibited');
10+
11+
CREATE TABLE "alerts" (
12+
"id" UUID PRIMARY KEY,
13+
"created_at" timestamp DEFAULT (now ()) NOT NUll,
14+
"fingerprint" string UNIQUE NOT NUll,
15+
"alertname" string NOT NUll
16+
);
17+
18+
CREATE TABLE "alert_states" (
19+
"id" UUID PRIMARY KEY,
20+
"created_at" timestamp DEFAULT (now ()) NOT NUll,
21+
"alert" STRING REFERENCES alerts (fingerprint) NOT NUll,
22+
"state" alert_states NOT NUll,
23+
"suppressed_by" string REFERENCES alerts (fingerprint),
24+
"suppressed_reason" suppressed_reason
25+
);
26+
27+
CREATE TABLE "labels" (
28+
"id" UUID PRIMARY KEY,
29+
"key" string NOT NUll,
30+
"value" string NOT NUll
31+
);
32+
33+
CREATE TABLE "annotations" (
34+
"id" UUID PRIMARY KEY,
35+
"key" string NOT NUll,
36+
"value" string NOT NUll
37+
);
38+
39+
CREATE TABLE "alert_label_set" (
40+
"id" UUID PRIMARY KEY,
41+
"alert" string REFERENCES alerts (fingerprint) NOT NUll,
42+
"label" UUID REFERENCES labels (id) NOT NUll
43+
);
44+
45+
CREATE TABLE "alert_annotation_set" (
46+
"id" UUID PRIMARY KEY,
47+
"alert" string REFERENCES alerts (fingerprint) NOT NULL,
48+
"annotation" UUID REFERENCES annotations (id) NOT NULL
49+
);
50+
51+
CREATE UNIQUE INDEX labels_key_value_unique ON "labels" ("key", "value");
52+
53+
CREATE UNIQUE INDEX anno_key_value_unique ON "annotations" ("key", "value");
54+
55+
CREATE UNIQUE INDEX alert_lbl_set_alert_lbl_unique ON "alert_label_set" ("alert", "label");
56+
57+
CREATE UNIQUE INDEX alert_anno_set_alert_lbl_unique ON "alert_annotation_set" ("alert", "annotation");

sqlc.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# TODO(khellemun): add sqlc make target
2+
version: "2"
3+
sql:
4+
- engine: "postgresql"
5+
queries: "types/queries.sql"
6+
schema: "analytics/schema.sql"
7+
gen:
8+
go:
9+
package: "internal"
10+
out: "types/internal"
11+
overrides:
12+
- db_type: "uuid"
13+
go_type:
14+
import: "github.com/gofrs/uuid"
15+
type: "UUID"
16+
- db_type: "uinteger"
17+
go_type:
18+
type: "uint"
19+
- db_type: "ubigint"
20+
go_type:
21+
type: "uint64"
22+
- engine: "postgresql"
23+
queries: "analytics/queries.sql"
24+
schema: "analytics/schema.sql"
25+
gen:
26+
go:
27+
package: "internal"
28+
out: "analytics/internal"
29+
overrides:
30+
- db_type: "uuid"
31+
go_type:
32+
import: "github.com/gofrs/uuid"
33+
type: "UUID"
34+
- db_type: "uinteger"
35+
go_type:
36+
type: "uint"
37+
- db_type: "ubigint"
38+
go_type:
39+
type: "uint64"

types/internal/db.go

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

0 commit comments

Comments
 (0)