Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial DB Domain Types #2

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go_library(
"@com_github_go_chi_chi_v5//middleware",
"@com_github_go_chi_httprate//:httprate",
"@com_github_go_chi_jwtauth_v5//:jwtauth",
"@com_github_jackc_pgx_v4//pgxpool",
"@com_github_jackc_pgx_v5//pgxpool",
"@com_github_namsral_flag//:flag",
"@com_github_rs_cors//:cors",
"@com_github_silicon_ally_zaphttplog//:zaphttplog",
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/httprate"
"github.com/go-chi/jwtauth/v5"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/namsral/flag"
"github.com/rs/cors"
"go.uber.org/zap"
Expand Down Expand Up @@ -93,7 +93,7 @@ func run(args []string) error {
}

logger.Info("Connecting to database", zap.String("db_host", postgresCfg.ConnConfig.Host))
pgConn, err := pgxpool.ConnectConfig(ctx, postgresCfg)
pgConn, err := pgxpool.NewWithConfig(ctx, postgresCfg)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/pactasrv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ go_library(
importpath = "github.com/RMI/pacta/cmd/server/pactasrv",
visibility = ["//visibility:public"],
deps = [
"//:pacta",
"//db",
"//openapi:pacta_generated",
"//pacta",
],
)

Expand Down
46 changes: 44 additions & 2 deletions cmd/server/pactasrv/pactasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"fmt"
"net/http"

"github.com/RMI/pacta"
"github.com/RMI/pacta/db"

pactaapi "github.com/RMI/pacta/openapi/pacta"
"github.com/RMI/pacta/pacta"
)

type DB interface {
Expand All @@ -20,7 +20,49 @@ type DB interface {
Transactional(context.Context, func(tx db.Tx) error) error
RunOrContinueTransaction(db.Tx, func(tx db.Tx) error) error

User(db.Tx, pacta.UserID) (*pacta.User, error)
Blob(tx db.Tx, id pacta.BlobID) (*pacta.Blob, error)
Blobs(tx db.Tx, ids []pacta.BlobID) (map[pacta.BlobID]*pacta.Blob, error)
CreateBlob(tx db.Tx, b *pacta.Blob) (pacta.BlobID, error)
UpdateBlob(tx db.Tx, id pacta.BlobID, mutations ...db.UpdateBlobFn) error
DeleteBlob(tx db.Tx, id pacta.BlobID) (pacta.BlobURI, error)

InitiativeInvitation(tx db.Tx, id pacta.InitiativeInvitationID) (*pacta.InitiativeInvitation, error)
InitiativeInvitationsByInitiative(tx db.Tx, iid pacta.InitiativeID) ([]*pacta.InitiativeInvitation, error)
CreateInitiativeInvitation(tx db.Tx, ii *pacta.InitiativeInvitation) (pacta.InitiativeInvitationID, error)
UpdateInitiativeInvitation(tx db.Tx, id pacta.InitiativeInvitationID, mutations ...db.UpdateInitiativeInvitationFn) error
DeleteInitiativeInvitation(tx db.Tx, id pacta.InitiativeInvitationID) error

InitiativeUserRelationship(tx db.Tx, iid pacta.InitiativeID, uid pacta.UserID) (*pacta.InitiativeUserRelationship, error)
InitiativeUserRelationshipsByUser(tx db.Tx, uid pacta.UserID) ([]*pacta.InitiativeUserRelationship, error)
InitiativeUserRelationshipsByInitiatives(tx db.Tx, iid pacta.InitiativeID) ([]*pacta.InitiativeUserRelationship, error)
PutInitiativeUserRelationship(tx db.Tx, iur *pacta.InitiativeUserRelationship) error

Initiative(tx db.Tx, id pacta.InitiativeID) (*pacta.Initiative, error)
Initiatives(tx db.Tx, ids []pacta.InitiativeID) (map[pacta.InitiativeID]*pacta.Initiative, error)
AllInitiatives(tx db.Tx) ([]*pacta.Initiative, error)
CreateInitiative(tx db.Tx, i *pacta.Initiative) error
UpdateInitiative(tx db.Tx, id pacta.InitiativeID, mutations ...db.UpdateInitiativeFn) error
DeleteInitiative(tx db.Tx, id pacta.InitiativeID) error

PACTAVersion(tx db.Tx, id pacta.PACTAVersionID) (*pacta.PACTAVersion, error)
DefaultPACTAVersion(tx db.Tx) (*pacta.PACTAVersion, error)
PACTAVersions(tx db.Tx) ([]*pacta.PACTAVersion, error)
CreatePACTAVersion(tx db.Tx, pv *pacta.PACTAVersion) (pacta.PACTAVersionID, error)
SetDefaultPACTAVersion(tx db.Tx, id pacta.PACTAVersionID) error
UpdatePACTAVersion(tx db.Tx, id pacta.PACTAVersionID, mutations ...db.UpdatePACTAVersionFn) error
DeletePACTAVersion(tx db.Tx, id pacta.PACTAVersionID) error

PortfolioInitiativeMembershipsByPortfolio(tx db.Tx, pid pacta.PortfolioID) ([]*pacta.PortfolioInitiativeMembership, error)
PortfolioInitiativeMembershipsByInitiative(tx db.Tx, iid pacta.InitiativeID) ([]*pacta.PortfolioInitiativeMembership, error)
CreatePortfolioInitiativeMembership(tx db.Tx, pim *pacta.PortfolioInitiativeMembership) error
DeletePortfolioInitiativeMembership(tx db.Tx, pid pacta.PortfolioID, iid pacta.InitiativeID) error

User(tx db.Tx, id pacta.UserID) (*pacta.User, error)
UserByAuthn(tx db.Tx, authnMechanism pacta.AuthnMechanism, authnID string) (*pacta.User, error)
Users(tx db.Tx, ids []pacta.UserID) (map[pacta.UserID]*pacta.User, error)
CreateUser(tx db.Tx, u *pacta.User) (pacta.UserID, error)
UpdateUser(tx db.Tx, id pacta.UserID, mutations ...db.UpdateUserFn) error
DeleteUser(tx db.Tx, id pacta.UserID) error
}

type Server struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/migratesqldb/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//secrets",
"@com_github_jackc_pgx_v4//pgxpool",
"@com_github_jackc_pgx_v5//pgxpool",
"@com_github_silicon_ally_testpgx//migrate",
"@com_github_spf13_cobra//:cobra",
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/migratesqldb/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/RMI/pacta/secrets"
"github.com/Silicon-Ally/testpgx/migrate"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion db/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go_library(
srcs = ["db.go"],
importpath = "github.com/RMI/pacta/db",
visibility = ["//visibility:public"],
deps = ["//:pacta"],
deps = ["//pacta"],
)
144 changes: 141 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package db
import (
"errors"
"fmt"
"time"

"github.com/RMI/pacta"
"github.com/RMI/pacta/pacta"
)

type errNotFound struct {
Expand Down Expand Up @@ -41,16 +42,153 @@ type Tx interface {

type UpdateUserFn func(*pacta.User) error

func SetUserAdmin(value bool) UpdateUserFn {
return func(u *pacta.User) error {
u.Admin = value
return nil
}
}

func SetUserSuperAdmin(value bool) UpdateUserFn {
return func(u *pacta.User) error {
u.SuperAdmin = value
return nil
}
}

func SetUserName(value string) UpdateUserFn {
return func(u *pacta.User) error {
u.Name = value
return nil
}
}

func SetUserEmail(value string) UpdateUserFn {
func SetUserPreferredLanguage(value pacta.Language) UpdateUserFn {
return func(u *pacta.User) error {
u.Email = value
u.PreferredLanguage = value
return nil
}
}

type UpdatePACTAVersionFn func(*pacta.PACTAVersion) error

func SetPACTAVersionName(value string) UpdatePACTAVersionFn {
return func(v *pacta.PACTAVersion) error {
v.Name = value
return nil
}
}

func SetPACTAVersionDescription(value string) UpdatePACTAVersionFn {
return func(v *pacta.PACTAVersion) error {
v.Description = value
return nil
}
}

func SetPACTAVersionDigest(value string) UpdatePACTAVersionFn {
return func(v *pacta.PACTAVersion) error {
v.Digest = value
return nil
}
}

type UpdateInitiativeFn func(*pacta.Initiative) error

func SetInitiativeName(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.Name = value
return nil
}
}

func SetInitiativeAffiliation(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.Affiliation = value
return nil
}
}

func SetInitiativePublicDescription(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.PublicDescription = value
return nil
}
}

func SetInitiativeInternalDescription(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.InternalDescription = value
return nil
}
}

func SetInitiativeRequiresInvitationToJoin(value bool) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.RequiresInvitationToJoin = value
return nil
}
}

func SetInitiativeIsAcceptingNewMembers(value bool) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.IsAcceptingNewMembers = value
return nil
}
}

func SetInitiativeIsAcceptingNewPortfolios(value bool) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.IsAcceptingNewPortfolios = value
return nil
}
}

func SetInitiativePACTAVersion(pvid pacta.PACTAVersionID) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.PACTAVersion = &pacta.PACTAVersion{ID: pvid}
return nil
}
}

func SetInitiativeLanguage(l pacta.Language) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.Language = l
return nil
}
}

type UpdateInitiativeInvitationFn func(*pacta.InitiativeInvitation) error

func SetInitiativeInvitationUsedAt(t time.Time) UpdateInitiativeInvitationFn {
return func(ii *pacta.InitiativeInvitation) error {
ii.UsedAt = t
return nil
}
}

func ClearInitiativeInvitationUsedBy() UpdateInitiativeInvitationFn {
return func(ii *pacta.InitiativeInvitation) error {
ii.UsedBy = nil
return nil
}
}

func SetInitiativeInvitationUsedBy(u pacta.UserID) UpdateInitiativeInvitationFn {
gbdubs marked this conversation as resolved.
Show resolved Hide resolved
return func(ii *pacta.InitiativeInvitation) error {
if u == "" {
return fmt.Errorf("cannot set used by to empty user ID")
}
ii.UsedBy = &pacta.User{ID: u}
return nil
}
}

type UpdateBlobFn func(*pacta.Blob) error

func SetBlobFileName(v string) UpdateBlobFn {
return func(b *pacta.Blob) error {
b.FileName = v
return nil
}
}
20 changes: 16 additions & 4 deletions db/sqldb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "sqldb",
srcs = [
"blob.go",
"initiative.go",
"initiative_invitation.go",
"initiative_user.go",
"pacta_version.go",
"portfolio_initiative.go",
"sqldb.go",
"user.go",
],
importpath = "github.com/RMI/pacta/db/sqldb",
visibility = ["//visibility:public"],
deps = [
"//:pacta",
"//db",
"//pacta",
"@com_github_hashicorp_go_multierror//:go-multierror",
"@com_github_jackc_pgconn//:pgconn",
"@com_github_jackc_pgx_v4//:pgx",
"@com_github_jackc_pgx_v5//:pgx",
"@com_github_jackc_pgx_v5//pgconn",
"@com_github_jackc_pgx_v5//pgtype",
"@com_github_silicon_ally_cryptorand//:cryptorand",
"@com_github_silicon_ally_idgen//:idgen",
],
Expand All @@ -23,6 +30,11 @@ go_test(
name = "sqldb_test",
size = "large",
srcs = [
"blob_test.go",
"initiative_invitation_test.go",
"initiative_test.go",
"initiative_user_test.go",
"pacta_version_test.go",
"sqldb_test.go",
"user_test.go",
],
Expand All @@ -32,8 +44,8 @@ go_test(
],
embed = [":sqldb"],
deps = [
"//:pacta",
"//db",
"//pacta",
"@com_github_google_go_cmp//cmp",
"@com_github_google_go_cmp//cmp/cmpopts",
"@com_github_silicon_ally_idgen//:idgen",
Expand Down
Loading