From ab1e21a489a510baa556c038b2d4515f5e86e8e3 Mon Sep 17 00:00:00 2001 From: Trey Ivy Date: Tue, 10 Dec 2024 17:34:44 +0000 Subject: [PATCH] Add support for postgres --- MODULE.bazel | 1 + cmd/bb_portal/BUILD.bazel | 3 +++ cmd/bb_portal/main.go | 24 ++++++++++++++++++++---- go.mod | 4 ++++ go.sum | 8 ++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 261bc55..419277a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -65,6 +65,7 @@ use_repo( "com_github_gorilla_mux", "com_github_hashicorp_go_multierror", "com_github_hedwigz_entviz", + "com_github_jackc_pgx_v5", "com_github_machinebox_graphql", "com_github_mattn_go_sqlite3", "com_github_pkg_errors", diff --git a/cmd/bb_portal/BUILD.bazel b/cmd/bb_portal/BUILD.bazel index 8346efd..1a3c7a4 100644 --- a/cmd/bb_portal/BUILD.bazel +++ b/cmd/bb_portal/BUILD.bazel @@ -24,8 +24,11 @@ go_library( "@com_github_buildbarn_bb_storage//pkg/util", "@com_github_fsnotify_fsnotify//:fsnotify", "@com_github_gorilla_mux//:mux", + "@com_github_jackc_pgx_v5//stdlib", "@com_github_mattn_go_sqlite3//:go-sqlite3", "@io_entgo_contrib//entgql", + "@io_entgo_ent//dialect", + "@io_entgo_ent//dialect/sql", "@org_golang_google_genproto//googleapis/devtools/build/v1:build", "@org_golang_google_grpc//:grpc", ], diff --git a/cmd/bb_portal/main.go b/cmd/bb_portal/main.go index 0d984a4..c2fc598 100644 --- a/cmd/bb_portal/main.go +++ b/cmd/bb_portal/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "database/sql" "flag" "log/slog" "net/http" @@ -11,10 +12,13 @@ import ( "time" "entgo.io/contrib/entgql" + "entgo.io/ent/dialect" + entsql "entgo.io/ent/dialect/sql" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/playground" "github.com/fsnotify/fsnotify" "github.com/gorilla/mux" + _ "github.com/jackc/pgx/v5/stdlib" _ "github.com/mattn/go-sqlite3" build "google.golang.org/genproto/googleapis/devtools/build/v1" go_grpc "google.golang.org/grpc" @@ -65,10 +69,22 @@ func main() { return util.StatusWrap(err, "Failed to apply global configuration options") } - dbClient, err := ent.Open( - *dsDriver, - *dsURL, - ) + var dbClient *ent.Client + + if *dsDriver == "pgx" { + db, err := sql.Open("pgx", *dsURL) + if err != nil { + fatal("Failed to open pgx database", "err", err) + } + drv := entsql.OpenDB(dialect.Postgres, db) + dbClient = ent.NewClient(ent.Driver(drv)) + } else { + dbClient, err = ent.Open( + *dsDriver, + *dsURL, + ) + } + if err != nil { return util.StatusWrapf(err, "Failed to open ent client") } diff --git a/go.mod b/go.mod index 118f46f..1618aef 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/gorilla/mux v1.8.1 github.com/hashicorp/go-multierror v1.1.1 github.com/hedwigz/entviz v0.0.0-20221011080911-9d47f6f1d818 + github.com/jackc/pgx/v5 v5.7.1 github.com/machinebox/graphql v0.2.2 github.com/mattn/go-sqlite3 v1.14.22 github.com/pkg/errors v0.9.1 @@ -56,6 +57,9 @@ require ( github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/hashicorp/hcl/v2 v2.13.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/matryer/is v1.4.1 // indirect diff --git a/go.sum b/go.sum index f0fdf5b..b9ac640 100644 --- a/go.sum +++ b/go.sum @@ -321,6 +321,14 @@ github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47 github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= +github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=