-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlc: define the schema/queries and make use of generated models (#71)
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
Showing
15 changed files
with
633 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL="sqlite:db/database.sqlite3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,6 @@ go.work.sum | |
|
||
# Go binaries | ||
/ncps | ||
|
||
# SQLite database | ||
/db/database.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) <= ?; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
@@ -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" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.