Documentation ¶
Overview ¶
Package pgstore provides a worklog data storage layer using PostgreSQL.
Index ¶
- Constants
- type DB
- func (db *DB) AmendEvents(ctx context.Context, ts time.Time, note *worklog.Amendment) (sql.Result, error)
- func (db *DB) Backup(ctx context.Context, dir string) (string, error)
- func (db *DB) BucketID(uid string) string
- func (db *DB) BucketMetadata(ctx context.Context, bid string) (*worklog.BucketMetadata, error)
- func (db *DB) Buckets(ctx context.Context) ([]worklog.BucketMetadata, error)
- func (db *DB) Close(ctx context.Context) error
- func (db *DB) CreateBucket(ctx context.Context, uid, name, typ, client string, created time.Time, ...) (m *worklog.BucketMetadata, err error)
- func (db *DB) Dump(ctx context.Context) ([]worklog.BucketMetadata, error)
- func (db *DB) DumpRange(ctx context.Context, start, end time.Time) ([]worklog.BucketMetadata, error)
- func (db *DB) Events(ctx context.Context, bid string) ([]worklog.Event, error)
- func (db *DB) EventsRange(ctx context.Context, bid string, start, end time.Time, limit int) ([]worklog.Event, error)
- func (db *DB) EventsRangeFunc(ctx context.Context, bid string, start, end time.Time, limit int, ...) error
- func (db *DB) InsertEvent(ctx context.Context, e *worklog.Event) (sql.Result, error)
- func (db *DB) LastEvent(ctx context.Context, uid string) (*worklog.Event, error)
- func (db *DB) Load(ctx context.Context, buckets []worklog.BucketMetadata, replace bool) (err error)
- func (db *DB) Name() string
- func (db *DB) Select(ctx context.Context, query string) ([]map[string]any, error)
- func (db *DB) UpdateEvent(ctx context.Context, e *worklog.Event) (sql.Result, error)
- type Warning
Constants ¶
const ( EventsRange = `` /* 192-byte string literal not displayed */ EventsRangeUntil = `` /* 174-byte string literal not displayed */ EventsRangeFrom = `` /* 172-byte string literal not displayed */ EventsLimit = `` /* 154-byte string literal not displayed */ )
const AmendEventsPrepare = `` /* 208-byte string literal not displayed */
const AmendEventsUpdate = `` /* 1007-byte string literal not displayed */
const BucketMetadata = `select id, name, type, client, hostname, created, timezone, datastr from buckets where id = $1`
const Buckets = `select id, name, type, client, hostname, created, timezone, datastr from buckets`
const CreateBucket = `` /* 150-byte string literal not displayed */
const DeleteBucket = `delete from buckets where id = $1`
const DeleteBucketEvents = `delete from events where bucketrow in (
select rowid from buckets where id = $1
)`
const DeleteEvent = `delete from events where bucketrow = (
select rowid from buckets where id = $1
) and id = $2`
const Event = `` /* 143-byte string literal not displayed */
const Events = `select id, starttime, endtime, timezone, datastr from events where bucketrow = (
select rowid from buckets where id = $1
)`
const InsertEvent = `` /* 148-byte string literal not displayed */
const LastEvent = `` /* 255-byte string literal not displayed */
const Schema = `` /* 908-byte string literal not displayed */
Schema is the DB schema.
const UpdateEvent = `` /* 148-byte string literal not displayed */
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a persistent store.
func Open ¶
Open opens a PostgresSQL DB. See pgx.Connect for name handling details. Two connections to the database are created, one using the username within the name parameter and one with the same username, but "_ro" appended. If the second user does not have SELECT role_table_grants for the buckets' and 'events' the database, or has any non-SELECT role_table_grants, the second connection will be closed and a Warning error will be returned. If the second connection is closed, the DB.Select method will return a non-nil error and perform no DB operation. Open attempts to get the CNAME for the host, which may wait indefinitely, so a timeout context can be provided to fall back to the kernel-provided hostname.
func (*DB) AmendEvents ¶
func (db *DB) AmendEvents(ctx context.Context, ts time.Time, note *worklog.Amendment) (sql.Result, error)
AmendEvents adds amendment notes to the data for events in the store overlapping the note. On return the note.Replace slice will be sorted.
The SQL commands run are AmendEventsPrepare and AmendEventsUpdate in a transaction.
func (*DB) Backup ¶
Backup creates a backup of the DB using the pg_dump command into the provided directory as a gzip file. It returns the path of the backup.
func (*DB) BucketMetadata ¶
BucketMetadata returns the metadata for the bucket with the provided internal bucket ID. The SQL command run is BucketMetadata.
func (*DB) Buckets ¶
Buckets returns the full set of bucket metadata. The SQL command run is Buckets.
func (*DB) CreateBucket ¶
func (db *DB) CreateBucket(ctx context.Context, uid, name, typ, client string, created time.Time, data map[string]any) (m *worklog.BucketMetadata, err error)
CreateBucket creates a new entry in the bucket table. The SQL command run is CreateBucket.
func (*DB) Dump ¶
Dump dumps the complete database into a slice of worklog.BucketMetadata.
func (*DB) DumpRange ¶
func (db *DB) DumpRange(ctx context.Context, start, end time.Time) ([]worklog.BucketMetadata, error)
DumpRange dumps the database spanning the specified time range into a slice of worklog.BucketMetadata.
func (*DB) Events ¶
Buckets returns the full set of events in the bucket with the provided internal bucket ID. The SQL command run is Events.
func (*DB) EventsRange ¶
func (db *DB) EventsRange(ctx context.Context, bid string, start, end time.Time, limit int) ([]worklog.Event, error)
EventsRange returns the events in the bucket with the provided bucket ID within the specified time range, sorted descending by end time. The SQL command run is EventsRange, EventsRangeUntil, EventsRangeFrom or EventsLimit depending on whether start and end are zero.
func (*DB) EventsRangeFunc ¶
func (db *DB) EventsRangeFunc(ctx context.Context, bid string, start, end time.Time, limit int, fn func(worklog.Event) error) error
EventsRange calls fn on all the events in the bucket with the provided bucket ID within the specified time range, sorted descending by end time. The SQL command run is EventsRange, EventsRangeUntil, EventsRangeFrom or EventsLimit depending on whether start and end are zero.
func (*DB) InsertEvent ¶
InsertEvent inserts a new event into the events table. The SQL command run is InsertEvent.
func (*DB) LastEvent ¶
LastEvent returns the last event in the named bucket. The SQL command run is LastEvent.
func (*DB) Load ¶
Load loads a complete database from a slice of worklog.BucketMetadata. Event IDs will be regenerated by the backing database and so will not match the input data. If replace is true and a bucket already exists matching the bucket in the provided buckets slice, the existing events will be deleted and replaced. If replace is false, the new events will be added to the existing events in the store.