Skip to content

Commit

Permalink
Addresses Review Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdubs committed Sep 3, 2023
1 parent 3573cb4 commit b2543c7
Show file tree
Hide file tree
Showing 28 changed files with 1,455 additions and 1,773 deletions.
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/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
12 changes: 9 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,19 @@ func SetInitiativeInvitationUsedAt(t time.Time) UpdateInitiativeInvitationFn {
}
}

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

func SetInitiativeInvitationUsedBy(u pacta.UserID) UpdateInitiativeInvitationFn {
return func(ii *pacta.InitiativeInvitation) error {
if u == "" {
ii.UsedBy = nil
} else {
ii.UsedBy = &pacta.User{ID: u}
return fmt.Errorf("cannot set used by to empty user ID")
}
ii.UsedBy = &pacta.User{ID: u}
return nil
}
}
Expand Down
6 changes: 3 additions & 3 deletions db/sqldb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ go_library(
"//db",
"//pacta",
"@com_github_hashicorp_go_multierror//:go-multierror",
"@com_github_jackc_pgconn//:pgconn",
"@com_github_jackc_pgtype//:pgtype",
"@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 Down
29 changes: 13 additions & 16 deletions db/sqldb/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package sqldb

import (
"fmt"
"time"

"github.com/RMI/pacta/db"
"github.com/RMI/pacta/pacta"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
)

const blobIDNamespace = "blob"
const blobSelectColumns = `id, blob_uri, file_type, file_name, created_at`
const blobSelectColumns = `
blob.id,
blob.blob_uri,
blob.file_type,
blob.file_name,
blob.created_at`

func (d *DB) Blob(tx db.Tx, id pacta.BlobID) (*pacta.Blob, error) {
rows, err := d.query(tx, `
Expand Down Expand Up @@ -51,13 +55,12 @@ func (d *DB) CreateBlob(tx db.Tx, b *pacta.Blob) (pacta.BlobID, error) {
return "", fmt.Errorf("validating blob for creation: %w", err)
}
id := pacta.BlobID(d.randomID(blobIDNamespace))
createdAt := time.Now()
err := d.exec(tx, `
INSERT INTO blob
(id, blob_uri, file_type, file_name, created_at)
(id, blob_uri, file_type, file_name)
VALUES
($1, $2, $3, $4, $5);
`, id, b.BlobURI, b.FileType, b.FileName, createdAt)
($1, $2, $3, $4);
`, id, b.BlobURI, b.FileType, b.FileName)
if err != nil {
return "", fmt.Errorf("creating blob row: %w", err)
}
Expand Down Expand Up @@ -90,16 +93,10 @@ func (d *DB) UpdateBlob(tx db.Tx, id pacta.BlobID, mutations ...db.UpdateBlobFn)

func (d *DB) DeleteBlob(tx db.Tx, id pacta.BlobID) (pacta.BlobURI, error) {
var buri pacta.BlobURI
err := d.RunOrContinueTransaction(tx, func(tx db.Tx) error {
row := d.queryRow(tx, `DELETE FROM blob WHERE id = $1 RETURNING blob_uri;`, id)
err := row.Scan(&buri)
if err != nil {
return fmt.Errorf("deleting blob: %w", err)
}
return nil
})
row := d.queryRow(tx, `DELETE FROM blob WHERE id = $1 RETURNING blob_uri;`, id)
err := row.Scan(&buri)
if err != nil {
return "", fmt.Errorf("while deleting blob: %w", err)
return "", fmt.Errorf("when deleting blob: %w", err)
}
return buri, nil
}
Expand Down
26 changes: 13 additions & 13 deletions db/sqldb/golden/human_readable_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CREATE TYPE language AS ENUM (
CREATE TABLE analysis (
analysis_type analysis_type NOT NULL,
completed_at timestamp with time zone,
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
description text NOT NULL,
failure_code failure_code,
failure_message text,
Expand Down Expand Up @@ -79,11 +79,11 @@ ALTER TABLE ONLY analysis_artifact ADD CONSTRAINT analysis_artifact_blob_id_fkey


CREATE TABLE audit_log (
"time" timestamp with time zone NOT NULL,
action audit_log_action NOT NULL,
actor_id text NOT NULL,
actor_owner_id text,
actor_type audit_log_actor_type NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
primary_target_id text NOT NULL,
primary_target_owner_id text,
primary_target_type audit_log_target_type NOT NULL,
Expand All @@ -97,7 +97,7 @@ ALTER TABLE ONLY audit_log ADD CONSTRAINT audit_log_secondary_target_owner_id_fk

CREATE TABLE blob (
blob_uri text NOT NULL,
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
file_name text NOT NULL,
file_type file_type NOT NULL,
id text NOT NULL);
Expand All @@ -109,7 +109,7 @@ CREATE TABLE incomplete_upload (
admin_debug_enabled boolean NOT NULL,
blob_id text,
completed_at timestamp with time zone,
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
description text NOT NULL,
failure_code failure_code,
failure_message text,
Expand All @@ -125,7 +125,7 @@ ALTER TABLE ONLY incomplete_upload ADD CONSTRAINT incomplete_upload_owner_id_fke

CREATE TABLE initiative (
affiliation text NOT NULL,
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
id text NOT NULL,
internal_description text NOT NULL,
is_accepting_new_members boolean NOT NULL,
Expand All @@ -140,7 +140,7 @@ ALTER TABLE ONLY initiative ADD CONSTRAINT initiative_pacta_version_id_fkey FORE


CREATE TABLE initiative_invitation (
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
id text NOT NULL,
initiative_id text NOT NULL,
used_at timestamp with time zone,
Expand All @@ -154,7 +154,7 @@ CREATE TABLE initiative_user_relationship (
initiative_id text NOT NULL,
manager boolean NOT NULL,
member boolean NOT NULL,
updated_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
user_id text NOT NULL);
ALTER TABLE ONLY initiative_user_relationship ADD CONSTRAINT initiative_user_relationship_pkey PRIMARY KEY (user_id, initiative_id);

Expand All @@ -176,7 +176,7 @@ CREATE TABLE pacta_user (
authn_id text NOT NULL,
authn_mechanism authn_mechanism NOT NULL,
canonical_email text NOT NULL,
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
entered_email text NOT NULL,
id text NOT NULL,
name text NOT NULL,
Expand All @@ -190,7 +190,7 @@ ALTER TABLE ONLY pacta_user ADD CONSTRAINT pacta_user_pkey PRIMARY KEY (id);

CREATE TABLE pacta_version (
CONSTRAINT is_default_is_true_or_null CHECK (is_default),
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
description text NOT NULL,
digest text NOT NULL,
id text NOT NULL,
Expand All @@ -209,15 +209,15 @@ CREATE TABLE portfolio (
holdings_date timestamp with time zone,
id text NOT NULL,
name text NOT NULL,
number_of_rows bigint,
number_of_rows integer,
owner_id text NOT NULL);
ALTER TABLE ONLY portfolio ADD CONSTRAINT portfolio_pkey PRIMARY KEY (id);
ALTER TABLE ONLY portfolio ADD CONSTRAINT portfolio_blob_id_fkey FOREIGN KEY (blob_id) REFERENCES blob(id) ON DELETE RESTRICT;
ALTER TABLE ONLY portfolio ADD CONSTRAINT portfolio_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES owner(id) ON DELETE RESTRICT;


CREATE TABLE portfolio_group (
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
description text NOT NULL,
id text NOT NULL,
name text NOT NULL,
Expand All @@ -227,7 +227,7 @@ ALTER TABLE ONLY portfolio_group ADD CONSTRAINT portfolio_group_owner_id_fkey FO


CREATE TABLE portfolio_group_membership (
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
portfolio_group_id text,
portfolio_id text);
ALTER TABLE ONLY portfolio_group_membership ADD CONSTRAINT portfolio_group_membership_portfolio_group_id_fkey FOREIGN KEY (portfolio_group_id) REFERENCES portfolio_group(id) ON DELETE RESTRICT;
Expand All @@ -236,7 +236,7 @@ ALTER TABLE ONLY portfolio_group_membership ADD CONSTRAINT portfolio_group_membe

CREATE TABLE portfolio_initiative_membership (
added_by_user_id text,
created_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
initiative_id text NOT NULL,
portfolio_id text NOT NULL);
ALTER TABLE ONLY portfolio_initiative_membership ADD CONSTRAINT portfolio_initiative_membership_added_by_user_id_fkey FOREIGN KEY (added_by_user_id) REFERENCES pacta_user(id) ON DELETE RESTRICT;
Expand Down
2 changes: 1 addition & 1 deletion db/sqldb/golden/regen/humanreadableschema/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
importpath = "github.com/RMI/pacta/db/sqldb/golden/regen/humanreadableschema",
visibility = ["//visibility:private"],
deps = [
"@com_github_jackc_pgx_v4//pgxpool",
"@com_github_jackc_pgx_v5//pgxpool",
"@com_github_silicon_ally_testpgx//:testpgx",
"@com_github_silicon_ally_testpgx//migrate",
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/Silicon-Ally/testpgx"
"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"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion db/sqldb/golden/regen/schemadump/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
importpath = "github.com/RMI/pacta/db/sqldb/golden/regen/schemadump",
visibility = ["//visibility:private"],
deps = [
"@com_github_jackc_pgx_v4//pgxpool",
"@com_github_jackc_pgx_v5//pgxpool",
"@com_github_silicon_ally_testpgx//:testpgx",
"@com_github_silicon_ally_testpgx//migrate",
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion db/sqldb/golden/regen/schemadump/schemadump.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/Silicon-Ally/testpgx"
"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"
)

func main() {
Expand Down
Loading

0 comments on commit b2543c7

Please sign in to comment.