-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sqlite helper and some integration tests
- Loading branch information
1 parent
9ab4739
commit 81eb9b7
Showing
8 changed files
with
216 additions
and
52 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 +1,5 @@ | ||
-- name: UpsertCache :exec | ||
insert or replace into Cache(domain, key, value, timestamp) values (?, ?, ?, ?); | ||
|
||
-- name: QueryCache :one | ||
-- name: QueryCache :many | ||
select value, timestamp from Cache where domain = ? and key = ?; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,45 @@ | ||
package sqlite | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
// enable the sqlite3 driver. | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/nix-community/go-nix/pkg/sqlite/binary_cache_v6" | ||
"github.com/nix-community/go-nix/pkg/sqlite/eval_cache_v5" | ||
"github.com/nix-community/go-nix/pkg/sqlite/fetcher_cache_v2" | ||
"github.com/nix-community/go-nix/pkg/sqlite/nix_v10" | ||
) | ||
|
||
func BinaryCacheV6(dsn string) (*sql.DB, *binary_cache_v6.Queries, error) { | ||
db, err := sql.Open("sqlite3", dsn) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to open database: %w", err) | ||
} | ||
return db, binary_cache_v6.New(db), nil | ||
} | ||
|
||
func EvalCacheV5(dsn string) (*sql.DB, *eval_cache_v5.Queries, error) { | ||
db, err := sql.Open("sqlite3", dsn) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to open database: %w", err) | ||
} | ||
return db, eval_cache_v5.New(db), nil | ||
} | ||
|
||
func FetcherCacheV2(dsn string) (*sql.DB, *fetcher_cache_v2.Queries, error) { | ||
db, err := sql.Open("sqlite3", dsn) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to open database: %w", err) | ||
} | ||
return db, fetcher_cache_v2.New(db), nil | ||
} | ||
|
||
func NixV10(dsn string) (*sql.DB, *nix_v10.Queries, error) { | ||
db, err := sql.Open("sqlite3", dsn) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to open database: %w", err) | ||
} | ||
return db, nix_v10.New(db), nil | ||
} |
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,71 @@ | ||
//go:build integration | ||
|
||
package sqlite | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/adrg/xdg" | ||
"github.com/nix-community/go-nix/pkg/sqlite/fetcher_cache_v2" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBinaryCacheV6(t *testing.T) { | ||
as := require.New(t) | ||
|
||
// open our user-specific binary cache db | ||
path, err := xdg.CacheFile("nix/binary-cache-v6.sqlite") | ||
as.NoError(err, "failed to resolve binary cache file") | ||
as.FileExists(path) | ||
|
||
// open the sqlite db | ||
db, queries, err := BinaryCacheV6(fmt.Sprintf("file:%s?mode=ro", path)) | ||
as.NoError(err) | ||
defer db.Close() | ||
|
||
// perform a basic query, we aren't interested in the result | ||
_, err = queries.QueryLastPurge(context.Background()) | ||
as.NoError(err) | ||
} | ||
|
||
func TestFetcherCacheV2(t *testing.T) { | ||
as := require.New(t) | ||
|
||
// open our user-specific binary cache db | ||
path, err := xdg.CacheFile("nix/fetcher-cache-v2.sqlite") | ||
as.NoError(err, "failed to resolve fetcher cache file") | ||
as.FileExists(path) | ||
|
||
// open the sqlite db | ||
db, queries, err := FetcherCacheV2(fmt.Sprintf("file:%s?mode=ro", path)) | ||
as.NoError(err) | ||
defer db.Close() | ||
|
||
// perform a basic query, we aren't interested in the result | ||
_, err = queries.QueryCache(context.Background(), fetcher_cache_v2.QueryCacheParams{}) | ||
as.NoError(err) | ||
} | ||
|
||
func TestNixV10(t *testing.T) { | ||
as := require.New(t) | ||
|
||
// pull down a known path | ||
path := "/nix/store/kz5clxh7s1n0fnx6d37c1wc2cs9qm53q-hello-2.12.1" | ||
as.NoError(exec.Command("nix", "build", "--no-link", "--refresh", path).Run(), "failed to pull hello path") | ||
|
||
// open the sqlite db | ||
db, queries, err := NixV10("file:/nix/var/nix/db/db.sqlite?mode=ro") | ||
as.NoError(err) | ||
defer db.Close() | ||
|
||
// query the path we just pulled down | ||
info, err := queries.QueryPathInfo(context.Background(), path) | ||
as.NoError(err) | ||
as.Equal("sha256:f8340af15f7996faded748bea9e2d0b82a6f7c96417b03f7fa8e1a6a873748e8", info.Hash) | ||
as.Equal("/nix/store/qnavcbp5ydyd12asgz7rpr7is7hlswaz-hello-2.12.1.drv", info.Deriver.String) | ||
as.Equal(int64(226560), info.Narsize.Int64) | ||
} |