From 36e8bdb368bdd6a751adab92fe4fa5374c97857d Mon Sep 17 00:00:00 2001 From: Johannes Batzill Date: Tue, 20 Jun 2023 00:26:40 +0000 Subject: [PATCH] [GITHA] Introduce Generic `store` Package (#143) --- cli/operations/migrate/migrate.go | 2 +- cli/server/config.go | 9 ++++ cmd/gitness/wire.go | 1 + cmd/gitness/wire_gen.go | 3 +- .../api/controller/pullreq/comment_create.go | 2 +- internal/api/controller/pullreq/pr_list.go | 2 +- .../api/controller/pullreq/review_submit.go | 4 +- .../api/controller/pullreq/reviewer_add.go | 4 +- internal/api/controller/repo/create.go | 2 +- internal/api/controller/repo/delete_path.go | 2 +- internal/api/controller/repo/list_paths.go | 2 +- internal/api/controller/repo/move.go | 2 +- internal/api/controller/space/create.go | 2 +- internal/api/controller/space/delete_path.go | 2 +- internal/api/controller/space/list_paths.go | 2 +- internal/api/controller/space/move.go | 4 +- internal/api/controller/tx.go | 4 +- internal/api/controller/user/login.go | 2 +- internal/api/usererror/translate.go | 2 +- internal/bootstrap/bootstrap.go | 2 +- internal/services/webhook/events.go | 2 +- internal/services/webhook/trigger.go | 2 +- internal/store/database/check.go | 15 +++--- internal/store/database/check_req.go | 15 +++--- internal/store/database/code_comment.go | 13 +++--- internal/store/database/migrate/migrate.go | 40 +++++++++++----- internal/store/database/path.go | 46 ++++++++++--------- internal/store/database/principal.go | 26 ++++++----- internal/store/database/principal_info.go | 17 +++---- internal/store/database/principal_service.go | 25 +++++----- .../database/principal_service_account.go | 25 +++++----- internal/store/database/principal_test.go | 3 +- internal/store/database/principal_user.go | 33 ++++++------- internal/store/database/pullreq.go | 38 +++++++-------- internal/store/database/pullreq_activity.go | 34 +++++++------- internal/store/database/pullreq_reviewers.go | 17 +++---- internal/store/database/pullreq_reviews.go | 9 ++-- internal/store/database/repo.go | 36 ++++++++------- internal/store/database/repo_git_info.go | 7 +-- internal/store/database/space.go | 37 ++++++++------- internal/store/database/store_test.go | 4 +- internal/store/database/token.go | 19 ++++---- internal/store/database/webhook.go | 34 +++++++------- internal/store/database/webhook_execution.go | 19 ++++---- internal/store/database/wire.go | 18 ++++++-- store/database/config.go | 11 +++++ .../store => store}/database/dbtx/ctx.go | 0 {internal/store => store}/database/dbtx/db.go | 0 .../database/dbtx/interface.go | 0 .../store => store}/database/dbtx/locker.go | 0 .../store => store}/database/dbtx/runner.go | 0 .../database/dbtx/runner_test.go | 0 {internal/store => store}/database/dbtx/tx.go | 0 {internal/store => store}/database/store.go | 20 +++----- {internal/store => store}/database/util.go | 14 +++--- {internal/store => store}/database/util_pq.go | 0 .../store => store}/database/util_sqlite.go | 0 .../store => store}/database/util_test.go | 12 +++-- {internal/store => store}/errors.go | 0 59 files changed, 361 insertions(+), 285 deletions(-) create mode 100644 store/database/config.go rename {internal/store => store}/database/dbtx/ctx.go (100%) rename {internal/store => store}/database/dbtx/db.go (100%) rename {internal/store => store}/database/dbtx/interface.go (100%) rename {internal/store => store}/database/dbtx/locker.go (100%) rename {internal/store => store}/database/dbtx/runner.go (100%) rename {internal/store => store}/database/dbtx/runner_test.go (100%) rename {internal/store => store}/database/dbtx/tx.go (100%) rename {internal/store => store}/database/store.go (86%) rename {internal/store => store}/database/util.go (81%) rename {internal/store => store}/database/util_pq.go (100%) rename {internal/store => store}/database/util_sqlite.go (100%) rename {internal/store => store}/database/util_test.go (87%) rename {internal/store => store}/errors.go (100%) diff --git a/cli/operations/migrate/migrate.go b/cli/operations/migrate/migrate.go index ca95a65443..41ff644b08 100644 --- a/cli/operations/migrate/migrate.go +++ b/cli/operations/migrate/migrate.go @@ -9,7 +9,7 @@ import ( "fmt" "github.com/harness/gitness/cli/server" - "github.com/harness/gitness/internal/store/database" + "github.com/harness/gitness/store/database" "github.com/jmoiron/sqlx" "github.com/joho/godotenv" diff --git a/cli/server/config.go b/cli/server/config.go index f165b145d7..b1dea701f6 100644 --- a/cli/server/config.go +++ b/cli/server/config.go @@ -15,6 +15,7 @@ import ( "github.com/harness/gitness/gitrpc/server" "github.com/harness/gitness/internal/services/webhook" "github.com/harness/gitness/lock" + "github.com/harness/gitness/store/database" "github.com/harness/gitness/types" "github.com/kelseyhightower/envconfig" @@ -86,6 +87,14 @@ func getSanitizedMachineName() (string, error) { return hostName, nil } +// ProvideDatabaseConfig loads the database config from the main config. +func ProvideDatabaseConfig(config *types.Config) database.Config { + return database.Config{ + Driver: config.Database.Driver, + Datasource: config.Database.Datasource, + } +} + // ProvideGitRPCServerConfig loads the gitrpc server config from the environment. // It backfills certain config elements to work with cmdone. func ProvideGitRPCServerConfig() (server.Config, error) { diff --git a/cmd/gitness/wire.go b/cmd/gitness/wire.go index bd7fe53da9..ddd1bd0aa7 100644 --- a/cmd/gitness/wire.go +++ b/cmd/gitness/wire.go @@ -53,6 +53,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e cliserver.NewSystem, cliserver.ProvideRedis, bootstrap.WireSet, + cliserver.ProvideDatabaseConfig, database.WireSet, cache.WireSet, router.WireSet, diff --git a/cmd/gitness/wire_gen.go b/cmd/gitness/wire_gen.go index 0cc531b3e4..c34208fc2d 100644 --- a/cmd/gitness/wire_gen.go +++ b/cmd/gitness/wire_gen.go @@ -49,7 +49,8 @@ import ( func initSystem(ctx context.Context, config *types.Config) (*server.System, error) { principalUID := check.ProvidePrincipalUIDCheck() authorizer := authz.ProvideAuthorizer() - db, err := database.ProvideDatabase(ctx, config) + databaseConfig := server.ProvideDatabaseConfig(config) + db, err := database.ProvideDatabase(ctx, databaseConfig) if err != nil { return nil, err } diff --git a/internal/api/controller/pullreq/comment_create.go b/internal/api/controller/pullreq/comment_create.go index 5eda6f59b3..315c1923ef 100644 --- a/internal/api/controller/pullreq/comment_create.go +++ b/internal/api/controller/pullreq/comment_create.go @@ -13,7 +13,7 @@ import ( "github.com/harness/gitness/gitrpc" "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store" + "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" diff --git a/internal/api/controller/pullreq/pr_list.go b/internal/api/controller/pullreq/pr_list.go index f52b38c035..bbd2fe036f 100644 --- a/internal/api/controller/pullreq/pr_list.go +++ b/internal/api/controller/pullreq/pr_list.go @@ -9,7 +9,7 @@ import ( "fmt" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/pullreq/review_submit.go b/internal/api/controller/pullreq/review_submit.go index 61ebe56f91..bab5af0bef 100644 --- a/internal/api/controller/pullreq/review_submit.go +++ b/internal/api/controller/pullreq/review_submit.go @@ -14,8 +14,8 @@ import ( "github.com/harness/gitness/gitrpc" "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" diff --git a/internal/api/controller/pullreq/reviewer_add.go b/internal/api/controller/pullreq/reviewer_add.go index aef1d47191..a89f86f82c 100644 --- a/internal/api/controller/pullreq/reviewer_add.go +++ b/internal/api/controller/pullreq/reviewer_add.go @@ -13,8 +13,8 @@ import ( apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/repo/create.go b/internal/api/controller/repo/create.go index 5aaa6d40f5..eadf3e66ff 100644 --- a/internal/api/controller/repo/create.go +++ b/internal/api/controller/repo/create.go @@ -17,8 +17,8 @@ import ( "github.com/harness/gitness/internal/auth" "github.com/harness/gitness/internal/bootstrap" "github.com/harness/gitness/internal/paths" - "github.com/harness/gitness/internal/store/database/dbtx" "github.com/harness/gitness/resources" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/check" "github.com/harness/gitness/types/enum" diff --git a/internal/api/controller/repo/delete_path.go b/internal/api/controller/repo/delete_path.go index 625f5fc45d..288df08066 100644 --- a/internal/api/controller/repo/delete_path.go +++ b/internal/api/controller/repo/delete_path.go @@ -11,7 +11,7 @@ import ( apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/repo/list_paths.go b/internal/api/controller/repo/list_paths.go index 9d9858f82c..94e637888a 100644 --- a/internal/api/controller/repo/list_paths.go +++ b/internal/api/controller/repo/list_paths.go @@ -10,7 +10,7 @@ import ( apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/repo/move.go b/internal/api/controller/repo/move.go index 38efda9ac4..a4a87ab09f 100644 --- a/internal/api/controller/repo/move.go +++ b/internal/api/controller/repo/move.go @@ -12,7 +12,7 @@ import ( apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/auth" "github.com/harness/gitness/internal/paths" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/space/create.go b/internal/api/controller/space/create.go index a12251bf37..c5d27777a5 100644 --- a/internal/api/controller/space/create.go +++ b/internal/api/controller/space/create.go @@ -14,7 +14,7 @@ import ( "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" "github.com/harness/gitness/internal/paths" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/check" "github.com/harness/gitness/types/enum" diff --git a/internal/api/controller/space/delete_path.go b/internal/api/controller/space/delete_path.go index 8d8ef0d658..d50cd2a498 100644 --- a/internal/api/controller/space/delete_path.go +++ b/internal/api/controller/space/delete_path.go @@ -11,7 +11,7 @@ import ( apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/space/list_paths.go b/internal/api/controller/space/list_paths.go index ae0d57b66e..ec8fe7d7d6 100644 --- a/internal/api/controller/space/list_paths.go +++ b/internal/api/controller/space/list_paths.go @@ -10,7 +10,7 @@ import ( apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) diff --git a/internal/api/controller/space/move.go b/internal/api/controller/space/move.go index fdbeea6838..eb9cc66963 100644 --- a/internal/api/controller/space/move.go +++ b/internal/api/controller/space/move.go @@ -15,8 +15,8 @@ import ( "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" "github.com/harness/gitness/internal/paths" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/check" "github.com/harness/gitness/types/enum" diff --git a/internal/api/controller/tx.go b/internal/api/controller/tx.go index 38f2509d29..bdf8d00b18 100644 --- a/internal/api/controller/tx.go +++ b/internal/api/controller/tx.go @@ -8,8 +8,8 @@ import ( "context" "errors" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database/dbtx" "github.com/jmoiron/sqlx" ) diff --git a/internal/api/controller/user/login.go b/internal/api/controller/user/login.go index a8cd35adfd..32f8bb7a3c 100644 --- a/internal/api/controller/user/login.go +++ b/internal/api/controller/user/login.go @@ -14,8 +14,8 @@ import ( "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store" "github.com/harness/gitness/internal/token" + "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/rs/zerolog/log" diff --git a/internal/api/usererror/translate.go b/internal/api/usererror/translate.go index e30e8a1967..952260c9ca 100644 --- a/internal/api/usererror/translate.go +++ b/internal/api/usererror/translate.go @@ -11,7 +11,7 @@ import ( "github.com/harness/gitness/gitrpc" apiauth "github.com/harness/gitness/internal/api/auth" "github.com/harness/gitness/internal/services/webhook" - "github.com/harness/gitness/internal/store" + "github.com/harness/gitness/store" "github.com/harness/gitness/types/check" "github.com/harness/go-rbac" diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index 744e73a8ce..cdcb24a8c4 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -12,7 +12,7 @@ import ( "github.com/harness/gitness/internal/api/controller/service" "github.com/harness/gitness/internal/api/controller/user" "github.com/harness/gitness/internal/auth" - "github.com/harness/gitness/internal/store" + "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/rs/zerolog/log" diff --git a/internal/services/webhook/events.go b/internal/services/webhook/events.go index 4ab694769d..ad67644bea 100644 --- a/internal/services/webhook/events.go +++ b/internal/services/webhook/events.go @@ -10,7 +10,7 @@ import ( "fmt" "github.com/harness/gitness/events" - "github.com/harness/gitness/internal/store" + "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" diff --git a/internal/services/webhook/trigger.go b/internal/services/webhook/trigger.go index 271c089fd1..e8745d3caf 100644 --- a/internal/services/webhook/trigger.go +++ b/internal/services/webhook/trigger.go @@ -18,7 +18,7 @@ import ( "net/http" "time" - "github.com/harness/gitness/internal/store" + "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" "github.com/harness/gitness/version" diff --git a/internal/store/database/check.go b/internal/store/database/check.go index 2d99a204e4..35558b7419 100644 --- a/internal/store/database/check.go +++ b/internal/store/database/check.go @@ -11,7 +11,8 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -121,11 +122,11 @@ func (s *CheckStore) Upsert(ctx context.Context, check *types.Check) error { query, arg, err := db.BindNamed(sqlQuery, mapInternalCheck(check)) if err != nil { - return processSQLErrorf(err, "Failed to bind status check object") + return database.ProcessSQLErrorf(err, "Failed to bind status check object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&check.ID, &check.CreatedBy, &check.Created); err != nil { - return processSQLErrorf(err, "Upsert query failed") + return database.ProcessSQLErrorf(err, "Upsert query failed") } return nil @@ -133,7 +134,7 @@ func (s *CheckStore) Upsert(ctx context.Context, check *types.Check) error { // List returns a list of status check results for a specific commit in a repo. func (s *CheckStore) List(ctx context.Context, repoID int64, commitSHA string) ([]*types.Check, error) { - stmt := builder. + stmt := database.Builder. Select(checkColumns). From("checks"). Where("check_repo_id = ?", repoID). @@ -150,7 +151,7 @@ func (s *CheckStore) List(ctx context.Context, repoID int64, commitSHA string) ( db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed to execute list status checks query") + return nil, database.ProcessSQLErrorf(err, "Failed to execute list status checks query") } result, err := s.mapSliceCheck(ctx, dst) @@ -163,7 +164,7 @@ func (s *CheckStore) List(ctx context.Context, repoID int64, commitSHA string) ( // ListRecent returns a list of recently executed status checks in a repository. func (s *CheckStore) ListRecent(ctx context.Context, repoID int64, since time.Time) ([]string, error) { - stmt := builder. + stmt := database.Builder. Select("distinct check_uid"). From("checks"). Where("check_repo_id = ?", repoID). @@ -180,7 +181,7 @@ func (s *CheckStore) ListRecent(ctx context.Context, repoID int64, since time.Ti db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed to execute list recent status checks query") + return nil, database.ProcessSQLErrorf(err, "Failed to execute list recent status checks query") } return dst, nil diff --git a/internal/store/database/check_req.go b/internal/store/database/check_req.go index e120da1e26..e779be825b 100644 --- a/internal/store/database/check_req.go +++ b/internal/store/database/check_req.go @@ -9,7 +9,8 @@ import ( "fmt" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/jmoiron/sqlx" @@ -78,11 +79,11 @@ func (s *ReqCheckStore) Create(ctx context.Context, reqCheck *types.ReqCheck) er query, arg, err := db.BindNamed(sqlQuery, mapInternalReqCheck(reqCheck)) if err != nil { - return processSQLErrorf(err, "Failed to bind required status check object") + return database.ProcessSQLErrorf(err, "Failed to bind required status check object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&reqCheck.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -90,7 +91,7 @@ func (s *ReqCheckStore) Create(ctx context.Context, reqCheck *types.ReqCheck) er // List returns a list of required status checks for a repo. func (s *ReqCheckStore) List(ctx context.Context, repoID int64) ([]*types.ReqCheck, error) { - stmt := builder. + stmt := database.Builder. Select(reqCheckColumns). From("reqchecks"). Where("reqcheck_repo_id = ?", repoID). @@ -106,7 +107,7 @@ func (s *ReqCheckStore) List(ctx context.Context, repoID int64) ([]*types.ReqChe db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed to execute list required status checks query") + return nil, database.ProcessSQLErrorf(err, "Failed to execute list required status checks query") } result, err := s.mapSliceReqCheck(ctx, dst) @@ -119,7 +120,7 @@ func (s *ReqCheckStore) List(ctx context.Context, repoID int64) ([]*types.ReqChe // Delete removes a required status checks for a repo. func (s *ReqCheckStore) Delete(ctx context.Context, repoID, reqCheckID int64) error { - stmt := builder. + stmt := database.Builder. Delete("reqchecks"). Where("reqcheck_repo_id = ?", repoID). Where("reqcheck_id = ?", reqCheckID) @@ -133,7 +134,7 @@ func (s *ReqCheckStore) Delete(ctx context.Context, repoID, reqCheckID int64) er _, err = db.ExecContext(ctx, sql, args...) if err != nil { - return processSQLErrorf(err, "Failed to execute delete required status check query") + return database.ProcessSQLErrorf(err, "Failed to execute delete required status check query") } return nil diff --git a/internal/store/database/code_comment.go b/internal/store/database/code_comment.go index b69f5f9a91..040712a29e 100644 --- a/internal/store/database/code_comment.go +++ b/internal/store/database/code_comment.go @@ -9,7 +9,8 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -65,7 +66,7 @@ func (s *CodeCommentView) list(ctx context.Context, ,coalesce(pullreq_activity_code_comment_line_old, 1) as "pullreq_activity_code_comment_line_old" ,coalesce(pullreq_activity_code_comment_span_old, 0) as "pullreq_activity_code_comment_span_old"` - stmt := builder. + stmt := database.Builder. Select(codeCommentColumns). From("pullreq_activities"). Where("pullreq_activity_pullreq_id = ?", prID). @@ -95,7 +96,7 @@ func (s *CodeCommentView) list(ctx context.Context, db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &result, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed executing code comment list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing code comment list query") } return result, nil @@ -126,7 +127,7 @@ func (s *CodeCommentView) UpdateAll(ctx context.Context, codeComments []*types.C stmt, err := db.PrepareNamedContext(ctx, sqlQuery) if err != nil { - return processSQLErrorf(err, "Failed to prepare update statement for update code comments") + return database.ProcessSQLErrorf(err, "Failed to prepare update statement for update code comments") } updatedAt := time.Now() @@ -137,12 +138,12 @@ func (s *CodeCommentView) UpdateAll(ctx context.Context, codeComments []*types.C result, err := stmt.ExecContext(ctx, codeComment) if err != nil { - return processSQLErrorf(err, "Failed to update code comment=%d", codeComment.ID) + return database.ProcessSQLErrorf(err, "Failed to update code comment=%d", codeComment.ID) } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows for code comment=%d", codeComment.ID) + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows for code comment=%d", codeComment.ID) } if count == 0 { diff --git a/internal/store/database/migrate/migrate.go b/internal/store/database/migrate/migrate.go index e6e7ea35e4..9b65906c6a 100644 --- a/internal/store/database/migrate/migrate.go +++ b/internal/store/database/migrate/migrate.go @@ -22,17 +22,31 @@ var postgres embed.FS //go:embed sqlite/*.sql var sqlite embed.FS -const tableName = "migrations" +const ( + tableName = "migrations" + + postgresDriverName = "postgres" + postgresSourceDir = "postgres" + + sqliteDriverName = "sqlite3" + sqliteSourceDir = "sqlite" +) // Migrate performs the database migration. func Migrate(ctx context.Context, db *sqlx.DB) error { - opts := getMigrator(db) + opts, err := getMigrator(db) + if err != nil { + return fmt.Errorf("failed to get migrator: %w", err) + } return migrate.New(opts).MigrateUp(ctx) } // To performs the database migration to the specific version. func To(ctx context.Context, db *sqlx.DB, version string) error { - opts := getMigrator(db) + opts, err := getMigrator(db) + if err != nil { + return fmt.Errorf("failed to get migrator: %w", err) + } return migrate.New(opts).MigrateTo(ctx, version) } @@ -44,16 +58,18 @@ func Current(ctx context.Context, db *sqlx.DB) (string, error) { ) switch db.DriverName() { - case "sqlite3": + case sqliteDriverName: query = ` SELECT count(*) FROM sqlite_master WHERE name = ? and type = 'table'` - default: + case postgresDriverName: query = ` SELECT count(*) FROM information_schema.tables WHERE table_name = ? and table_schema = 'public'` + default: + return "", fmt.Errorf("unsupported driver '%s'", db.DriverName()) } if err := db.QueryRowContext(ctx, query, tableName).Scan(&migrationTableCount); err != nil { @@ -74,7 +90,7 @@ func Current(ctx context.Context, db *sqlx.DB) (string, error) { return version, nil } -func getMigrator(db *sqlx.DB) migrate.Options { +func getMigrator(db *sqlx.DB) (migrate.Options, error) { before := func(_ context.Context, _ *sql.Tx, version string) error { log.Trace().Str("version", version).Msg("migration started") return nil @@ -94,14 +110,16 @@ func getMigrator(db *sqlx.DB) migrate.Options { } switch db.DriverName() { - case "postgres": - folder, _ := fs.Sub(postgres, "postgres") + case sqliteDriverName: + folder, _ := fs.Sub(sqlite, sqliteSourceDir) + opts.FS = folder + case postgresDriverName: + folder, _ := fs.Sub(postgres, postgresSourceDir) opts.FS = folder default: - folder, _ := fs.Sub(sqlite, "sqlite") - opts.FS = folder + return migrate.Options{}, fmt.Errorf("unsupported driver '%s'", db.DriverName()) } - return opts + return opts, nil } diff --git a/internal/store/database/path.go b/internal/store/database/path.go index c63ddf0542..87b11ec3a9 100644 --- a/internal/store/database/path.go +++ b/internal/store/database/path.go @@ -12,7 +12,9 @@ import ( "github.com/harness/gitness/internal/paths" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -111,11 +113,11 @@ func (s *PathStore) Create(ctx context.Context, path *types.Path) error { query, arg, err := db.BindNamed(sqlQuery, dbPath) if err != nil { - return processSQLErrorf(err, "Failed to bind path object") + return database.ProcessSQLErrorf(err, "Failed to bind path object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&path.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -127,7 +129,7 @@ func (s *PathStore) findInner(ctx context.Context, id int64, lock bool) (*types. WHERE path_id = $1` if lock && !strings.HasPrefix(s.db.DriverName(), "sqlite") { - sqlQuery += "\n" + sqlForUpdate + sqlQuery += "\n" + database.SQLForUpdate } db := dbtx.GetAccessor(ctx, s.db) @@ -135,7 +137,7 @@ func (s *PathStore) findInner(ctx context.Context, id int64, lock bool) (*types. dst := new(path) err := db.GetContext(ctx, dst, sqlQuery, id) if err != nil { - return nil, processSQLErrorf(err, "Failed to find path") + return nil, database.ProcessSQLErrorf(err, "Failed to find path") } res, err := mapToPath(dst) @@ -172,7 +174,7 @@ func (s *PathStore) FindValue(ctx context.Context, value string) (*types.Path, e dst := new(path) err = db.GetContext(ctx, dst, sqlQuery, valueUnique) if err != nil { - return nil, processSQLErrorf(err, "Failed to find path") + return nil, database.ProcessSQLErrorf(err, "Failed to find path") } res, err := mapToPath(dst) @@ -186,7 +188,7 @@ func (s *PathStore) FindValue(ctx context.Context, value string) (*types.Path, e // findPrimaryInternal finds the primary path for the given target and locks the record if requested. func (s *PathStore) findPrimaryInternal(ctx context.Context, targetType enum.PathTargetType, targetID int64, lock bool) (*types.Path, error) { - stmt := builder. + stmt := database.Builder. Select(pathColumns). From("paths"). Where("path_is_primary = ?", true) @@ -197,7 +199,7 @@ func (s *PathStore) findPrimaryInternal(ctx context.Context, } if lock && !strings.HasPrefix(s.db.DriverName(), "sqlite") { - stmt.Suffix(sqlForUpdate) + stmt.Suffix(database.SQLForUpdate) } sqlQuery, args, err := stmt.ToSql() @@ -211,7 +213,7 @@ func (s *PathStore) findPrimaryInternal(ctx context.Context, dst := new(path) err = db.GetContext(ctx, dst, sqlQuery, args...) if err != nil { - return nil, processSQLErrorf(err, "Failed to find path") + return nil, database.ProcessSQLErrorf(err, "Failed to find path") } res, err := mapToPath(dst) @@ -258,21 +260,21 @@ func (s *PathStore) Update(ctx context.Context, path *types.Path) error { query, args, err := db.BindNamed(sqlQuery, dbPath) if err != nil { - return processSQLErrorf(err, "Failed to bind path object") + return database.ProcessSQLErrorf(err, "Failed to bind path object") } result, err := db.ExecContext(ctx, query, args...) if err != nil { - return processSQLErrorf(err, "failed to update path") + return database.ProcessSQLErrorf(err, "failed to update path") } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows") + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows") } if count == 0 { - return store.ErrVersionConflict + return gitness_store.ErrVersionConflict } path.Version = dbPath.Version @@ -290,7 +292,7 @@ func (s *PathStore) Delete(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, sqlQuery, id); err != nil { - return processSQLErrorf(err, "Delete query failed") + return database.ProcessSQLErrorf(err, "Delete query failed") } return nil @@ -299,7 +301,7 @@ func (s *PathStore) Delete(ctx context.Context, id int64) error { // Count returns the count of paths for a target. func (s *PathStore) Count(ctx context.Context, targetType enum.PathTargetType, targetID int64, opts *types.PathFilter) (int64, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("paths") @@ -318,7 +320,7 @@ func (s *PathStore) Count(ctx context.Context, targetType enum.PathTargetType, t var count int64 err = db.QueryRowContext(ctx, sqlQuery, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil } @@ -327,7 +329,7 @@ func (s *PathStore) Count(ctx context.Context, targetType enum.PathTargetType, t func (s *PathStore) List(ctx context.Context, targetType enum.PathTargetType, targetID int64, opts *types.PathFilter) ([]*types.Path, error) { // else we construct the sql statement. - stmt := builder. + stmt := database.Builder. Select("*"). From("paths") @@ -336,8 +338,8 @@ func (s *PathStore) List(ctx context.Context, targetType enum.PathTargetType, ta return nil, err } - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) switch opts.Sort { case enum.PathAttrID, enum.PathAttrNone: @@ -362,7 +364,7 @@ func (s *PathStore) List(ctx context.Context, targetType enum.PathTargetType, ta dst := []*path{} if err = db.SelectContext(ctx, &dst, sqlQuery, args...); err != nil { - return nil, processSQLErrorf(err, "Path select query failed") + return nil, database.ProcessSQLErrorf(err, "Path select query failed") } res, err := mapToPaths(dst) @@ -379,7 +381,7 @@ func (s *PathStore) ListPrimaryDescendantsWithLock(ctx context.Context, value st WHERE path_value_unique LIKE $1 AND path_is_primary = true` if !strings.HasPrefix(s.db.DriverName(), "sqlite") { - sqlQuery += "\n" + sqlForUpdate + sqlQuery += "\n" + database.SQLForUpdate } // map the Value to unique Value before searching! @@ -394,7 +396,7 @@ func (s *PathStore) ListPrimaryDescendantsWithLock(ctx context.Context, value st dst := []*path{} if err = db.SelectContext(ctx, &dst, sqlQuery, valueUniquePattern); err != nil { - return nil, processSQLErrorf(err, "Failed to list paths") + return nil, database.ProcessSQLErrorf(err, "Failed to list paths") } res, err := mapToPaths(dst) diff --git a/internal/store/database/principal.go b/internal/store/database/principal.go index bc20bfdff8..c37bcce6ce 100644 --- a/internal/store/database/principal.go +++ b/internal/store/database/principal.go @@ -10,7 +10,9 @@ import ( "strings" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/Masterminds/squirrel" @@ -73,7 +75,7 @@ func (s *PrincipalStore) Find(ctx context.Context, id int64) (*types.Principal, dst := new(principal) if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Select by id query failed") + return nil, database.ProcessSQLErrorf(err, "Select by id query failed") } return s.mapDBPrincipal(dst), nil @@ -89,14 +91,14 @@ func (s *PrincipalStore) FindByUID(ctx context.Context, uid string) (*types.Prin if err != nil { // in case we fail to transform, return a not found (as it can't exist in the first place) log.Ctx(ctx).Debug().Msgf("failed to transform uid '%s': %s", uid, err.Error()) - return nil, store.ErrResourceNotFound + return nil, gitness_store.ErrResourceNotFound } db := dbtx.GetAccessor(ctx, s.db) dst := new(principal) if err = db.GetContext(ctx, dst, sqlQuery, uidUnique); err != nil { - return nil, processSQLErrorf(err, "Select by uid query failed") + return nil, database.ProcessSQLErrorf(err, "Select by uid query failed") } return s.mapDBPrincipal(dst), nil @@ -116,7 +118,7 @@ func (s *PrincipalStore) FindManyByUID(ctx context.Context, uids []string) ([]*t } } - stmt := builder. + stmt := database.Builder. Select(principalColumns). From("principals"). Where(squirrel.Eq{"principal_uid_unique": uids}) @@ -124,12 +126,12 @@ func (s *PrincipalStore) FindManyByUID(ctx context.Context, uids []string) ([]*t sqlQuery, params, err := stmt.ToSql() if err != nil { - return nil, processSQLErrorf(err, "failed to generate find many principal query") + return nil, database.ProcessSQLErrorf(err, "failed to generate find many principal query") } dst := []*principal{} if err := db.SelectContext(ctx, &dst, sqlQuery, params...); err != nil { - return nil, processSQLErrorf(err, "find many by uid for principals query failed") + return nil, database.ProcessSQLErrorf(err, "find many by uid for principals query failed") } return s.mapDBPrincipals(dst), nil @@ -144,7 +146,7 @@ func (s *PrincipalStore) FindByEmail(ctx context.Context, email string) (*types. dst := new(principal) if err := db.GetContext(ctx, dst, sqlQuery, strings.ToLower(email)); err != nil { - return nil, processSQLErrorf(err, "Select by email query failed") + return nil, database.ProcessSQLErrorf(err, "Select by email query failed") } return s.mapDBPrincipal(dst), nil @@ -153,7 +155,7 @@ func (s *PrincipalStore) FindByEmail(ctx context.Context, email string) (*types. // List lists the principals matching the provided filter. func (s *PrincipalStore) List(ctx context.Context, opts *types.PrincipalFilter) ([]*types.Principal, error) { - stmt := builder. + stmt := database.Builder. Select(principalColumns). From("principals") @@ -175,8 +177,8 @@ func (s *PrincipalStore) List(ctx context.Context, ) } - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) sql, args, err := stmt.ToSql() if err != nil { @@ -187,7 +189,7 @@ func (s *PrincipalStore) List(ctx context.Context, dst := []*principal{} if err := db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Search by display_name and email query failed") + return nil, database.ProcessSQLErrorf(err, "Search by display_name and email query failed") } return s.mapDBPrincipals(dst), nil diff --git a/internal/store/database/principal_info.go b/internal/store/database/principal_info.go index 122e527349..a882e45983 100644 --- a/internal/store/database/principal_info.go +++ b/internal/store/database/principal_info.go @@ -8,7 +8,8 @@ import ( "context" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/Masterminds/squirrel" @@ -51,14 +52,14 @@ func (s *PrincipalInfoView) Find(ctx context.Context, id int64) (*types.Principa v := db.QueryRowContext(ctx, sqlQuery, id) if err := v.Err(); err != nil { - return nil, processSQLErrorf(err, "failed to find principal info") + return nil, database.ProcessSQLErrorf(err, "failed to find principal info") } info := &types.PrincipalInfo{} if err := v.Scan(&info.ID, &info.UID, &info.Email, &info.DisplayName, &info.Type, &info.Created, &info.Updated); err != nil { - return nil, processSQLErrorf(err, "failed to scan principal info") + return nil, database.ProcessSQLErrorf(err, "failed to scan principal info") } return info, nil @@ -68,19 +69,19 @@ func (s *PrincipalInfoView) Find(ctx context.Context, id int64) (*types.Principa func (s *PrincipalInfoView) FindMany(ctx context.Context, ids []int64) ([]*types.PrincipalInfo, error) { db := dbtx.GetAccessor(ctx, s.db) - stmt := builder. + stmt := database.Builder. Select(principalInfoCommonColumns). From("principals"). Where(squirrel.Eq{"principal_id": ids}) sqlQuery, params, err := stmt.ToSql() if err != nil { - return nil, processSQLErrorf(err, "failed to generate find many principal info SQL query") + return nil, database.ProcessSQLErrorf(err, "failed to generate find many principal info SQL query") } rows, err := db.QueryContext(ctx, sqlQuery, params...) if err != nil { - return nil, processSQLErrorf(err, "failed to query find many principal info") + return nil, database.ProcessSQLErrorf(err, "failed to query find many principal info") } defer func() { _ = rows.Close() @@ -93,7 +94,7 @@ func (s *PrincipalInfoView) FindMany(ctx context.Context, ids []int64) ([]*types err = rows.Scan(&info.ID, &info.UID, &info.Email, &info.DisplayName, &info.Type, &info.Created, &info.Updated) if err != nil { - return nil, processSQLErrorf(err, "failed to scan principal info") + return nil, database.ProcessSQLErrorf(err, "failed to scan principal info") } result = append(result, info) @@ -101,7 +102,7 @@ func (s *PrincipalInfoView) FindMany(ctx context.Context, ids []int64) ([]*types err = rows.Err() if err != nil { - return nil, processSQLErrorf(err, "failed to read principal info data") + return nil, database.ProcessSQLErrorf(err, "failed to read principal info data") } return result, nil diff --git a/internal/store/database/principal_service.go b/internal/store/database/principal_service.go index 0f068c003f..ff25ecd48b 100644 --- a/internal/store/database/principal_service.go +++ b/internal/store/database/principal_service.go @@ -8,8 +8,9 @@ import ( "context" "fmt" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/rs/zerolog/log" @@ -38,7 +39,7 @@ func (s *PrincipalStore) FindService(ctx context.Context, id int64) (*types.Serv dst := new(service) if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Select by id query failed") + return nil, database.ProcessSQLErrorf(err, "Select by id query failed") } return s.mapDBService(dst), nil @@ -54,14 +55,14 @@ func (s *PrincipalStore) FindServiceByUID(ctx context.Context, uid string) (*typ if err != nil { // in case we fail to transform, return a not found (as it can't exist in the first place) log.Ctx(ctx).Debug().Msgf("failed to transform uid '%s': %s", uid, err.Error()) - return nil, store.ErrResourceNotFound + return nil, gitness_store.ErrResourceNotFound } db := dbtx.GetAccessor(ctx, s.db) dst := new(service) if err = db.GetContext(ctx, dst, sqlQuery, uidUnique); err != nil { - return nil, processSQLErrorf(err, "Select by uid query failed") + return nil, database.ProcessSQLErrorf(err, "Select by uid query failed") } return s.mapDBService(dst), nil @@ -103,11 +104,11 @@ func (s *PrincipalStore) CreateService(ctx context.Context, svc *types.Service) query, arg, err := db.BindNamed(sqlQuery, dbSVC) if err != nil { - return processSQLErrorf(err, "Failed to bind service object") + return database.ProcessSQLErrorf(err, "Failed to bind service object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&svc.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -134,11 +135,11 @@ func (s *PrincipalStore) UpdateService(ctx context.Context, svc *types.Service) query, arg, err := db.BindNamed(sqlQuery, dbSVC) if err != nil { - return processSQLErrorf(err, "Failed to bind service object") + return database.ProcessSQLErrorf(err, "Failed to bind service object") } if _, err = db.ExecContext(ctx, query, arg...); err != nil { - return processSQLErrorf(err, "Update query failed") + return database.ProcessSQLErrorf(err, "Update query failed") } return err @@ -154,7 +155,7 @@ func (s *PrincipalStore) DeleteService(ctx context.Context, id int64) error { // delete the service if _, err := db.ExecContext(ctx, sqlQuery, id); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -172,7 +173,7 @@ func (s *PrincipalStore) ListServices(ctx context.Context) ([]*types.Service, er err := db.SelectContext(ctx, &dst, sqlQuery) if err != nil { - return nil, processSQLErrorf(err, "Failed executing default list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing default list query") } return s.mapDBServices(dst), nil @@ -190,7 +191,7 @@ func (s *PrincipalStore) CountServices(ctx context.Context) (int64, error) { var count int64 err := db.QueryRowContext(ctx, sqlQuery).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil diff --git a/internal/store/database/principal_service_account.go b/internal/store/database/principal_service_account.go index 42c191f1ba..3b89ce1a64 100644 --- a/internal/store/database/principal_service_account.go +++ b/internal/store/database/principal_service_account.go @@ -8,8 +8,9 @@ import ( "context" "fmt" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -40,7 +41,7 @@ func (s *PrincipalStore) FindServiceAccount(ctx context.Context, id int64) (*typ dst := new(serviceAccount) if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Select by id query failed") + return nil, database.ProcessSQLErrorf(err, "Select by id query failed") } return s.mapDBServiceAccount(dst), nil } @@ -55,14 +56,14 @@ func (s *PrincipalStore) FindServiceAccountByUID(ctx context.Context, uid string if err != nil { // in case we fail to transform, return a not found (as it can't exist in the first place) log.Ctx(ctx).Debug().Msgf("failed to transform uid '%s': %s", uid, err.Error()) - return nil, store.ErrResourceNotFound + return nil, gitness_store.ErrResourceNotFound } db := dbtx.GetAccessor(ctx, s.db) dst := new(serviceAccount) if err = db.GetContext(ctx, dst, sqlQuery, uidUnique); err != nil { - return nil, processSQLErrorf(err, "Select by uid query failed") + return nil, database.ProcessSQLErrorf(err, "Select by uid query failed") } return s.mapDBServiceAccount(dst), nil @@ -108,11 +109,11 @@ func (s *PrincipalStore) CreateServiceAccount(ctx context.Context, sa *types.Ser query, arg, err := db.BindNamed(sqlQuery, dbSA) if err != nil { - return processSQLErrorf(err, "Failed to bind service account object") + return database.ProcessSQLErrorf(err, "Failed to bind service account object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&sa.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -139,11 +140,11 @@ func (s *PrincipalStore) UpdateServiceAccount(ctx context.Context, sa *types.Ser query, arg, err := db.BindNamed(sqlQuery, dbSA) if err != nil { - return processSQLErrorf(err, "Failed to bind service account object") + return database.ProcessSQLErrorf(err, "Failed to bind service account object") } if _, err = db.ExecContext(ctx, query, arg...); err != nil { - return processSQLErrorf(err, "Update query failed") + return database.ProcessSQLErrorf(err, "Update query failed") } return err @@ -158,7 +159,7 @@ func (s *PrincipalStore) DeleteServiceAccount(ctx context.Context, id int64) err db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, sqlQuery, id); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -176,7 +177,7 @@ func (s *PrincipalStore) ListServiceAccounts(ctx context.Context, parentType enu dst := []*serviceAccount{} err := db.SelectContext(ctx, &dst, sqlQuery, parentType, parentID) if err != nil { - return nil, processSQLErrorf(err, "Failed executing default list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing default list query") } return s.mapDBServiceAccounts(dst), nil @@ -195,7 +196,7 @@ func (s *PrincipalStore) CountServiceAccounts(ctx context.Context, var count int64 err := db.QueryRowContext(ctx, sqlQuery, parentType, parentID).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil diff --git a/internal/store/database/principal_test.go b/internal/store/database/principal_test.go index a1e72e221f..c80cd15c4a 100644 --- a/internal/store/database/principal_test.go +++ b/internal/store/database/principal_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/harness/gitness/internal/store" + gitness_store "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/google/go-cmp/cmp" @@ -244,7 +245,7 @@ func testUserDelete(s store.PrincipalStore) func(t *testing.T) { t.Error(err) return } - if _, err = s.Find(ctx, 1); !errors.Is(err, store.ErrResourceNotFound) { + if _, err = s.Find(ctx, 1); !errors.Is(err, gitness_store.ErrResourceNotFound) { t.Errorf("Expected store.ErrResourceNotFound got %s", err) } } diff --git a/internal/store/database/principal_user.go b/internal/store/database/principal_user.go index 50e7a45e52..356215b767 100644 --- a/internal/store/database/principal_user.go +++ b/internal/store/database/principal_user.go @@ -9,8 +9,9 @@ import ( "fmt" "strings" - "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -41,7 +42,7 @@ func (s *PrincipalStore) FindUser(ctx context.Context, id int64) (*types.User, e dst := new(user) if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Select by id query failed") + return nil, database.ProcessSQLErrorf(err, "Select by id query failed") } return s.mapDBUser(dst), nil @@ -57,14 +58,14 @@ func (s *PrincipalStore) FindUserByUID(ctx context.Context, uid string) (*types. if err != nil { // in case we fail to transform, return a not found (as it can't exist in the first place) log.Ctx(ctx).Debug().Msgf("failed to transform uid '%s': %s", uid, err.Error()) - return nil, store.ErrResourceNotFound + return nil, gitness_store.ErrResourceNotFound } db := dbtx.GetAccessor(ctx, s.db) dst := new(user) if err = db.GetContext(ctx, dst, sqlQuery, uidUnique); err != nil { - return nil, processSQLErrorf(err, "Select by uid query failed") + return nil, database.ProcessSQLErrorf(err, "Select by uid query failed") } return s.mapDBUser(dst), nil @@ -79,7 +80,7 @@ func (s *PrincipalStore) FindUserByEmail(ctx context.Context, email string) (*ty dst := new(user) if err := db.GetContext(ctx, dst, sqlQuery, strings.ToLower(email)); err != nil { - return nil, processSQLErrorf(err, "Select by email query failed") + return nil, database.ProcessSQLErrorf(err, "Select by email query failed") } return s.mapDBUser(dst), nil @@ -123,11 +124,11 @@ func (s *PrincipalStore) CreateUser(ctx context.Context, user *types.User) error query, arg, err := db.BindNamed(sqlQuery, dbUser) if err != nil { - return processSQLErrorf(err, "Failed to bind user object") + return database.ProcessSQLErrorf(err, "Failed to bind user object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&user.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -156,11 +157,11 @@ func (s *PrincipalStore) UpdateUser(ctx context.Context, user *types.User) error query, arg, err := db.BindNamed(sqlQuery, dbUser) if err != nil { - return processSQLErrorf(err, "Failed to bind user object") + return database.ProcessSQLErrorf(err, "Failed to bind user object") } if _, err = db.ExecContext(ctx, query, arg...); err != nil { - return processSQLErrorf(err, "Update query failed") + return database.ProcessSQLErrorf(err, "Update query failed") } return err @@ -175,7 +176,7 @@ func (s *PrincipalStore) DeleteUser(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, sqlQuery, id); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -186,12 +187,12 @@ func (s *PrincipalStore) ListUsers(ctx context.Context, opts *types.UserFilter) db := dbtx.GetAccessor(ctx, s.db) dst := []*user{} - stmt := builder. + stmt := database.Builder. Select(userColumns). From("principals"). Where("principal_type = 'user'") - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) switch opts.Sort { case enum.UserAttrName, enum.UserAttrNone: @@ -217,7 +218,7 @@ func (s *PrincipalStore) ListUsers(ctx context.Context, opts *types.UserFilter) } if err = db.SelectContext(ctx, &dst, sql); err != nil { - return nil, processSQLErrorf(err, "Failed executing custom list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing custom list query") } return s.mapDBUsers(dst), nil @@ -235,7 +236,7 @@ func (s *PrincipalStore) CountUsers(ctx context.Context) (int64, error) { var count int64 err := db.QueryRowContext(ctx, sqlQuery).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil diff --git a/internal/store/database/pullreq.go b/internal/store/database/pullreq.go index b06fbaf930..8ad4bbd447 100644 --- a/internal/store/database/pullreq.go +++ b/internal/store/database/pullreq.go @@ -11,7 +11,9 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -123,7 +125,7 @@ func (s *PullReqStore) Find(ctx context.Context, id int64) (*types.PullReq, erro dst := &pullReq{} if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Failed to find pull request") + return nil, database.ProcessSQLErrorf(err, "Failed to find pull request") } return s.mapPullReq(ctx, dst), nil @@ -139,14 +141,14 @@ func (s *PullReqStore) findByNumberInternal( WHERE pullreq_target_repo_id = $1 AND pullreq_number = $2` if lock && !strings.HasPrefix(s.db.DriverName(), "sqlite") { - sqlQuery += "\n" + sqlForUpdate + sqlQuery += "\n" + database.SQLForUpdate } db := dbtx.GetAccessor(ctx, s.db) dst := &pullReq{} if err := db.GetContext(ctx, dst, sqlQuery, repoID, number); err != nil { - return nil, processSQLErrorf(err, "Failed to find pull request by number") + return nil, database.ProcessSQLErrorf(err, "Failed to find pull request by number") } return s.mapPullReq(ctx, dst), nil @@ -230,11 +232,11 @@ func (s *PullReqStore) Create(ctx context.Context, pr *types.PullReq) error { query, arg, err := db.BindNamed(sqlQuery, mapInternalPullReq(pr)) if err != nil { - return processSQLErrorf(err, "Failed to bind pullReq object") + return database.ProcessSQLErrorf(err, "Failed to bind pullReq object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&pr.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -276,21 +278,21 @@ func (s *PullReqStore) Update(ctx context.Context, pr *types.PullReq) error { query, arg, err := db.BindNamed(sqlQuery, dbPR) if err != nil { - return processSQLErrorf(err, "Failed to bind pull request object") + return database.ProcessSQLErrorf(err, "Failed to bind pull request object") } result, err := db.ExecContext(ctx, query, arg...) if err != nil { - return processSQLErrorf(err, "Failed to update pull request") + return database.ProcessSQLErrorf(err, "Failed to update pull request") } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows") + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows") } if count == 0 { - return store.ErrVersionConflict + return gitness_store.ErrVersionConflict } *pr = *s.mapPullReq(ctx, dbPR) @@ -314,7 +316,7 @@ func (s *PullReqStore) UpdateOptLock(ctx context.Context, pr *types.PullReq, if err == nil { return &dup, nil } - if !errors.Is(err, store.ErrVersionConflict) { + if !errors.Is(err, gitness_store.ErrVersionConflict) { return nil, err } @@ -340,7 +342,7 @@ func (s *PullReqStore) Delete(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, pullReqDelete, id); err != nil { - return processSQLErrorf(err, "the delete query failed") + return database.ProcessSQLErrorf(err, "the delete query failed") } return nil @@ -348,7 +350,7 @@ func (s *PullReqStore) Delete(ctx context.Context, id int64) error { // Count of pull requests for a repo. func (s *PullReqStore) Count(ctx context.Context, opts *types.PullReqFilter) (int64, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("pullreqs") @@ -388,7 +390,7 @@ func (s *PullReqStore) Count(ctx context.Context, opts *types.PullReqFilter) (in var count int64 err = db.QueryRowContext(ctx, sql, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil @@ -396,7 +398,7 @@ func (s *PullReqStore) Count(ctx context.Context, opts *types.PullReqFilter) (in // List returns a list of pull requests for a repo. func (s *PullReqStore) List(ctx context.Context, opts *types.PullReqFilter) ([]*types.PullReq, error) { - stmt := builder. + stmt := database.Builder. Select(pullReqColumns). From("pullreqs") @@ -430,8 +432,8 @@ func (s *PullReqStore) List(ctx context.Context, opts *types.PullReqFilter) ([]* stmt = stmt.Where("pullreq_created_by = ?", opts.CreatedBy) } - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) // NOTE: string concatenation is safe because the // order attribute is an enum and is not user-defined, @@ -449,7 +451,7 @@ func (s *PullReqStore) List(ctx context.Context, opts *types.PullReqFilter) ([]* db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed executing custom list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing custom list query") } result, err := s.mapSlicePullReq(ctx, dst) diff --git a/internal/store/database/pullreq_activity.go b/internal/store/database/pullreq_activity.go index 803d747e64..07a04855c8 100644 --- a/internal/store/database/pullreq_activity.go +++ b/internal/store/database/pullreq_activity.go @@ -11,7 +11,9 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -126,7 +128,7 @@ func (s *PullReqActivityStore) Find(ctx context.Context, id int64) (*types.PullR dst := &pullReqActivity{} if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Failed to find pull request activity") + return nil, database.ProcessSQLErrorf(err, "Failed to find pull request activity") } return s.mapPullReqActivity(ctx, dst), nil @@ -197,11 +199,11 @@ func (s *PullReqActivityStore) Create(ctx context.Context, act *types.PullReqAct query, arg, err := db.BindNamed(sqlQuery, mapInternalPullReqActivity(act)) if err != nil { - return processSQLErrorf(err, "Failed to bind pull request activity object") + return database.ProcessSQLErrorf(err, "Failed to bind pull request activity object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&act.ID); err != nil { - return processSQLErrorf(err, "Failed to insert pull request activity") + return database.ProcessSQLErrorf(err, "Failed to insert pull request activity") } return nil @@ -272,21 +274,21 @@ func (s *PullReqActivityStore) Update(ctx context.Context, act *types.PullReqAct query, arg, err := db.BindNamed(sqlQuery, dbAct) if err != nil { - return processSQLErrorf(err, "Failed to bind pull request activity object") + return database.ProcessSQLErrorf(err, "Failed to bind pull request activity object") } result, err := db.ExecContext(ctx, query, arg...) if err != nil { - return processSQLErrorf(err, "Failed to update pull request activity") + return database.ProcessSQLErrorf(err, "Failed to update pull request activity") } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows") + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows") } if count == 0 { - return store.ErrVersionConflict + return gitness_store.ErrVersionConflict } *act = *s.mapPullReqActivity(ctx, dbAct) @@ -311,7 +313,7 @@ func (s *PullReqActivityStore) UpdateOptLock(ctx context.Context, if err == nil { return &dup, nil } - if !errors.Is(err, store.ErrVersionConflict) { + if !errors.Is(err, gitness_store.ErrVersionConflict) { return nil, err } @@ -327,7 +329,7 @@ func (s *PullReqActivityStore) Count(ctx context.Context, prID int64, opts *types.PullReqActivityFilter, ) (int64, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("pullreq_activities"). Where("pullreq_activity_pullreq_id = ?", prID) @@ -362,7 +364,7 @@ func (s *PullReqActivityStore) Count(ctx context.Context, var count int64 err = db.QueryRowContext(ctx, sql, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil @@ -373,7 +375,7 @@ func (s *PullReqActivityStore) List(ctx context.Context, prID int64, opts *types.PullReqActivityFilter, ) ([]*types.PullReqActivity, error) { - stmt := builder. + stmt := database.Builder. Select(pullreqActivityColumns). From("pullreq_activities"). Where("pullreq_activity_pullreq_id = ?", prID) @@ -399,7 +401,7 @@ func (s *PullReqActivityStore) List(ctx context.Context, } if opts.Limit > 0 { - stmt = stmt.Limit(uint64(limit(opts.Limit))) + stmt = stmt.Limit(database.Limit(opts.Limit)) } stmt = stmt.OrderBy("pullreq_activity_order asc", "pullreq_activity_sub_order asc") @@ -414,7 +416,7 @@ func (s *PullReqActivityStore) List(ctx context.Context, db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed executing pull request activity list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing pull request activity list query") } result, err := s.mapSlicePullReqActivity(ctx, dst) @@ -426,7 +428,7 @@ func (s *PullReqActivityStore) List(ctx context.Context, } func (s *PullReqActivityStore) CountUnresolved(ctx context.Context, prID int64) (int, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("pullreq_activities"). Where("pullreq_activity_pullreq_id = ?", prID). @@ -445,7 +447,7 @@ func (s *PullReqActivityStore) CountUnresolved(ctx context.Context, prID int64) var count int err = db.QueryRowContext(ctx, sql, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count unresolved query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count unresolved query") } return count, nil diff --git a/internal/store/database/pullreq_reviewers.go b/internal/store/database/pullreq_reviewers.go index 59c903cf51..9a4562d8aa 100644 --- a/internal/store/database/pullreq_reviewers.go +++ b/internal/store/database/pullreq_reviewers.go @@ -10,7 +10,8 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -82,7 +83,7 @@ func (s *PullReqReviewerStore) Find(ctx context.Context, prID, principalID int64 dst := &pullReqReviewer{} if err := db.GetContext(ctx, dst, sqlQuery, prID, principalID); err != nil { - return nil, processSQLErrorf(err, "Failed to find pull request reviewer") + return nil, database.ProcessSQLErrorf(err, "Failed to find pull request reviewer") } return s.mapPullReqReviewer(ctx, dst), nil @@ -119,11 +120,11 @@ func (s *PullReqReviewerStore) Create(ctx context.Context, v *types.PullReqRevie query, arg, err := db.BindNamed(sqlQuery, mapInternalPullReqReviewer(v)) if err != nil { - return processSQLErrorf(err, "Failed to bind pull request reviewer object") + return database.ProcessSQLErrorf(err, "Failed to bind pull request reviewer object") } if _, err = db.ExecContext(ctx, query, arg...); err != nil { - return processSQLErrorf(err, "Failed to insert pull request reviewer") + return database.ProcessSQLErrorf(err, "Failed to insert pull request reviewer") } return nil @@ -150,12 +151,12 @@ func (s *PullReqReviewerStore) Update(ctx context.Context, v *types.PullReqRevie query, arg, err := db.BindNamed(sqlQuery, dbv) if err != nil { - return processSQLErrorf(err, "Failed to bind pull request activity object") + return database.ProcessSQLErrorf(err, "Failed to bind pull request activity object") } _, err = db.ExecContext(ctx, query, arg...) if err != nil { - return processSQLErrorf(err, "Failed to update pull request activity") + return database.ProcessSQLErrorf(err, "Failed to update pull request activity") } v.Updated = dbv.Updated @@ -165,7 +166,7 @@ func (s *PullReqReviewerStore) Update(ctx context.Context, v *types.PullReqRevie // List returns a list of pull reviewers for a pull request. func (s *PullReqReviewerStore) List(ctx context.Context, prID int64) ([]*types.PullReqReviewer, error) { - stmt := builder. + stmt := database.Builder. Select(pullreqReviewerColumns). From("pullreq_reviewers"). Where("pullreq_reviewer_pullreq_id = ?", prID). @@ -182,7 +183,7 @@ func (s *PullReqReviewerStore) List(ctx context.Context, prID int64) ([]*types.P db := dbtx.GetAccessor(ctx, s.db) if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed executing pull request reviewer list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing pull request reviewer list query") } result, err := s.mapSlicePullReqReviewer(ctx, dst) diff --git a/internal/store/database/pullreq_reviews.go b/internal/store/database/pullreq_reviews.go index e1064538bb..4587cb78f0 100644 --- a/internal/store/database/pullreq_reviews.go +++ b/internal/store/database/pullreq_reviews.go @@ -8,7 +8,8 @@ import ( "context" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -67,7 +68,7 @@ func (s *PullReqReviewStore) Find(ctx context.Context, id int64) (*types.PullReq dst := &pullReqReview{} if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Failed to find pull request activity") + return nil, database.ProcessSQLErrorf(err, "Failed to find pull request activity") } return mapPullReqReview(dst), nil @@ -96,11 +97,11 @@ func (s *PullReqReviewStore) Create(ctx context.Context, v *types.PullReqReview) query, arg, err := db.BindNamed(sqlQuery, mapInternalPullReqReview(v)) if err != nil { - return processSQLErrorf(err, "Failed to bind pull request review object") + return database.ProcessSQLErrorf(err, "Failed to bind pull request review object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&v.ID); err != nil { - return processSQLErrorf(err, "Failed to insert pull request review") + return database.ProcessSQLErrorf(err, "Failed to insert pull request review") } return nil diff --git a/internal/store/database/repo.go b/internal/store/database/repo.go index dda85d61cc..85a6898c3d 100644 --- a/internal/store/database/repo.go +++ b/internal/store/database/repo.go @@ -12,7 +12,9 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -74,7 +76,7 @@ func (s *RepoStore) Find(ctx context.Context, id int64) (*types.Repository, erro dst := new(types.Repository) if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Failed to find repo") + return nil, database.ProcessSQLErrorf(err, "Failed to find repo") } return dst, nil } @@ -92,7 +94,7 @@ func (s *RepoStore) FindByRef(ctx context.Context, repoRef string) (*types.Repos if path.TargetType != enum.PathTargetTypeRepo { // IMPORTANT: expose as not found error as we didn't find the repo! - return nil, fmt.Errorf("path is not targeting a repo - %w", store.ErrResourceNotFound) + return nil, fmt.Errorf("path is not targeting a repo - %w", gitness_store.ErrResourceNotFound) } id = path.TargetID @@ -147,11 +149,11 @@ func (s *RepoStore) Create(ctx context.Context, repo *types.Repository) error { // insert repo first so we get id query, arg, err := db.BindNamed(sqlQuery, repo) if err != nil { - return processSQLErrorf(err, "Failed to bind repo object") + return database.ProcessSQLErrorf(err, "Failed to bind repo object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&repo.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -185,21 +187,21 @@ func (s *RepoStore) Update(ctx context.Context, repo *types.Repository) error { query, arg, err := db.BindNamed(sqlQuery, repo) if err != nil { - return processSQLErrorf(err, "Failed to bind repo object") + return database.ProcessSQLErrorf(err, "Failed to bind repo object") } result, err := db.ExecContext(ctx, query, arg...) if err != nil { - return processSQLErrorf(err, "Failed to update repository") + return database.ProcessSQLErrorf(err, "Failed to update repository") } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows") + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows") } if count == 0 { - return store.ErrVersionConflict + return gitness_store.ErrVersionConflict } return nil @@ -221,7 +223,7 @@ func (s *RepoStore) UpdateOptLock(ctx context.Context, if err == nil { return &dup, nil } - if !errors.Is(err, store.ErrVersionConflict) { + if !errors.Is(err, gitness_store.ErrVersionConflict) { return nil, err } @@ -241,7 +243,7 @@ func (s *RepoStore) Delete(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, repoDelete, id); err != nil { - return processSQLErrorf(err, "the delete query failed") + return database.ProcessSQLErrorf(err, "the delete query failed") } return nil @@ -249,7 +251,7 @@ func (s *RepoStore) Delete(ctx context.Context, id int64) error { // Count of repos in a space. func (s *RepoStore) Count(ctx context.Context, parentID int64, opts *types.RepoFilter) (int64, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("repositories"). Where("repo_parent_id = ?", parentID) @@ -268,14 +270,14 @@ func (s *RepoStore) Count(ctx context.Context, parentID int64, opts *types.RepoF var count int64 err = db.QueryRowContext(ctx, sql, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil } // List returns a list of repos in a space. func (s *RepoStore) List(ctx context.Context, parentID int64, opts *types.RepoFilter) ([]*types.Repository, error) { - stmt := builder. + stmt := database.Builder. Select(repoColumnsForJoin). From("repositories"). InnerJoin("paths ON repositories.repo_id=paths.path_repo_id AND paths.path_is_primary=true"). @@ -285,8 +287,8 @@ func (s *RepoStore) List(ctx context.Context, parentID int64, opts *types.RepoFi stmt = stmt.Where("LOWER(repo_uid) LIKE ?", fmt.Sprintf("%%%s%%", strings.ToLower(opts.Query))) } - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) switch opts.Sort { case enum.RepoAttrUID, enum.RepoAttrNone: @@ -315,7 +317,7 @@ func (s *RepoStore) List(ctx context.Context, parentID int64, opts *types.RepoFi dst := []*types.Repository{} if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed executing custom list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing custom list query") } return dst, nil diff --git a/internal/store/database/repo_git_info.go b/internal/store/database/repo_git_info.go index 55b8e394d7..af7b435df4 100644 --- a/internal/store/database/repo_git_info.go +++ b/internal/store/database/repo_git_info.go @@ -8,7 +8,8 @@ import ( "context" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/jmoiron/sqlx" @@ -38,13 +39,13 @@ func (s *RepoGitInfoView) Find(ctx context.Context, id int64) (*types.Repository v := db.QueryRowContext(ctx, sqlQuery, id) if err := v.Err(); err != nil { - return nil, processSQLErrorf(err, "failed to find git uid by repository id") + return nil, database.ProcessSQLErrorf(err, "failed to find git uid by repository id") } var result = types.RepositoryGitInfo{ID: id} if err := v.Scan(&result.GitUID); err != nil { - return nil, processSQLErrorf(err, "failed to scan git uid") + return nil, database.ProcessSQLErrorf(err, "failed to scan git uid") } return &result, nil diff --git a/internal/store/database/space.go b/internal/store/database/space.go index aa2ca882b0..904ca0f2e3 100644 --- a/internal/store/database/space.go +++ b/internal/store/database/space.go @@ -12,7 +12,9 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -81,7 +83,7 @@ func (s *SpaceStore) Find(ctx context.Context, id int64) (*types.Space, error) { dst := new(space) if err := db.GetContext(ctx, dst, spaceSelectByID, id); err != nil { - return nil, processSQLErrorf(err, "Failed to find space") + return nil, database.ProcessSQLErrorf(err, "Failed to find space") } return mapToSpace(dst), nil @@ -100,7 +102,7 @@ func (s *SpaceStore) FindByRef(ctx context.Context, spaceRef string) (*types.Spa if path.TargetType != enum.PathTargetTypeSpace { // IMPORTANT: expose as not found error as we didn't find the space! - return nil, fmt.Errorf("path is not targeting a space - %w", store.ErrResourceNotFound) + return nil, fmt.Errorf("path is not targeting a space - %w", gitness_store.ErrResourceNotFound) } id = path.TargetID @@ -141,11 +143,11 @@ func (s *SpaceStore) Create(ctx context.Context, space *types.Space) error { query, args, err := db.BindNamed(sqlQuery, dbSpace) if err != nil { - return processSQLErrorf(err, "Failed to bind space object") + return database.ProcessSQLErrorf(err, "Failed to bind space object") } if err = db.QueryRowContext(ctx, query, args...).Scan(&space.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -177,21 +179,21 @@ func (s *SpaceStore) Update(ctx context.Context, space *types.Space) error { query, arg, err := db.BindNamed(sqlQuery, dbSpace) if err != nil { - return processSQLErrorf(err, "Failed to bind space object") + return database.ProcessSQLErrorf(err, "Failed to bind space object") } result, err := db.ExecContext(ctx, query, arg...) if err != nil { - return processSQLErrorf(err, "Update query failed") + return database.ProcessSQLErrorf(err, "Update query failed") } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows") + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows") } if count == 0 { - return store.ErrVersionConflict + return gitness_store.ErrVersionConflict } space.Version = dbSpace.Version @@ -216,7 +218,7 @@ func (s *SpaceStore) UpdateOptLock(ctx context.Context, if err == nil { return &dup, nil } - if !errors.Is(err, store.ErrVersionConflict) { + if !errors.Is(err, gitness_store.ErrVersionConflict) { return nil, err } @@ -236,7 +238,7 @@ func (s *SpaceStore) Delete(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, sqlQuery, id); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -244,7 +246,7 @@ func (s *SpaceStore) Delete(ctx context.Context, id int64) error { // Count the child spaces of a space. func (s *SpaceStore) Count(ctx context.Context, id int64, opts *types.SpaceFilter) (int64, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("spaces"). Where("space_parent_id = ?", id) @@ -263,20 +265,21 @@ func (s *SpaceStore) Count(ctx context.Context, id int64, opts *types.SpaceFilte var count int64 err = db.QueryRowContext(ctx, sql, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil } // List returns a list of spaces under the parent space. func (s *SpaceStore) List(ctx context.Context, id int64, opts *types.SpaceFilter) ([]*types.Space, error) { - stmt := builder. + stmt := database.Builder. Select(spaceColumnsForJoin). From("spaces"). InnerJoin(`paths ON spaces.space_id=paths.path_space_id AND paths.path_is_primary=true`). Where("space_parent_id = ?", fmt.Sprint(id)) - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) if opts.Query != "" { stmt = stmt.Where("LOWER(space_uid) LIKE ?", fmt.Sprintf("%%%s%%", strings.ToLower(opts.Query))) @@ -309,7 +312,7 @@ func (s *SpaceStore) List(ctx context.Context, id int64, opts *types.SpaceFilter dst := []*space{} if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Failed executing custom list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing custom list query") } return mapToSpaces(dst), nil diff --git a/internal/store/database/store_test.go b/internal/store/database/store_test.go index 5f38faba0d..353e005a01 100644 --- a/internal/store/database/store_test.go +++ b/internal/store/database/store_test.go @@ -9,6 +9,8 @@ import ( "encoding/json" "os" + "github.com/harness/gitness/store/database" + "github.com/jmoiron/sqlx" _ "github.com/lib/pq" @@ -25,7 +27,7 @@ func connect() (*sqlx.DB, error) { driver = os.Getenv("DATABASE_DRIVER") config = os.Getenv("DATABASE_CONFIG") } - return Connect(context.Background(), driver, config) + return database.Connect(context.Background(), driver, config) } // seed seed the database state. diff --git a/internal/store/database/token.go b/internal/store/database/token.go index b9983d3773..2e7ef9fc99 100644 --- a/internal/store/database/token.go +++ b/internal/store/database/token.go @@ -8,7 +8,8 @@ import ( "context" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -33,7 +34,7 @@ func (s *TokenStore) Find(ctx context.Context, id int64) (*types.Token, error) { dst := new(types.Token) if err := db.GetContext(ctx, dst, TokenSelectByID, id); err != nil { - return nil, processSQLErrorf(err, "Failed to find token") + return nil, database.ProcessSQLErrorf(err, "Failed to find token") } return dst, nil @@ -45,7 +46,7 @@ func (s *TokenStore) FindByUID(ctx context.Context, principalID int64, tokenUID dst := new(types.Token) if err := db.GetContext(ctx, dst, TokenSelectByPrincipalIDAndUID, principalID, tokenUID); err != nil { - return nil, processSQLErrorf(err, "Failed to find token by UID") + return nil, database.ProcessSQLErrorf(err, "Failed to find token by UID") } return dst, nil @@ -57,11 +58,11 @@ func (s *TokenStore) Create(ctx context.Context, token *types.Token) error { query, arg, err := db.BindNamed(tokenInsert, token) if err != nil { - return processSQLErrorf(err, "Failed to bind token object") + return database.ProcessSQLErrorf(err, "Failed to bind token object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&token.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -72,7 +73,7 @@ func (s *TokenStore) Delete(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, tokenDelete, id); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -83,7 +84,7 @@ func (s *TokenStore) DeleteForPrincipal(ctx context.Context, principalID int64) db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, tokenDeleteForPrincipal, principalID); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -97,7 +98,7 @@ func (s *TokenStore) Count(ctx context.Context, var count int64 err := db.QueryRowContext(ctx, tokenCountForPrincipalIDOfType, principalID, tokenType).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil @@ -114,7 +115,7 @@ func (s *TokenStore) List(ctx context.Context, err := db.SelectContext(ctx, &dst, tokenSelectForPrincipalIDOfType, principalID, tokenType) if err != nil { - return nil, processSQLErrorf(err, "Failed executing token list query") + return nil, database.ProcessSQLErrorf(err, "Failed executing token list query") } return dst, nil } diff --git a/internal/store/database/webhook.go b/internal/store/database/webhook.go index 9045d91998..05fd68929d 100644 --- a/internal/store/database/webhook.go +++ b/internal/store/database/webhook.go @@ -11,7 +11,9 @@ import ( "time" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + gitness_store "github.com/harness/gitness/store" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -86,7 +88,7 @@ func (s *WebhookStore) Find(ctx context.Context, id int64) (*types.Webhook, erro dst := &webhook{} if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Select query failed") + return nil, database.ProcessSQLErrorf(err, "Select query failed") } res, err := mapToWebhook(dst) @@ -139,11 +141,11 @@ func (s *WebhookStore) Create(ctx context.Context, hook *types.Webhook) error { query, arg, err := db.BindNamed(sqlQuery, dbHook) if err != nil { - return processSQLErrorf(err, "Failed to bind webhook object") + return database.ProcessSQLErrorf(err, "Failed to bind webhook object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&hook.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -179,21 +181,21 @@ func (s *WebhookStore) Update(ctx context.Context, hook *types.Webhook) error { query, arg, err := db.BindNamed(sqlQuery, dbHook) if err != nil { - return processSQLErrorf(err, "Failed to bind webhook object") + return database.ProcessSQLErrorf(err, "Failed to bind webhook object") } result, err := db.ExecContext(ctx, query, arg...) if err != nil { - return processSQLErrorf(err, "failed to update webhook") + return database.ProcessSQLErrorf(err, "failed to update webhook") } count, err := result.RowsAffected() if err != nil { - return processSQLErrorf(err, "Failed to get number of updated rows") + return database.ProcessSQLErrorf(err, "Failed to get number of updated rows") } if count == 0 { - return store.ErrVersionConflict + return gitness_store.ErrVersionConflict } hook.Version = dbHook.Version @@ -217,7 +219,7 @@ func (s *WebhookStore) UpdateOptLock(ctx context.Context, hook *types.Webhook, if err == nil { return &dup, nil } - if !errors.Is(err, store.ErrVersionConflict) { + if !errors.Is(err, gitness_store.ErrVersionConflict) { return nil, fmt.Errorf("failed to update the webhook: %w", err) } @@ -235,7 +237,7 @@ func (s *WebhookStore) Delete(ctx context.Context, id int64) error { WHERE webhook_id = $1` if _, err := s.db.ExecContext(ctx, sqlQuery, id); err != nil { - return processSQLErrorf(err, "The delete query failed") + return database.ProcessSQLErrorf(err, "The delete query failed") } return nil @@ -244,7 +246,7 @@ func (s *WebhookStore) Delete(ctx context.Context, id int64) error { // Count counts the webhooks for a given parent type and id. func (s *WebhookStore) Count(ctx context.Context, parentType enum.WebhookParent, parentID int64, opts *types.WebhookFilter) (int64, error) { - stmt := builder. + stmt := database.Builder. Select("count(*)"). From("webhooks") @@ -267,7 +269,7 @@ func (s *WebhookStore) Count(ctx context.Context, parentType enum.WebhookParent, var count int64 err = db.QueryRowContext(ctx, sql, args...).Scan(&count) if err != nil { - return 0, processSQLErrorf(err, "Failed executing count query") + return 0, database.ProcessSQLErrorf(err, "Failed executing count query") } return count, nil @@ -276,7 +278,7 @@ func (s *WebhookStore) Count(ctx context.Context, parentType enum.WebhookParent, // List lists the webhooks for a given parent type and id. func (s *WebhookStore) List(ctx context.Context, parentType enum.WebhookParent, parentID int64, opts *types.WebhookFilter) ([]*types.Webhook, error) { - stmt := builder. + stmt := database.Builder. Select(webhookColumns). From("webhooks") @@ -293,8 +295,8 @@ func (s *WebhookStore) List(ctx context.Context, parentType enum.WebhookParent, stmt = stmt.Where("LOWER(webhook_display_name) LIKE ?", fmt.Sprintf("%%%s%%", strings.ToLower(opts.Query))) } - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) switch opts.Sort { case enum.WebhookAttrID, enum.WebhookAttrNone: @@ -320,7 +322,7 @@ func (s *WebhookStore) List(ctx context.Context, parentType enum.WebhookParent, dst := []*webhook{} if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Select query failed") + return nil, database.ProcessSQLErrorf(err, "Select query failed") } res, err := mapToWebhooks(dst) diff --git a/internal/store/database/webhook_execution.go b/internal/store/database/webhook_execution.go index 5b1aaa355f..115d1437ea 100644 --- a/internal/store/database/webhook_execution.go +++ b/internal/store/database/webhook_execution.go @@ -9,7 +9,8 @@ import ( "fmt" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/internal/store/database/dbtx" + "github.com/harness/gitness/store/database" + "github.com/harness/gitness/store/database/dbtx" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -87,7 +88,7 @@ func (s *WebhookExecutionStore) Find(ctx context.Context, id int64) (*types.Webh dst := &webhookExecution{} if err := db.GetContext(ctx, dst, sqlQuery, id); err != nil { - return nil, processSQLErrorf(err, "Select query failed") + return nil, database.ProcessSQLErrorf(err, "Select query failed") } return mapToWebhookExecution(dst), nil @@ -136,11 +137,11 @@ func (s *WebhookExecutionStore) Create(ctx context.Context, execution *types.Web query, arg, err := db.BindNamed(sqlQuery, mapToInternalWebhookExecution(execution)) if err != nil { - return processSQLErrorf(err, "Failed to bind webhook execution object") + return database.ProcessSQLErrorf(err, "Failed to bind webhook execution object") } if err = db.QueryRowContext(ctx, query, arg...).Scan(&execution.ID); err != nil { - return processSQLErrorf(err, "Insert query failed") + return database.ProcessSQLErrorf(err, "Insert query failed") } return nil @@ -149,13 +150,13 @@ func (s *WebhookExecutionStore) Create(ctx context.Context, execution *types.Web // ListForWebhook lists the webhook executions for a given webhook id. func (s *WebhookExecutionStore) ListForWebhook(ctx context.Context, webhookID int64, opts *types.WebhookExecutionFilter) ([]*types.WebhookExecution, error) { - stmt := builder. + stmt := database.Builder. Select(webhookExecutionColumns). From("webhook_executions"). Where("webhook_execution_webhook_id = ?", webhookID) - stmt = stmt.Limit(uint64(limit(opts.Size))) - stmt = stmt.Offset(uint64(offset(opts.Page, opts.Size))) + stmt = stmt.Limit(database.Limit(opts.Size)) + stmt = stmt.Offset(database.Offset(opts.Page, opts.Size)) // fixed ordering by desc id (new ones first) - add customized ordering if deemed necessary. stmt = stmt.OrderBy("webhook_execution_id DESC") @@ -169,7 +170,7 @@ func (s *WebhookExecutionStore) ListForWebhook(ctx context.Context, webhookID in dst := []*webhookExecution{} if err = db.SelectContext(ctx, &dst, sql, args...); err != nil { - return nil, processSQLErrorf(err, "Select query failed") + return nil, database.ProcessSQLErrorf(err, "Select query failed") } return mapToWebhookExecutions(dst), nil @@ -185,7 +186,7 @@ func (s *WebhookExecutionStore) ListForTrigger(ctx context.Context, dst := []*webhookExecution{} if err := db.SelectContext(ctx, &dst, sqlQuery, triggerID); err != nil { - return nil, processSQLErrorf(err, "Select query failed") + return nil, database.ProcessSQLErrorf(err, "Select query failed") } return mapToWebhookExecutions(dst), nil diff --git a/internal/store/database/wire.go b/internal/store/database/wire.go index 72ba15c0f8..4870f3d98c 100644 --- a/internal/store/database/wire.go +++ b/internal/store/database/wire.go @@ -8,7 +8,8 @@ import ( "context" "github.com/harness/gitness/internal/store" - "github.com/harness/gitness/types" + "github.com/harness/gitness/internal/store/database/migrate" + "github.com/harness/gitness/store/database" "github.com/google/wire" "github.com/jmoiron/sqlx" @@ -35,12 +36,19 @@ var WireSet = wire.NewSet( ProvideReqCheckStore, ) +// helper function to setup the database by performing automated +// database migration steps. +func migrator(ctx context.Context, db *sqlx.DB) error { + return migrate.Migrate(ctx, db) +} + // ProvideDatabase provides a database connection. -func ProvideDatabase(ctx context.Context, config *types.Config) (*sqlx.DB, error) { - return ConnectAndMigrate( +func ProvideDatabase(ctx context.Context, config database.Config) (*sqlx.DB, error) { + return database.ConnectAndMigrate( ctx, - config.Database.Driver, - config.Database.Datasource, + config.Driver, + config.Datasource, + migrator, ) } diff --git a/store/database/config.go b/store/database/config.go new file mode 100644 index 0000000000..399e8494d2 --- /dev/null +++ b/store/database/config.go @@ -0,0 +1,11 @@ +// Copyright 2022 Harness Inc. All rights reserved. +// Use of this source code is governed by the Polyform Free Trial License +// that can be found in the LICENSE.md file for this repository. + +package database + +// Config specifies the config for the database package. +type Config struct { + Driver string + Datasource string +} diff --git a/internal/store/database/dbtx/ctx.go b/store/database/dbtx/ctx.go similarity index 100% rename from internal/store/database/dbtx/ctx.go rename to store/database/dbtx/ctx.go diff --git a/internal/store/database/dbtx/db.go b/store/database/dbtx/db.go similarity index 100% rename from internal/store/database/dbtx/db.go rename to store/database/dbtx/db.go diff --git a/internal/store/database/dbtx/interface.go b/store/database/dbtx/interface.go similarity index 100% rename from internal/store/database/dbtx/interface.go rename to store/database/dbtx/interface.go diff --git a/internal/store/database/dbtx/locker.go b/store/database/dbtx/locker.go similarity index 100% rename from internal/store/database/dbtx/locker.go rename to store/database/dbtx/locker.go diff --git a/internal/store/database/dbtx/runner.go b/store/database/dbtx/runner.go similarity index 100% rename from internal/store/database/dbtx/runner.go rename to store/database/dbtx/runner.go diff --git a/internal/store/database/dbtx/runner_test.go b/store/database/dbtx/runner_test.go similarity index 100% rename from internal/store/database/dbtx/runner_test.go rename to store/database/dbtx/runner_test.go diff --git a/internal/store/database/dbtx/tx.go b/store/database/dbtx/tx.go similarity index 100% rename from internal/store/database/dbtx/tx.go rename to store/database/dbtx/tx.go diff --git a/internal/store/database/store.go b/store/database/store.go similarity index 86% rename from internal/store/database/store.go rename to store/database/store.go index fd33d18589..d71e756801 100644 --- a/internal/store/database/store.go +++ b/store/database/store.go @@ -14,8 +14,6 @@ import ( "net/url" "time" - "github.com/harness/gitness/internal/store/database/migrate" - "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" "github.com/rs/zerolog/log" @@ -23,12 +21,14 @@ import ( const ( // sqlForUpdate is the sql statement used for locking rows returned by select queries. - sqlForUpdate = "FOR UPDATE" + SQLForUpdate = "FOR UPDATE" ) -// build is a global instance of the sql builder. we are able to +type Migrator func(ctx context.Context, dbx *sqlx.DB) error + +// Builder is a global instance of the sql builder. we are able to // hardcode to postgres since sqlite3 is compatible with postgres. -var builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) +var Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) // Connect to a database and verify with a ping. func Connect(ctx context.Context, driver string, datasource string) (*sqlx.DB, error) { @@ -51,13 +51,13 @@ func Connect(ctx context.Context, driver string, datasource string) (*sqlx.DB, e } // ConnectAndMigrate creates the database handle and migrates the database. -func ConnectAndMigrate(ctx context.Context, driver string, datasource string) (*sqlx.DB, error) { +func ConnectAndMigrate(ctx context.Context, driver string, datasource string, migrator Migrator) (*sqlx.DB, error) { dbx, err := Connect(ctx, driver, datasource) if err != nil { return nil, err } - if err = migrateDatabase(ctx, dbx); err != nil { + if err = migrator(ctx, dbx); err != nil { return nil, fmt.Errorf("failed to setup the db: %w", err) } @@ -124,9 +124,3 @@ func pingDatabase(ctx context.Context, db *sqlx.DB) error { return fmt.Errorf("all 30 tries failed, last failure: %w", err) } - -// helper function to setup the database by performing automated -// database migration steps. -func migrateDatabase(ctx context.Context, db *sqlx.DB) error { - return migrate.Migrate(ctx, db) -} diff --git a/internal/store/database/util.go b/store/database/util.go similarity index 81% rename from internal/store/database/util.go rename to store/database/util.go index 9439e15172..0f5cb1c8d9 100644 --- a/internal/store/database/util.go +++ b/store/database/util.go @@ -8,7 +8,7 @@ import ( "database/sql" "fmt" - "github.com/harness/gitness/internal/store" + "github.com/harness/gitness/store" "github.com/pkg/errors" "github.com/rs/zerolog/log" @@ -18,15 +18,15 @@ import ( const defaultLimit = 100 // limit returns the page size to a sql limit. -func limit(size int) int { +func Limit(size int) uint64 { if size == 0 { size = defaultLimit } - return size + return uint64(size) } // offset converts the page to a sql offset. -func offset(page, size int) int { +func Offset(page, size int) uint64 { if page == 0 { page = 1 } @@ -34,12 +34,14 @@ func offset(page, size int) int { size = defaultLimit } page-- - return page * size + return uint64(page * size) } // Logs the error and message, returns either the provided message or a gitrpc equivalent if possible. // Always logs the full message with error as warning. -func processSQLErrorf(err error, format string, args ...interface{}) error { +// +//nolint:unparam // revisit error processing +func ProcessSQLErrorf(err error, format string, args ...interface{}) error { // create fallback error returned if we can't map it fallbackErr := fmt.Errorf(format, args...) diff --git a/internal/store/database/util_pq.go b/store/database/util_pq.go similarity index 100% rename from internal/store/database/util_pq.go rename to store/database/util_pq.go diff --git a/internal/store/database/util_sqlite.go b/store/database/util_sqlite.go similarity index 100% rename from internal/store/database/util_sqlite.go rename to store/database/util_sqlite.go diff --git a/internal/store/database/util_test.go b/store/database/util_test.go similarity index 87% rename from internal/store/database/util_test.go rename to store/database/util_test.go index 3e635902dc..1cd691c5f8 100644 --- a/internal/store/database/util_test.go +++ b/store/database/util_test.go @@ -4,13 +4,15 @@ package database -import "testing" +import ( + "testing" +) func TestOffset(t *testing.T) { tests := []struct { page int size int - want int + want uint64 }{ { page: 0, @@ -45,7 +47,7 @@ func TestOffset(t *testing.T) { } for _, test := range tests { - got, want := offset(test.page, test.size), test.want + got, want := Offset(test.page, test.size), test.want if got != want { t.Errorf("Got %d want %d for page %d, size %d", got, want, test.page, test.size) } @@ -55,7 +57,7 @@ func TestOffset(t *testing.T) { func TestLimit(t *testing.T) { tests := []struct { size int - want int + want uint64 }{ { size: 0, @@ -68,7 +70,7 @@ func TestLimit(t *testing.T) { } for _, test := range tests { - got, want := limit(test.size), test.want + got, want := Limit(test.size), test.want if got != want { t.Errorf("Got %d want %d for size %d", got, want, test.size) } diff --git a/internal/store/errors.go b/store/errors.go similarity index 100% rename from internal/store/errors.go rename to store/errors.go