From e8b3d97fdcc62bda1c96252e291d7893014b2c5c Mon Sep 17 00:00:00 2001 From: Wael Nasreddine Date: Thu, 12 Dec 2024 21:23:27 -0800 Subject: [PATCH] pkg/database: add query field to the nars table (#102) Add query column to nars table - Add `query` column to nars table - Quote reserved keyword `query` in SQL statements - Update database models and queries to support new column --- ...20241213014846_add-query-to-nars-table.sql | 6 ++ db/query.sql | 4 +- pkg/database/models.go | 1 + pkg/database/query.sql.go | 80 ++++++++++--------- 4 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 db/migrations/20241213014846_add-query-to-nars-table.sql diff --git a/db/migrations/20241213014846_add-query-to-nars-table.sql b/db/migrations/20241213014846_add-query-to-nars-table.sql new file mode 100644 index 0000000..7f1e8f0 --- /dev/null +++ b/db/migrations/20241213014846_add-query-to-nars-table.sql @@ -0,0 +1,6 @@ +-- migrate:up +ALTER TABLE nars ADD COLUMN `query` TEXT NOT NULL DEFAULT ''; + + +-- migrate:down +ALTER TABLE nars DROP COLUMN `query`; diff --git a/db/query.sql b/db/query.sql index 41ba12c..f22f2ad 100644 --- a/db/query.sql +++ b/db/query.sql @@ -28,9 +28,9 @@ RETURNING *; -- name: CreateNar :one INSERT INTO nars ( - narinfo_id, hash, compression, file_size + narinfo_id, hash, compression, `query`, file_size ) VALUES ( - ?, ?, ?, ? + ?, ?, ?, ?, ? ) RETURNING *; diff --git a/pkg/database/models.go b/pkg/database/models.go index 7fab771..b9ed653 100644 --- a/pkg/database/models.go +++ b/pkg/database/models.go @@ -18,6 +18,7 @@ type Nar struct { CreatedAt time.Time UpdatedAt sql.NullTime LastAccessedAt sql.NullTime + Query string } type NarInfo struct { diff --git a/pkg/database/query.sql.go b/pkg/database/query.sql.go index bd1faca..1b4afee 100644 --- a/pkg/database/query.sql.go +++ b/pkg/database/query.sql.go @@ -11,34 +11,36 @@ import ( ) const createNar = `-- name: CreateNar :one -INSERT into nars ( - narinfo_id, hash, compression, file_size +INSERT INTO nars ( + narinfo_id, hash, compression, query, file_size ) VALUES ( - ?, ?, ?, ? + ?, ?, ?, ?, ? ) -RETURNING id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at +RETURNING id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at, "query" ` type CreateNarParams struct { NarInfoID int64 Hash string Compression string + Query string FileSize uint64 } // CreateNar // -// INSERT into nars ( -// narinfo_id, hash, compression, file_size +// INSERT INTO nars ( +// narinfo_id, hash, compression, query, file_size // ) VALUES ( -// ?, ?, ?, ? +// ?, ?, ?, ?, ? // ) -// RETURNING id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at +// RETURNING id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at, "query" func (q *Queries) CreateNar(ctx context.Context, arg CreateNarParams) (Nar, error) { row := q.db.QueryRowContext(ctx, createNar, arg.NarInfoID, arg.Hash, arg.Compression, + arg.Query, arg.FileSize, ) var i Nar @@ -51,25 +53,26 @@ func (q *Queries) CreateNar(ctx context.Context, arg CreateNarParams) (Nar, erro &i.CreatedAt, &i.UpdatedAt, &i.LastAccessedAt, + &i.Query, ) return i, err } const createNarInfo = `-- name: CreateNarInfo :one -INSERT into narinfos ( - hash +INSERT INTO narinfos ( + hash ) VALUES ( - ? + ? ) RETURNING id, hash, created_at, updated_at, last_accessed_at ` // CreateNarInfo // -// INSERT into narinfos ( -// hash +// INSERT INTO narinfos ( +// hash // ) VALUES ( -// ? +// ? // ) // RETURNING id, hash, created_at, updated_at, last_accessed_at func (q *Queries) CreateNarInfo(ctx context.Context, hash string) (NarInfo, error) { @@ -154,25 +157,23 @@ func (q *Queries) DeleteNarInfoByID(ctx context.Context, id int64) (int64, error } const getLeastUsedNars = `-- name: GetLeastUsedNars :many -SELECT - n1.id, n1.narinfo_id, n1.hash, n1.compression, n1.file_size, n1.created_at, n1.updated_at, n1.last_accessed_at +SELECT n1.id, n1.narinfo_id, n1.hash, n1.compression, n1.file_size, n1.created_at, n1.updated_at, n1.last_accessed_at, n1."query" FROM nars n1 WHERE ( - SELECT SUM(n2.file_size) - FROM nars n2 - WHERE n2.last_accessed_at <= n1.last_accessed_at + SELECT SUM(n2.file_size) + FROM nars n2 + WHERE n2.last_accessed_at <= n1.last_accessed_at ) <= ? ` // GetLeastUsedNars // -// SELECT -// n1.id, n1.narinfo_id, n1.hash, n1.compression, n1.file_size, n1.created_at, n1.updated_at, n1.last_accessed_at +// SELECT n1.id, n1.narinfo_id, n1.hash, n1.compression, n1.file_size, n1.created_at, n1.updated_at, n1.last_accessed_at, n1."query" // FROM nars n1 // WHERE ( -// SELECT SUM(n2.file_size) -// FROM nars n2 -// WHERE n2.last_accessed_at <= n1.last_accessed_at +// SELECT SUM(n2.file_size) +// FROM nars n2 +// WHERE n2.last_accessed_at <= n1.last_accessed_at // ) <= ? func (q *Queries) GetLeastUsedNars(ctx context.Context, fileSize uint64) ([]Nar, error) { rows, err := q.db.QueryContext(ctx, getLeastUsedNars, fileSize) @@ -192,6 +193,7 @@ func (q *Queries) GetLeastUsedNars(ctx context.Context, fileSize uint64) ([]Nar, &i.CreatedAt, &i.UpdatedAt, &i.LastAccessedAt, + &i.Query, ); err != nil { return nil, err } @@ -207,14 +209,14 @@ func (q *Queries) GetLeastUsedNars(ctx context.Context, fileSize uint64) ([]Nar, } const getNarByHash = `-- name: GetNarByHash :one -SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at +SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at, "query" FROM nars WHERE hash = ? ` // GetNarByHash // -// SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at +// SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at, "query" // FROM nars // WHERE hash = ? func (q *Queries) GetNarByHash(ctx context.Context, hash string) (Nar, error) { @@ -229,19 +231,20 @@ func (q *Queries) GetNarByHash(ctx context.Context, hash string) (Nar, error) { &i.CreatedAt, &i.UpdatedAt, &i.LastAccessedAt, + &i.Query, ) return i, err } const getNarByID = `-- name: GetNarByID :one -SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at +SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at, "query" FROM nars WHERE id = ? ` // GetNarByID // -// SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at +// SELECT id, narinfo_id, hash, compression, file_size, created_at, updated_at, last_accessed_at, "query" // FROM nars // WHERE id = ? func (q *Queries) GetNarByID(ctx context.Context, id int64) (Nar, error) { @@ -256,6 +259,7 @@ func (q *Queries) GetNarByID(ctx context.Context, id int64) (Nar, error) { &i.CreatedAt, &i.UpdatedAt, &i.LastAccessedAt, + &i.Query, ) return i, err } @@ -326,16 +330,18 @@ func (q *Queries) GetNarTotalSize(ctx context.Context) (sql.NullFloat64, error) const touchNar = `-- name: TouchNar :execrows UPDATE nars -SET last_accessed_at = CURRENT_TIMESTAMP, - updated_at = CURRENT_TIMESTAMP +SET + last_accessed_at = CURRENT_TIMESTAMP, + updated_at = CURRENT_TIMESTAMP WHERE hash = ? ` // TouchNar // // UPDATE nars -// SET last_accessed_at = CURRENT_TIMESTAMP, -// updated_at = CURRENT_TIMESTAMP +// SET +// last_accessed_at = CURRENT_TIMESTAMP, +// updated_at = CURRENT_TIMESTAMP // WHERE hash = ? func (q *Queries) TouchNar(ctx context.Context, hash string) (int64, error) { result, err := q.db.ExecContext(ctx, touchNar, hash) @@ -347,16 +353,18 @@ func (q *Queries) TouchNar(ctx context.Context, hash string) (int64, error) { const touchNarInfo = `-- name: TouchNarInfo :execrows UPDATE narinfos -SET last_accessed_at = CURRENT_TIMESTAMP, - updated_at = CURRENT_TIMESTAMP +SET + last_accessed_at = CURRENT_TIMESTAMP, + updated_at = CURRENT_TIMESTAMP WHERE hash = ? ` // TouchNarInfo // // UPDATE narinfos -// SET last_accessed_at = CURRENT_TIMESTAMP, -// updated_at = CURRENT_TIMESTAMP +// SET +// last_accessed_at = CURRENT_TIMESTAMP, +// updated_at = CURRENT_TIMESTAMP // WHERE hash = ? func (q *Queries) TouchNarInfo(ctx context.Context, hash string) (int64, error) { result, err := q.db.ExecContext(ctx, touchNarInfo, hash)