pgdb

package
v1.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2026 License: Apache-2.0 Imports: 30 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectENV

func ConnectENV() (*sql.DB, error)

ConnectENV returns a DB instance. Used for testing, it requires the following environment variables to be set - POSTGRES_HOST - POSTGRES_PORT (will default to 5432 if missing) - POSTGRES_USER - POSTGRES_PASSWORD - PENNSIEVE_DB - POSTGRES_SSL_MODE (should be set to "disable" if the server is not https, left blank if it is)

func ConnectENVWithOrg

func ConnectENVWithOrg(orgId int) (*sql.DB, error)

func ConnectRDS

func ConnectRDS() (*sql.DB, error)

ConnectRDS returns a DB instance. The Lambda function leverages IAM roles to gain access to the DB Proxy. The function does NOT set the search_path to the organization schema. Requires following LAMBDA ENV VARIABLES:

  • RDS_PROXY_ENDPOINT
  • REGION
  • ENV

If ENV is set to DOCKER, the call is redirected to ConnectENV()

func ConnectRDSWithOrg

func ConnectRDSWithOrg(orgId int) (*sql.DB, error)

ConnectRDSWithOrg returns a DB instance. The Lambda function leverages IAM roles to gain access to the DB Proxy. The function DOES set the search_path to the organization schema.

func ContributorColumns added in v1.3.7

func ContributorColumns() []string

func ReadContributorColumns added in v1.3.7

func ReadContributorColumns() string

func WriteContributorColumns added in v1.3.7

func WriteContributorColumns() string

Types

type ContributorNotFoundError added in v1.3.7

type ContributorNotFoundError struct {
	// contains filtered or unexported fields
}

func (ContributorNotFoundError) Error added in v1.3.7

func (e ContributorNotFoundError) Error() string

type CreateDatasetParams added in v1.3.7

type CreateDatasetParams struct {
	Name                         string
	Description                  string
	Status                       *pgdb.DatasetStatus
	AutomaticallyProcessPackages bool
	License                      string
	Tags                         []string
	DataUseAgreement             *pgdb.DataUseAgreement
	Type                         datasetType.DatasetType
}

type CreateDatasetReleaseParams added in v1.3.7

type CreateDatasetReleaseParams struct {
	Origin     string
	Url        string
	Label      string
	Marker     string
	Properties pgdb.Properties
	Tags       pgdb.Tags
}

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

DBTX Default interface with methods that are available for both DB adn TX sessions.

type DatasetNotFoundError added in v1.3.7

type DatasetNotFoundError struct {
	ErrorMessage string
}

func (DatasetNotFoundError) Error added in v1.3.7

func (e DatasetNotFoundError) Error() string

type DatasetOrganizationNotFoundError added in v1.19.0

type DatasetOrganizationNotFoundError struct {
	DatasetNodeId string
}

func (DatasetOrganizationNotFoundError) Error added in v1.19.0

type DatasetReleaseNotFoundError added in v1.3.7

type DatasetReleaseNotFoundError struct {
	ErrorMessage string
}

func (DatasetReleaseNotFoundError) Error added in v1.3.7

type DatasetUserNotFoundError added in v1.3.7

type DatasetUserNotFoundError struct {
	ErrorMessage string
}

func (DatasetUserNotFoundError) Error added in v1.3.7

func (e DatasetUserNotFoundError) Error() string

type NewContributor added in v1.3.7

type NewContributor struct {
	FirstName     string
	MiddleInitial string
	LastName      string
	Degree        string
	EmailAddress  string
	Orcid         string
	UserId        int64
}

type OrganizationUserNotFoundError added in v1.3.7

type OrganizationUserNotFoundError struct {
	ErrorMessage string
}

func (OrganizationUserNotFoundError) Error added in v1.3.7

type Queries

type Queries struct {
	// contains filtered or unexported fields
}

Queries is a struct with a db object that implements the DBTX interface. This means that db can either be a direct DB connection or a TX transaction.

func New

func New(db DBTX) *Queries

New returns a Queries object backed by a DBTX interface (either DB or TX)

func (*Queries) AddContributor added in v1.3.7

func (q *Queries) AddContributor(ctx context.Context, newContributor NewContributor) (*pgdb.Contributor, error)

AddContributor will add a Contributor to the Organization's Contributors table.

func (*Queries) AddDatasetContributor added in v1.3.7

func (q *Queries) AddDatasetContributor(ctx context.Context, dataset *pgdb.Dataset, contributor *pgdb.Contributor) (*pgdb.DatasetContributor, error)

func (*Queries) AddDatasetRelease added in v1.3.7

func (q *Queries) AddDatasetRelease(ctx context.Context, release pgdb.DatasetRelease) (*pgdb.DatasetRelease, error)

func (*Queries) AddDatasetUser added in v1.3.7

func (q *Queries) AddDatasetUser(ctx context.Context, dataset *pgdb.Dataset, user *pgdb.User, role role.Role) (*pgdb.DatasetUser, error)

func (*Queries) AddFiles

func (q *Queries) AddFiles(ctx context.Context, files []pgdb.FileParams) ([]pgdb.File, error)

AddFiles add files to packages

func (*Queries) AddFolder

func (q *Queries) AddFolder(ctx context.Context, r pgdb.PackageParams) (*pgdb.Package, error)

AddFolder adds a single folder to a dataset.

func (*Queries) AddOrganizationUser added in v1.3.7

func (q *Queries) AddOrganizationUser(ctx context.Context, orgId int64, userId int64, permBit pgdb.DbPermission) (*pgdb.OrganizationUser, error)

func (*Queries) AddPackages

func (q *Queries) AddPackages(ctx context.Context, records []pgdb.PackageParams) ([]pgdb.Package, error)

AddPackages adds packages to a dataset with the legacy "keep both" conflict behavior: name collisions result in the new package being renamed with a " (N)" suffix until it lands. Equivalent to AddPackagesWithConflict using conflictStrategy.KeepBoth.

  • This call should typically be wrapped in a Transaction as it will run multiple queries.
  • Packages can be in different folders, but it is assumed that the folders already exist.

func (*Queries) AddPackagesWithConflict added in v1.16.0

func (q *Queries) AddPackagesWithConflict(ctx context.Context, records []pgdb.PackageParams, strategy conflictStrategy.Strategy) ([]pgdb.Package, error)

AddPackagesWithConflict adds packages, using the given strategy to resolve name collisions with existing non-deleted packages under the same (dataset_id, parent_id, name) tuple.

  • This call should typically be wrapped in a Transaction as it will run multiple queries.
  • Collection-type records are rejected; use AddFolder instead.
  • Replace soft-deletes the conflicting predecessor (state=DELETING, name prefixed with __DELETED__<nodeId>_) and links the new package via replaces_package_id / replaced_by_package_id.
  • Fail returns an error listing conflicting names without inserting anything.

func (*Queries) CreateDataset added in v1.3.7

func (q *Queries) CreateDataset(ctx context.Context, p CreateDatasetParams) (*pgdb.Dataset, error)

func (*Queries) FindContributor added in v1.3.7

func (q *Queries) FindContributor(ctx context.Context, search NewContributor) (*pgdb.Contributor, error)

FindContributor will search for a contributor by several User Id, Email Address, and ORCID

func (*Queries) GetByCognitoId

func (q *Queries) GetByCognitoId(ctx context.Context, id string) (*pgdb.User, error)

GetByCognitoId returns a user from Postgress based on his/her cognito-id This function also returns the preferred org and whether the user is a super-admin. Returns (nil, sql.ErrNoRows) if no user with the given cognito id exists

func (*Queries) GetContributor added in v1.3.7

func (q *Queries) GetContributor(ctx context.Context, id int64) (*pgdb.Contributor, error)

GetContributor will get a Contributor by the Contributor Id (not the User Id).

func (*Queries) GetContributorByEmail added in v1.3.7

func (q *Queries) GetContributorByEmail(ctx context.Context, email string) (*pgdb.Contributor, error)

GetContributorByEmail will get a Contributor by Email Address.

func (*Queries) GetContributorByOrcid added in v1.3.7

func (q *Queries) GetContributorByOrcid(ctx context.Context, orcid string) (*pgdb.Contributor, error)

GetContributorByOrcid will get a Contributor by ORCID iD.

func (*Queries) GetContributorByUserId added in v1.3.7

func (q *Queries) GetContributorByUserId(ctx context.Context, userId int64) (*pgdb.Contributor, error)

GetContributorByUserId will get a Contributor by User Id.

func (*Queries) GetDatasetById added in v1.3.7

func (q *Queries) GetDatasetById(ctx context.Context, id int64) (*pgdb.Dataset, error)

GetDatasetById will query workspace datasets by name and return one if found.

func (*Queries) GetDatasetByName added in v1.3.7

func (q *Queries) GetDatasetByName(ctx context.Context, name string) (*pgdb.Dataset, error)

GetDatasetByName will query workspace datasets by name and return one if found.

func (*Queries) GetDatasetByNodeId added in v1.3.7

func (q *Queries) GetDatasetByNodeId(ctx context.Context, nodeId string) (*pgdb.Dataset, error)

GetDatasetByNodeId will query workspace datasets by name and return one if found.

func (*Queries) GetDatasetClaim

func (q *Queries) GetDatasetClaim(ctx context.Context, user *pgdb.User, datasetNodeId string, organizationId int64) (*dataset.Claim, error)

GetDatasetClaim returns the highest role that the user has for a given dataset. This method checks the roles of the dataset, the teams, and the specific user roles. returns (nil, sql.ErrNoRows) if no dataset with the given nodeId is found

func (*Queries) GetDatasetContributor added in v1.3.7

func (q *Queries) GetDatasetContributor(ctx context.Context, datasetId int64, contributorId int64) (*pgdb.DatasetContributor, error)

func (*Queries) GetDatasetRelease added in v1.3.7

func (q *Queries) GetDatasetRelease(ctx context.Context, datasetId int64, label string, marker string) (*pgdb.DatasetRelease, error)

func (*Queries) GetDatasetReleaseById added in v1.3.7

func (q *Queries) GetDatasetReleaseById(ctx context.Context, id int64) (*pgdb.DatasetRelease, error)

func (*Queries) GetDatasetStorageById

func (q *Queries) GetDatasetStorageById(ctx context.Context, datasetId int64) (int64, error)

func (*Queries) GetDatasetUser added in v1.3.7

func (q *Queries) GetDatasetUser(ctx context.Context, dataset *pgdb.Dataset, user *pgdb.User) (*pgdb.DatasetUser, error)

func (*Queries) GetDatasets

func (q *Queries) GetDatasets(ctx context.Context, organizationId int) ([]pgdb.Dataset, error)

GetDatasets returns all rows in the Upload Record Table

func (*Queries) GetDefaultDataUseAgreement added in v1.3.7

func (q *Queries) GetDefaultDataUseAgreement(ctx context.Context, organizationId int) (*pgdb.DataUseAgreement, error)

GetDefaultDataUseAgreement will return the default data use agreement for the organization. Returns (nil, sql.ErrNoRows) if no default data use agreement is found for the organization

func (*Queries) GetDefaultDatasetStatus added in v1.3.7

func (q *Queries) GetDefaultDatasetStatus(ctx context.Context, organizationId int) (*pgdb.DatasetStatus, error)

GetDefaultDatasetStatus will return the default dataset status for the organization. This is assumed to be the dataset status row with the lowest id number. Returns (nil, sql.ErrNoRows) if no dataset status is found for the organization

func (*Queries) GetEnabledFeatureFlags added in v1.3.7

func (q *Queries) GetEnabledFeatureFlags(ctx context.Context, organizationId int64) ([]pgdb.FeatureFlags, error)

func (*Queries) GetFeatureFlags

func (q *Queries) GetFeatureFlags(ctx context.Context, organizationId int64) ([]pgdb.FeatureFlags, error)

GetFeatureFlags returns all rows in the FeatureFlags Table

func (*Queries) GetOrganization

func (q *Queries) GetOrganization(ctx context.Context, id int64) (*pgdb.Organization, error)

GetOrganization returns a single organization

func (*Queries) GetOrganizationByName added in v1.3.7

func (q *Queries) GetOrganizationByName(ctx context.Context, name string) (*pgdb.Organization, error)

GetOrganizationByName returns a single organization

func (*Queries) GetOrganizationByNodeId added in v1.3.7

func (q *Queries) GetOrganizationByNodeId(ctx context.Context, nodeId string) (*pgdb.Organization, error)

GetOrganizationByNodeId returns a single organization

func (*Queries) GetOrganizationBySlug added in v1.3.7

func (q *Queries) GetOrganizationBySlug(ctx context.Context, slug string) (*pgdb.Organization, error)

GetOrganizationBySlug returns a single organization

func (*Queries) GetOrganizationClaim

func (q *Queries) GetOrganizationClaim(ctx context.Context, userId int64, organizationId int64) (*organization.Claim, error)

GetOrganizationClaim returns an organization claim for a specific user given a workspace id

func (*Queries) GetOrganizationClaimByNodeId added in v1.3.7

func (q *Queries) GetOrganizationClaimByNodeId(ctx context.Context, userId int64, organizationNodeId string) (*organization.Claim, error)

GetOrganizationClaimByNodeId returns an organization claim for a specific user given a workspace node id

func (*Queries) GetOrganizationIdForDataset added in v1.19.0

func (q *Queries) GetOrganizationIdForDataset(ctx context.Context, datasetNodeId string) (int64, error)

GetOrganizationIdForDataset resolves the organization id that owns the given dataset node id using the global pennsieve.dataset_organization map. Returns DatasetOrganizationNotFoundError if the node id is not present in the map, so callers can distinguish a genuine map miss (deny) from a DB/connection failure (retryable error).

func (*Queries) GetOrganizationStorageById

func (q *Queries) GetOrganizationStorageById(ctx context.Context, organizationId int64) (int64, error)

func (*Queries) GetOrganizationUser added in v1.3.7

func (q *Queries) GetOrganizationUser(ctx context.Context, orgId int64, userId int64) (*pgdb.OrganizationUser, error)

GetOrganizationUser returns the *pgdb.OrganizationUser with the give org and user ids. Returns (nil, OrganizationUserNotFoundError) if no such org user is found

func (*Queries) GetOrganizationUserById

func (q *Queries) GetOrganizationUserById(ctx context.Context, id int64) (*pgdb.OrganizationUser, error)

GetOrganizationUserById returns *pgdb.OrganizationUser with the given user id. If no such user exists, returns (nil, sql.ErrNoRows)

func (*Queries) GetPackageAncestorIds added in v1.3.7

func (q *Queries) GetPackageAncestorIds(ctx context.Context, packageId int64) ([]int64, error)

GetPackageAncestorIds returns an array of Package Ids corresponding with the ancestor Package Ids for the provided package.

  • resulting array includes requested package Id as first entry
  • resulting array includes first folder in dataset as last entry if package is in nested folder

func (*Queries) GetPackageByNodeId

func (q *Queries) GetPackageByNodeId(ctx context.Context, nodeId string) (*pgdb.Package, error)

func (*Queries) GetPackageChildren

func (q *Queries) GetPackageChildren(ctx context.Context, parent *pgdb.Package, datasetId int, onlyFolders bool) ([]pgdb.Package, error)

GetPackageChildren Get the children in a package

func (*Queries) GetPackageStorageById

func (q *Queries) GetPackageStorageById(ctx context.Context, packageId int64) (int64, error)

func (*Queries) GetTeamClaims deprecated added in v1.3.7

func (q *Queries) GetTeamClaims(ctx context.Context, userId int64) ([]teamUser.Claim, error)

Deprecated: GetTeamClaims resolves teams using the user's preferred_org_id (via GetTeamMemberships), which is mutable server-side state and wrong for org/dataset-scoped auth. Use GetTeamClaimsForOrg with the organization resolved from the request instead.

func (*Queries) GetTeamClaimsForOrg added in v1.19.0

func (q *Queries) GetTeamClaimsForOrg(ctx context.Context, userId int64, organizationId int64) ([]teamUser.Claim, error)

GetTeamClaimsForOrg returns the user's team claims within the given organization. Unlike the deprecated GetTeamClaims, the organization is supplied explicitly (resolved from the request) rather than read from the user's mutable preferred_org_id.

func (*Queries) GetTeamMemberships deprecated added in v1.3.7

func (q *Queries) GetTeamMemberships(ctx context.Context, userId int64) ([]UserTeamMembership, error)

Deprecated: GetTeamMemberships resolves teams using the user's preferred_org_id, which is mutable server-side state and is wrong for org/dataset-scoped auth (it returns the preferred org's team memberships regardless of which org the request targets). Use GetTeamMembershipsForOrg with the organization resolved from the request instead.

func (*Queries) GetTeamMembershipsForOrg added in v1.19.0

func (q *Queries) GetTeamMembershipsForOrg(ctx context.Context, userId int64, organizationId int64) ([]UserTeamMembership, error)

GetTeamMembershipsForOrg returns the user's team memberships within the given organization. Unlike the deprecated GetTeamMemberships, the organization is supplied explicitly (resolved from the request) rather than read from the user's mutable preferred_org_id.

func (*Queries) GetTokenByCognitoId

func (q *Queries) GetTokenByCognitoId(ctx context.Context, id string) (*pgdb.Token, error)

GetTokenByCognitoId returns a user from Postgress based on his/her cognito-id This function also returns the preferred org and whether the user is a super-admin. Returns (nil, sql.ErrNoRows) if no user with the given cognito id exists.

func (*Queries) GetUserByCognitoId

func (q *Queries) GetUserByCognitoId(ctx context.Context, id string) (*pgdb.User, error)

GetUserByCognitoId returns a Pennsieve User based on the cognito id in the token pool. Returns (nil, sql.ErrNoRows) if no user with the given token exists.

NOTE: the returned User.PreferredOrg is populated from the *token's* organization_id (the workspace the API token is bound to), NOT from the user's persisted preferred_org_id column. This is intentional for API-key/token-pool auth — the token carries its own workspace — but callers performing org/dataset-scoped authorization must not treat User.PreferredOrg here as the user's active/preferred org.

func (*Queries) GetUserById added in v1.3.7

func (q *Queries) GetUserById(ctx context.Context, id int64) (*pgdb.User, error)

GetUserById returns a user from Postgres based on the user's int id This function also returns the preferred org and whether the user is a super-admin. Returns (nil, sql.ErrNoRows) if no user with the given id exists.

func (*Queries) IncrementDatasetStorage

func (q *Queries) IncrementDatasetStorage(ctx context.Context, datasetId int64, size int64) error

IncrementDatasetStorage increases the storage associated with the provided dataset.

func (*Queries) IncrementOrganizationStorage

func (q *Queries) IncrementOrganizationStorage(ctx context.Context, organizationId int64, size int64) error

IncrementOrganizationStorage increases the storage associated with the provided organization.

func (*Queries) IncrementPackageStorage

func (q *Queries) IncrementPackageStorage(ctx context.Context, packageId int64, size int64) error

IncrementPackageStorage increases the storage associated with the provided package.

func (*Queries) IncrementPackageStorageAncestors

func (q *Queries) IncrementPackageStorageAncestors(ctx context.Context, parentId int64, size int64) error

IncrementPackageStorageAncestors increases the storage associated with the parents of the provided package.

func (*Queries) IsFilePublished added in v1.15.1

func (q *Queries) IsFilePublished(ctx context.Context, uploadId string, organizationId int64) (bool, error)

IsFilePublished checks whether a file identified by its UUID has been published by looking for a non-null published_s3_version_id.

func (*Queries) PrepareReplace added in v1.16.2

func (q *Queries) PrepareReplace(ctx context.Context, predecessors []*pgdb.Package) error

PrepareReplace frees the name in the packages table so an insert for a new file with the same name can claim it. For each predecessor it renames the row to "__DELETED__<NodeId>_<oldName>" and sets state = DELETING.

PrepareReplace handles only this "make room" step. After calling it the caller still needs to:

  • INSERT the new package with replaces_package_id pointing at the predecessor's id, and
  • UPDATE the predecessor's replaced_by_package_id to point at the new package once it exists.

Run this inside the same transaction as those inserts and updates (use Queries.WithTx). The full replace looks like: PrepareReplace → insert new → set back-reference → commit. Then call packagedelete.DeletePackages to hand off storage/S3/restore-record cleanup to process-jobs-service.

Each predecessor needs Id, NodeId, and Name set; findConflictingPackages already returns those.

func (*Queries) SetUpdatedAt added in v1.3.7

func (q *Queries) SetUpdatedAt(ctx context.Context, datasetId int64, t time.Time) error

func (*Queries) ShowSearchPath

func (q *Queries) ShowSearchPath(loc string)

func (*Queries) UpdateBucketForFile

func (q *Queries) UpdateBucketForFile(ctx context.Context, uploadId string, bucket string, s3Key string, organizationId int64) error

UpdateBucketForFile updates the storage bucket as part of upload process.

func (*Queries) UpdateBucketForUnpublishedFile added in v1.15.1

func (q *Queries) UpdateBucketForUnpublishedFile(ctx context.Context, uploadId string, bucket string, s3Key string, organizationId int64) error

UpdateBucketForUnpublishedFile updates the storage bucket for a file, but only if the file has not been published (published_s3_version_id IS NULL). This prevents the upload-move workflow from overwriting a publish-bucket location in the DB. Returns ErrFileNotFound if the file does not exist. Returns ErrFileAlreadyPublished if the file was published (0 rows affected due to the guard).

func (*Queries) UpdateDatasetRelease added in v1.3.7

func (q *Queries) UpdateDatasetRelease(ctx context.Context, release pgdb.DatasetRelease) (*pgdb.DatasetRelease, error)

func (*Queries) WithOrg

func (q *Queries) WithOrg(orgId int) (*Queries, error)

func (*Queries) WithTx

func (q *Queries) WithTx(tx *sql.Tx) *Queries

WithTx Returns a new Queries object wrapped by a transactions.

type SQLStore

type SQLStore struct {
	*Queries
	// contains filtered or unexported fields
}

SQLStore provides the Queries interface and a db instance.

func NewSQLStore

func NewSQLStore(db *sql.DB) *SQLStore

NewSQLStore returns a SQLStore object which implements the Queries

type UserTeamMembership added in v1.3.7

type UserTeamMembership struct {
	UserId            int64
	UserEmail         string
	UserNodeId        string
	OrgId             int64
	OrgName           string
	OrgNodeId         string
	OrgUserPermission pgdb.DbPermission
	TeamId            int64
	TeamName          string
	TeamNodeId        string
	TeamPermission    pgdb.DbPermission
	TeamType          sql.NullString
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL