Skip to content

Commit

Permalink
sqlc: define the schema/queries and make use of generated models (#71)
Browse files Browse the repository at this point in the history
Use `sqlc generate` to generate the models and make use of them. The
command also generates queries, but they are not being used in this pr
to keep it small and manageable. Follow-up PR is coming.
  • Loading branch information
kalbasit authored Dec 11, 2024
1 parent 761dd5f commit 05395c3
Show file tree
Hide file tree
Showing 15 changed files with 633 additions and 87 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL="sqlite:db/database.sqlite3"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ go.work.sum

# Go binaries
/ncps

# SQLite database
/db/database.sqlite3
17 changes: 17 additions & 0 deletions db/migrations/20241210054814_create-narinfos-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- migrate:up
CREATE TABLE narinfos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP,
last_accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX idx_narinfos_id ON narinfos (id);
CREATE UNIQUE INDEX idx_narinfos_hash ON narinfos (hash);
CREATE INDEX idx_narinfos_last_accessed_at ON narinfos (last_accessed_at);

-- migrate:down
DROP INDEX idx_narinfos_id;
DROP INDEX idx_narinfos_hash;
DROP INDEX idx_narinfos_last_accessed_at;
DROP TABLE narinfos;
23 changes: 23 additions & 0 deletions db/migrations/20241210054829_create-nars-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- migrate:up
CREATE TABLE nars (
id INTEGER PRIMARY KEY AUTOINCREMENT,
narinfo_id INTEGER NOT NULL REFERENCES narinfos(id),
hash TEXT NOT NULL UNIQUE,
compression TEXT NOT NULL DEFAULT '',
file_size INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP,
last_accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX idx_nars_id ON nars (id);
CREATE UNIQUE INDEX idx_nars_hash ON nars (hash);
CREATE INDEX idx_nars_narinfo_id ON nars (narinfo_id);
CREATE INDEX idx_nars_last_accessed_at ON nars (last_accessed_at);

-- migrate:down
DROP INDEX idx_nars_id;
DROP INDEX idx_nars_hash;
DROP INDEX idx_nars_narinfo_id;
DROP INDEX idx_nars_last_accessed_at;
DROP TABLE nars;
77 changes: 77 additions & 0 deletions db/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
-- name: GetNarInfoByHash :one
SELECT *
FROM narinfos
WHERE hash = ?;

-- name: GetNarInfoByID :one
SELECT *
FROM narinfos
WHERE id = ?;

-- name: GetNarByHash :one
SELECT *
FROM nars
WHERE hash = ?;

-- name: GetNarByID :one
SELECT *
FROM nars
WHERE id = ?;

-- name: CreateNarInfo :one
INSERT into narinfos (
hash
) VALUES (
?
)
RETURNING *;

-- name: CreateNar :one
INSERT into nars (
narinfo_id, hash, compression, file_size
) VALUES (
?, ?, ?, ?
)
RETURNING *;

-- name: TouchNarInfo :exec
UPDATE narinfos
SET last_accessed_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE hash = ?;

-- name: TouchNar :exec
UPDATE nars
SET last_accessed_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE hash = ?;

-- name: DeleteNarInfoByHash :exec
DELETE FROM narinfos
WHERE hash = ?;

-- name: DeleteNarByHash :exec
DELETE FROM nars
WHERE hash = ?;

-- name: DeleteNarInfoByID :exec
DELETE FROM narinfos
WHERE id = ?;

-- name: DeleteNarByID :exec
DELETE FROM nars
WHERE id = ?;

-- name: GetNarTotalSize :one
SELECT SUM(file_size) AS total_size
FROM nars;

-- name: GetLeastUsedNars :many
SELECT
n1.*
FROM nars n1
WHERE (
SELECT SUM(n2.file_size)
FROM nars n2
WHERE n2.last_accessed_at <= n1.last_accessed_at
) <= ?;
4 changes: 3 additions & 1 deletion devbox.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.13.4/.schema/devbox.schema.json",
"packages": [
"[email protected]",
"[email protected]",
"[email protected]"
"[email protected]",
"[email protected]"
],
"shell": {
"init_hook": [
Expand Down
96 changes: 96 additions & 0 deletions devbox.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
{
"lockfile_version": "1",
"packages": {
"[email protected]": {
"last_modified": "2024-11-28T07:51:56Z",
"resolved": "github:NixOS/nixpkgs/226216574ada4c3ecefcbbec41f39ce4655f78ef#dbmate",
"source": "devbox-search",
"version": "2.22.0",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/b28qb15ypyj60p4bsv8n2h7dx1rd3i67-dbmate-2.22.0",
"default": true
}
],
"store_path": "/nix/store/b28qb15ypyj60p4bsv8n2h7dx1rd3i67-dbmate-2.22.0"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/r48rg5qsxv04b6kiyrgz2q9az5vp3dbx-dbmate-2.22.0",
"default": true
}
],
"store_path": "/nix/store/r48rg5qsxv04b6kiyrgz2q9az5vp3dbx-dbmate-2.22.0"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/4ai029322bsk0vvcbihvp0zymr5hsaq6-dbmate-2.22.0",
"default": true
}
],
"store_path": "/nix/store/4ai029322bsk0vvcbihvp0zymr5hsaq6-dbmate-2.22.0"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/9mv4isdm26wcy83ia1ga9pdwbch7fvri-dbmate-2.22.0",
"default": true
}
],
"store_path": "/nix/store/9mv4isdm26wcy83ia1ga9pdwbch7fvri-dbmate-2.22.0"
}
}
},
"[email protected]": {
"last_modified": "2024-11-28T07:51:56Z",
"resolved": "github:NixOS/nixpkgs/226216574ada4c3ecefcbbec41f39ce4655f78ef#go",
Expand Down Expand Up @@ -96,6 +144,54 @@
"store_path": "/nix/store/gr55w2x6wrzdvhhbzm4wc28cs4k7g7vr-golangci-lint-1.62.2"
}
}
},
"[email protected]": {
"last_modified": "2024-11-28T07:51:56Z",
"resolved": "github:NixOS/nixpkgs/226216574ada4c3ecefcbbec41f39ce4655f78ef#sqlc",
"source": "devbox-search",
"version": "1.27.0",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/nv7n7v77blj22ycymjxr7nbr26gdhfqb-sqlc-1.27.0",
"default": true
}
],
"store_path": "/nix/store/nv7n7v77blj22ycymjxr7nbr26gdhfqb-sqlc-1.27.0"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/3xklmmk7x08yr31k3wa0hks890sz98r9-sqlc-1.27.0",
"default": true
}
],
"store_path": "/nix/store/3xklmmk7x08yr31k3wa0hks890sz98r9-sqlc-1.27.0"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/8qhdd418krfkrmczn959m5hv9184b9yb-sqlc-1.27.0",
"default": true
}
],
"store_path": "/nix/store/8qhdd418krfkrmczn959m5hv9184b9yb-sqlc-1.27.0"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/6mcdgk9rg9g4kia87jjv25h90b2n9r62-sqlc-1.27.0",
"default": true
}
],
"store_path": "/nix/store/6mcdgk9rg9g4kia87jjv25h90b2n9r62-sqlc-1.27.0"
}
}
}
}
}
4 changes: 2 additions & 2 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (c *Cache) getNarFromStore(log log15.Logger, hash, compression string) (int
return 0, nil, fmt.Errorf("error fetching the nar record: %w", err)
}

if time.Since(nr.LastAccessedAt) > c.recordAgeIgnoreTouch {
if lat, err := nr.LastAccessedAt.Value(); err == nil && time.Since(lat.(time.Time)) > c.recordAgeIgnoreTouch {
if _, err := c.db.TouchNarRecord(tx, hash); err != nil {
return 0, nil, fmt.Errorf("error touching the nar record: %w", err)
}
Expand Down Expand Up @@ -583,7 +583,7 @@ func (c *Cache) getNarInfoFromStore(log log15.Logger, hash string) (*narinfo.Nar
return nil, fmt.Errorf("error fetching the narinfo record: %w", err)
}

if time.Since(nir.LastAccessedAt) > c.recordAgeIgnoreTouch {
if lat, err := nir.LastAccessedAt.Value(); err == nil && time.Since(lat.(time.Time)) > c.recordAgeIgnoreTouch {
if _, err := c.db.TouchNarInfoRecord(tx, hash); err != nil {
return nil, fmt.Errorf("error touching the narinfo record: %w", err)
}
Expand Down
Loading

0 comments on commit 05395c3

Please sign in to comment.