Documentation
¶
Overview ¶
Package psql is the Postgresql storage layer of Qvain.
Index ¶
- Constants
- Variables
- func NewError(text string) error
- type BatchManager
- func (b *BatchManager) Commit() error
- func (b *BatchManager) Create(dataset *models.Dataset) error
- func (b *BatchManager) CreateWithMetadata(dataset *models.Dataset) error
- func (b *BatchManager) Rollback()
- func (b *BatchManager) Update(id uuid.UUID, blob []byte) error
- func (b *BatchManager) Upsert(data *models.Dataset) error
- type DB
- func (db *DB) BatchStore(datasets []*models.Dataset) error
- func (psql *DB) Begin() (*Tx, error)
- func (db *DB) ChangeOwnerTo(id uuid.UUID, uid uuid.UUID) error
- func (psql *DB) Check() error
- func (db *DB) CheckOwner(id uuid.UUID, owner uuid.UUID) (err error)
- func (db *DB) Clone(id uuid.UUID, newid uuid.UUID, blob []byte) error
- func (psql *DB) Connect() (*pgx.Conn, error)
- func (db *DB) Create(dataset *models.Dataset) error
- func (db *DB) Delete(id uuid.UUID, owner *uuid.UUID) error
- func (db *DB) ExportAsJson(id uuid.UUID) (json.RawMessage, error)
- func (db *DB) Get(id uuid.UUID) (*models.Dataset, error)
- func (db *DB) GetIdentityForUid(svc string, uid uuid.UUID) (id string, err error)
- func (db *DB) GetLastSync(uid uuid.UUID) (time.Time, error)
- func (db *DB) GetUidForIdentity(svc, id string) (uid uuid.UUID, err error)
- func (db *DB) GetWithOwner(id uuid.UUID, owner uuid.UUID) (*models.Dataset, error)
- func (psql *DB) InitPool() (err error)
- func (db *DB) ListAllForUid(uid uuid.UUID) ([]*models.Dataset, error)
- func (psql *DB) Log(plevel pgx.LogLevel, msg string, data map[string]interface{})
- func (db *DB) LookupByFairdataIdentifier(fdid string) (uuid.UUID, error)
- func (db *DB) LookupByQvainId(id uuid.UUID) (bool, error)
- func (db *DB) MarkPublished(id uuid.UUID, published bool) error
- func (db *DB) MarkPublishedWithOwner(id uuid.UUID, owner uuid.UUID, published bool) error
- func (psql *DB) MustConnect() *pgx.Conn
- func (db *DB) NewBatch() (*BatchManager, error)
- func (db *DB) NewBatchForUser(uid uuid.UUID) (*BatchManager, error)
- func (db *DB) Patch(id uuid.UUID, blob []byte) error
- func (db *DB) PatchWithOwner(id uuid.UUID, blob []byte, owner uuid.UUID) error
- func (db *DB) RegisterIdentity(svc, id string) (uid uuid.UUID, isNew bool, err error)
- func (psql *DB) SetLogger(logger zerolog.Logger)
- func (db *DB) SmartGetWithOwner(id uuid.UUID, owner uuid.UUID) (*models.Dataset, error)
- func (db *DB) SmartUpdateWithOwner(id uuid.UUID, blob []byte, owner uuid.UUID) error
- func (db *DB) StoreNewVersion(id uuid.UUID, basedOn uuid.UUID, created time.Time, blob []byte) error
- func (db *DB) StorePublished(id uuid.UUID, blob []byte) error
- func (db *DB) Update(id uuid.UUID, blob []byte) error
- func (db *DB) UpdateWithOwner(id uuid.UUID, blob []byte, owner uuid.UUID) error
- func (psql *DB) Version() (string, error)
- func (db *DB) ViewDatasetWithOwner(id uuid.UUID, owner uuid.UUID, svc string) (json.RawMessage, error)
- func (db *DB) ViewDatasetsByOwner(owner uuid.UUID) (json.RawMessage, error)
- func (db *DB) ViewVersions(owner uuid.UUID, dataset uuid.UUID) (json.RawMessage, error)
- func (db *DB) WithTransaction(f func(tx *Tx) error) error
- type DatabaseError
- type Tx
Constants ¶
const DefaultPoolAcquireTimeout = 10 * time.Second
DefaultPoolAcquireTimeout is the duration pgx waits for a connection to become available from the pool.
Variables ¶
var ( ErrExists = NewError("exists") ErrNotFound = NewError("not found") ErrNotOwner = NewError("not owner") ErrInvalidJson = NewError("invalid json") ErrNotImplemented = NewError("not implemented") )
Errors exported by the database layer.
var ( ErrTemporary = NewError("temporary database error") ErrTimeout = NewError("database timeout") ErrConnection = NewError("database connection error") )
Errors from the underlying database connection.
Functions ¶
Types ¶
type BatchManager ¶
type BatchManager struct {
// contains filtered or unexported fields
}
func (*BatchManager) Commit ¶
func (b *BatchManager) Commit() error
func (*BatchManager) CreateWithMetadata ¶
func (b *BatchManager) CreateWithMetadata(dataset *models.Dataset) error
func (*BatchManager) Rollback ¶
func (b *BatchManager) Rollback()
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB holds the database methods and configuration.
func NewPoolService ¶
NewPoolService creates a psql service from a connection string and initialises the connection pool.
func NewPoolServiceFromEnv ¶
NewPoolService creates a psql service using environment variables and initialises the connection pool.
func NewService ¶
NewService returns a database handle configured with the given connection string. It does not try to connect.
func (*DB) BatchStore ¶
BatchStore takes a list of datasets and stores them as new datasets.
func (*DB) ChangeOwnerTo ¶
ChangeOwnerTo updates a dataset's owner.
func (*DB) CheckOwner ¶
CheckOwner calls tx.CheckOwner to check if the record exists and is owner by the given user.
func (*DB) Create ¶
Create creates a new dataset. It is a convenience wrapper for the Create method on transactions.
func (*DB) ExportAsJson ¶
func (*DB) GetIdentityForUid ¶
GetIdentityForUid gets the identity for a given uid and service.
func (*DB) GetUidForIdentity ¶
GetUidForIdentity gets the application uid for a given external service and identity.
Note that identities need not be unique, though those used for login ought to be.
func (*DB) GetWithOwner ¶
GetWithOwner retrieves a dataset from the database if the owner matches.
func (*DB) ListAllForUid ¶
ListAllForUid returns the list of datasets for a given user.
func (*DB) LookupByFairdataIdentifier ¶
LookupByFairdataIdentifier returns the Qvain id for a given Fairdata identifier.
func (*DB) MarkPublished ¶
MarkPublished marks a dataset as published and updates its sync time. It does not do owner checks.
func (*DB) MarkPublishedWithOwner ¶
MarkPublishedByOwner marks a dataset as published and updates its sync time. It checks if the given user is the dataset's owner first.
func (*DB) MustConnect ¶
MustConnect returns a single database conn and panics on failure.
func (*DB) NewBatch ¶
func (db *DB) NewBatch() (*BatchManager, error)
func (*DB) NewBatchForUser ¶
func (db *DB) NewBatchForUser(uid uuid.UUID) (*BatchManager, error)
func (*DB) PatchWithOwner ¶
PatchWithOwner patches a dataset JSON blob with ownership checks.
func (*DB) RegisterIdentity ¶
RegisterIdentity takes an external service and identity and gets the corresponding application uid, creating it if necessary. Returns either a UUID uid and a boolean indicating if the account has been newly created or an error.
Note that while external ids are not guaranteed to be unique and might refer to multiple application users, this function allows only unique registrations as it creates real (login) users mapped to external accounts.
func (*DB) SetLogger ¶
SetLogger assigns a zerolog logger to the database service. It is not safe to call this function after initialisation.
func (*DB) SmartGetWithOwner ¶
func (*DB) SmartUpdateWithOwner ¶
func (*DB) StoreNewVersion ¶
func (db *DB) StoreNewVersion(id uuid.UUID, basedOn uuid.UUID, created time.Time, blob []byte) error
StoreNewVersion wraps a StoreNewVersion transaction.
func (*DB) StorePublished ¶
StorePublished saves a published dataset to the database and marks it as published. TODO: handle empty blob
func (*DB) UpdateWithOwner ¶
UpdateWithOwner updates a dataset with ownership checks.
func (*DB) ViewDatasetWithOwner ¶
func (*DB) ViewDatasetsByOwner ¶
ViewDatasetsByOwner builds a JSON array with the datasets for a given owner.
func (*DB) ViewVersions ¶
ViewVersions returns a (JSON) array with existing versions for a given dataset and owner.
type DatabaseError ¶
type DatabaseError struct {
// contains filtered or unexported fields
}
DatabaseError is a trivial implementation of error.
func (*DatabaseError) Error ¶
func (e *DatabaseError) Error() string
Error satisfies Go's Error interface.