diff --git a/knotmirror/db/db.go b/knotmirror/db/db.go --- a/knotmirror/db/db.go +++ b/knotmirror/db/db.go @@ -10,6 +10,12 @@ _ "github.com/jackc/pgx/v5/stdlib" "tangled.org/core/log" ) +type DBTX interface { + ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) + QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) + QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row +} + func Make(ctx context.Context, dbUrl string, maxConns int) (*sql.DB, error) { db, err := sql.Open("pgx", dbUrl) if err != nil { diff --git a/knotmirror/db/hosts.go b/knotmirror/db/hosts.go --- a/knotmirror/db/hosts.go +++ b/knotmirror/db/hosts.go @@ -10,7 +10,7 @@ "tangled.org/core/knotmirror/models" ) -func UpsertHost(ctx context.Context, e *sql.DB, host *models.Host) error { +func UpsertHost(ctx context.Context, e DBTX, host *models.Host) error { if _, err := e.ExecContext(ctx, `insert into hosts (hostname, no_ssl, status, last_seq) values ($1, $2, $3, $4) @@ -29,7 +29,7 @@ } return nil } -func GetHost(ctx context.Context, e *sql.DB, hostname string) (*models.Host, error) { +func GetHost(ctx context.Context, e DBTX, hostname string) (*models.Host, error) { var host models.Host if err := e.QueryRowContext(ctx, `select hostname, no_ssl, status, last_seq @@ -70,7 +70,7 @@ } return tx.Commit() } -func ListHosts(ctx context.Context, e *sql.DB, status models.HostStatus) ([]models.Host, error) { +func ListHosts(ctx context.Context, e DBTX, status models.HostStatus) ([]models.Host, error) { rows, err := e.QueryContext(ctx, `select hostname, no_ssl, status, last_seq from hosts diff --git a/knotmirror/db/repos.go b/knotmirror/db/repos.go --- a/knotmirror/db/repos.go +++ b/knotmirror/db/repos.go @@ -11,7 +11,7 @@ "tangled.org/core/appview/pagination" "tangled.org/core/knotmirror/models" ) -func UpsertRepo(ctx context.Context, e *sql.DB, repo *models.Repo) error { +func UpsertRepo(ctx context.Context, e DBTX, repo *models.Repo) error { if repo.RepoDid == "" { return fmt.Errorf("upsert repo: repo_did is required") } @@ -48,7 +48,7 @@ } return nil } -func UpdateRepoState(ctx context.Context, e *sql.DB, repoDid syntax.DID, state models.RepoState) error { +func UpdateRepoState(ctx context.Context, e DBTX, repoDid syntax.DID, state models.RepoState) error { if _, err := e.ExecContext(ctx, `update repos set state = $1 @@ -61,7 +61,7 @@ } return nil } -func DeleteRepo(ctx context.Context, e *sql.DB, did syntax.DID, rkey syntax.RecordKey) error { +func DeleteRepo(ctx context.Context, e DBTX, did syntax.DID, rkey syntax.RecordKey) error { if _, err := e.ExecContext(ctx, `delete from repos where did = $1 and rkey = $2`, did, @@ -107,7 +107,7 @@ } return &repo, nil } -func GetRepoByRepoDid(ctx context.Context, e *sql.DB, repoDid syntax.DID) (*models.Repo, error) { +func GetRepoByRepoDid(ctx context.Context, e DBTX, repoDid syntax.DID) (*models.Repo, error) { row := e.QueryRowContext(ctx, `select`+repoColumns+` from repos @@ -124,7 +124,7 @@ } return repo, nil } -func GetRepoByAtUri(ctx context.Context, e *sql.DB, aturi syntax.ATURI) (*models.Repo, error) { +func GetRepoByAtUri(ctx context.Context, e DBTX, aturi syntax.ATURI) (*models.Repo, error) { row := e.QueryRowContext(ctx, `select`+repoColumns+` from repos @@ -141,7 +141,7 @@ } return repo, nil } -func ListRepos(ctx context.Context, e *sql.DB, page pagination.Page, did, knot, state, name string) ([]models.Repo, error) { +func ListRepos(ctx context.Context, e DBTX, page pagination.Page, did, knot, state, name string) ([]models.Repo, error) { var conditions []string var args []any @@ -200,7 +200,7 @@ return repos, nil } -func GetRepoCountsByState(ctx context.Context, e *sql.DB) (map[models.RepoState]int64, error) { +func GetRepoCountsByState(ctx context.Context, e DBTX) (map[models.RepoState]int64, error) { const q = ` SELECT state, COUNT(*) FROM repos