db

package
v0.4.34 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: BSD-3-Clause-Clear Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrTextCreationFailed      = "resource creation failed"
	ErrTextDeletionFailed      = "resource deletion failed"
	ErrTextDeactivationFailed  = "resource deactivation failed"
	ErrTextGetRetrievalFailed  = "resource retrieval failed"
	ErrTextListRetrievalFailed = "resource list retrieval failed"
	ErrTextUpdateFailed        = "resource update failed"
	ErrTextNotFound            = "resource not found"
	ErrTextConflict            = "resource unique field violation"
	ErrTextRelationInvalid     = "resource relation invalid"
	ErrTextEnumValueInvalid    = "enum value invalid"
	ErrTextUUIDInvalid         = "invalid input syntax for type uuid"
	ErrTextRestrictViolation   = "intended action would violate a restriction"
	ErrTextFqnMissingValue     = "FQN must specify a valid value and be of format 'https://<namespace>/attr/<attribute name>/value/<value>'"
	ErrTextListLimitTooLarge   = "requested pagination limit must be less than or equal to configured limit"
)

Variables

View Source
var (
	ErrUniqueConstraintViolation = errors.New("ErrUniqueConstraintViolation: value must be unique")
	ErrNotNullViolation          = errors.New("ErrNotNullViolation: value cannot be null")
	ErrForeignKeyViolation       = errors.New("ErrForeignKeyViolation: value is referenced by another table")
	ErrRestrictViolation         = errors.New("ErrRestrictViolation: value cannot be deleted due to restriction")
	ErrNotFound                  = errors.New("ErrNotFound: value not found")
	ErrEnumValueInvalid          = errors.New("ErrEnumValueInvalid: not a valid enum value")
	ErrUUIDInvalid               = errors.New("ErrUUIDInvalid: value not a valid UUID")
	ErrMissingValue              = errors.New("ErrMissingValue: value must be included")
	ErrListLimitTooLarge         = errors.New("ErrListLimitTooLarge: requested limit greater than configured maximum")
	ErrTxBeginFailed             = errors.New("ErrTxBeginFailed: failed to begin DB transaction")
	ErrTxRollbackFailed          = errors.New("ErrTxRollbackFailed: failed to rollback DB transaction")
	ErrTxCommitFailed            = errors.New("ErrTxCommitFailed: failed to commit DB transaction")
)

Functions

func GrantedPolicyObjectProtoJSON added in v0.4.19

func GrantedPolicyObjectProtoJSON(grantsJSON []byte) ([]*kasregistry.GrantedPolicyObject, error)

func IsQueryBuilderSetClauseError

func IsQueryBuilderSetClauseError(err error) bool

func KeyAccessServerProtoJSON

func KeyAccessServerProtoJSON(keyAccessServerJSON []byte) ([]*policy.KeyAccessServer, error)

func MarshalCreateMetadata

func MarshalCreateMetadata(metadata *common.MetadataMutable) ([]byte, *common.Metadata, error)

func MarshalUpdateMetadata

func MarshalUpdateMetadata(m *common.MetadataMutable, b common.MetadataUpdateEnum, getExtendableMetadata func() (*common.Metadata, error)) ([]byte, *common.Metadata, error)

func NewStatementBuilder

func NewStatementBuilder() sq.StatementBuilderType

Postgres uses $1, $2, etc. for placeholders

func NewTable

func NewTable(schema string) func(name string) Table

func NewUniqueAlreadyExistsError

func NewUniqueAlreadyExistsError(value string) error

func StatusifyError

func StatusifyError(err error, fallbackErr string, log ...any) error

func WrapIfKnownInvalidQueryErr

func WrapIfKnownInvalidQueryErr(err error) error

Get helpful error message for PostgreSQL violation

Types

type Client

type Client struct {
	Pgx    PgxIface
	Logger *logger.Logger

	// This is the stdlib connection that is used for transactions
	SQLDB *sql.DB
	// contains filtered or unexported fields
}

A wrapper around a pgxpool.Pool and sql.DB reference.

Each service should have a single instance of the Client to share a connection pool, schema (driven by the service namespace), and an embedded file system for migrations.

The 'search_path' is set to the schema on connection to the database.

If the database config 'runMigrations' is set to true, the client will run migrations on startup, once per namespace (as there should only be one embedded migrations FS per namespace).

Multiple pools, schemas, or migrations per service are not supported. Multiple databases per PostgreSQL instance or multiple PostgreSQL servers per platform instance are not supported.

func New

func New(ctx context.Context, config Config, logCfg logger.Config, o ...OptsFunc) (*Client, error)

func (*Client) Close

func (c *Client) Close()

func (Client) Exec

func (c Client) Exec(ctx context.Context, sql string, args []interface{}) error

Common function for all exec calls

func (*Client) MigrationDown

func (c *Client) MigrationDown(ctx context.Context, migrations *embed.FS) error

func (*Client) MigrationStatus

func (c *Client) MigrationStatus(ctx context.Context) ([]*goose.MigrationStatus, error)

func (*Client) MigrationsEnabled added in v0.4.18

func (c *Client) MigrationsEnabled() bool

func (Client) Query

func (c Client) Query(ctx context.Context, sql string, args []interface{}) (pgx.Rows, error)

Common function for all query calls

func (Client) QueryRow

func (c Client) QueryRow(ctx context.Context, sql string, args []interface{}) (pgx.Row, error)

Common function for all queryRow calls

func (*Client) RanMigrations added in v0.4.18

func (c *Client) RanMigrations() bool

func (*Client) RunMigrations

func (c *Client) RunMigrations(ctx context.Context, migrations *embed.FS) (int, error)

RunMigrations runs the migrations for the schema Schema will be created if it doesn't exist

func (*Client) Schema

func (c *Client) Schema() string

type Config

type Config struct {
	Host          string `mapstructure:"host" json:"host" default:"localhost"`
	Port          int    `mapstructure:"port" json:"port" default:"5432"`
	Database      string `mapstructure:"database" json:"database" default:"opentdf"`
	User          string `mapstructure:"user" json:"user" default:"postgres"`
	Password      string `mapstructure:"password" json:"password" default:"changeme"`
	RunMigrations bool   `mapstructure:"runMigrations" json:"runMigrations" default:"true"`
	SSLMode       string `mapstructure:"sslmode" json:"sslmode" default:"prefer"`
	Schema        string `mapstructure:"schema" json:"schema" default:"opentdf"`

	VerifyConnection bool      `mapstructure:"verifyConnection" json:"verifyConnection" default:"true"`
	MigrationsFS     *embed.FS `mapstructure:"-"`
}

func (Config) LogValue added in v0.4.19

func (c Config) LogValue() slog.Value

type OptsFunc

type OptsFunc func(c Config) Config

func WithMigrations

func WithMigrations(fs *embed.FS) OptsFunc

func WithService

func WithService(name string) OptsFunc

type PgxIface

type PgxIface interface {
	Acquire(ctx context.Context) (*pgxpool.Conn, error)
	Begin(ctx context.Context) (pgx.Tx, error)
	Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
	QueryRow(context.Context, string, ...any) pgx.Row
	Query(context.Context, string, ...any) (pgx.Rows, error)
	Ping(context.Context) error
	Close()
	Config() *pgxpool.Config
}

We can rename this but wanted to get mocks working.

type Table

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

func (Table) Field

func (t Table) Field(field string) string

func (Table) Name

func (t Table) Name() string

func (Table) WithoutSchema

func (t Table) WithoutSchema() Table

Jump to

Keyboard shortcuts

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