Skip to content

Commit 00d07c2

Browse files
committed
feat(history): add db models/queries
1 parent d119de9 commit 00d07c2

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- +goose Up
2+
CREATE TABLE history (
3+
id INTEGER PRIMARY KEY AUTOINCREMENT,
4+
collection_id INTEGER,
5+
collection_name TEXT,
6+
endpoint_name TEXT,
7+
method TEXT NOT NULL,
8+
url TEXT NOT NULL,
9+
status_code INTEGER NOT NULL,
10+
duration INTEGER NOT NULL, -- duration in milliseconds
11+
response_size INTEGER DEFAULT 0,
12+
request_headers TEXT DEFAULT '{}',
13+
query_params TEXT DEFAULT '{}',
14+
request_body TEXT DEFAULT '',
15+
response_body TEXT DEFAULT '',
16+
response_headers TEXT DEFAULT '{}',
17+
executed_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
18+
);
19+
20+
CREATE INDEX idx_history_collection_id ON history(collection_id);
21+
CREATE INDEX idx_history_executed_at ON history(executed_at);
22+
CREATE INDEX idx_history_status_code ON history(status_code);
23+
24+
-- +goose Down
25+
DROP INDEX IF EXISTS idx_history_status_code;
26+
DROP INDEX IF EXISTS idx_history_executed_at;
27+
DROP INDEX IF EXISTS idx_history_collection_id;
28+
DROP TABLE IF EXISTS history;

db/queries/history.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- name: CreateHistoryEntry :one
2+
INSERT INTO history (
3+
collection_id, collection_name, endpoint_name,
4+
method, url, status_code, duration, response_size,
5+
request_headers, query_params, request_body,
6+
response_body, response_headers, executed_at
7+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
8+
RETURNING *;
9+
10+
-- name: GetHistoryById :one
11+
SELECT * FROM history
12+
WHERE id = ?;
13+
14+
-- name: GetHistoryByCollection :many
15+
SELECT id, endpoint_name, status_code, executed_at, url, method FROM history
16+
WHERE collection_id = ?
17+
ORDER BY executed_at DESC
18+
LIMIT ? OFFSET ?;
19+
20+
-- name: DeleteHistoryEntry :exec
21+
DELETE FROM history
22+
WHERE id = ?;
23+
24+
-- name: DeleteOldHistory :exec
25+
DELETE FROM history
26+
WHERE executed_at < datetime('now', '-30 days');

internal/database/history.sql.go

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

internal/database/models.go

Lines changed: 22 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)