Documentation ¶
Index ¶
- Constants
- Variables
- func GenerateUpsertStrings(model interface{}) (string, string)
- func LatestSchemaVersion() model.Version
- type AdvisoryLock
- type CSVBatch
- type CSVStorage
- type CSVStorageOptions
- type Catalog
- type Connector
- type Database
- func (d *Database) AsORM() *pg.DB
- func (d *Database) Close(ctx context.Context) error
- func (d *Database) Connect(ctx context.Context) error
- func (d *Database) ExecContext(c context.Context, query interface{}, params ...interface{}) (pg.Result, error)
- func (d *Database) GetSchemaVersions(ctx context.Context) (model.Version, model.Version, error)
- func (d *Database) IsConnected(ctx context.Context) bool
- func (d *Database) MigrateSchema(ctx context.Context) error
- func (d *Database) MigrateSchemaTo(ctx context.Context, target model.Version) error
- func (d *Database) PersistBatch(ctx context.Context, ps ...model.Persistable) error
- func (d *Database) SchemaConfig() schemas.Config
- func (d *Database) VerifyCurrentSchema(ctx context.Context) error
- type MemStorage
- type Metadata
- type NullStorage
- type StorageWithMetadata
- type TxStorage
Constants ¶
const ( FilePatternTokenTable = "{table}" FilePatternTokenJobName = "{jobname}" DefaultFilePattern = FilePatternTokenTable + ".csv" )
const MaxPostgresNameLength = 64
const PostgresTimestampFormat = "2006-01-02T15:04:05.999Z07:00"
Variables ¶
var ( ErrSchemaTooOld = errors.New("database schema is too old and requires migration") ErrSchemaTooNew = errors.New("database schema is too new for this version of visor") ErrNameTooLong = errors.New("name exceeds maximum length for postgres application names") )
var ErrLockNotAcquired = errors.New("lock not acquired")
var ErrLockNotReleased = errors.New("lock not released")
var ErrMarshalUnsupportedType = errors.New("cannot marshal unsupported type")
Functions ¶
func GenerateUpsertStrings ¶ added in v0.5.0
GenerateUpsertString accepts a visor model and returns two string containing SQL that may be used to upsert the model. The first string is the conflict statement and the second is the insert.
Example given the below model:
type SomeModel struct { Height int64 `pg:",pk,notnull,use_zero"` MinerID string `pg:",pk,notnull"` StateRoot string `pg:",pk,notnull"` OwnerID string `pg:",notnull"` WorkerID string `pg:",notnull"` }
The strings returned are: conflict string:
"(cid, height, state_root) DO UPDATE"
update string:
"owner_id" = EXCLUDED.owner_id, "worker_id" = EXCLUDED.worker_id
func LatestSchemaVersion ¶ added in v0.6.6
LatestSchemaVersion returns the most recent version of the model schema. It is based on the highest migration version in the highest major schema version
Types ¶
type AdvisoryLock ¶
type AdvisoryLock int64
An AdvisoryLock is a lock that is managed by Postgres but is only enforced by the application. Advisory locks are automatically released at the end of a session. It is safe to hold both a shared and exclusive lock within a single session.
var (
SchemaLock AdvisoryLock = 1
)
Advisory locks
func (AdvisoryLock) LockExclusive ¶
func (l AdvisoryLock) LockExclusive(ctx context.Context, db *pg.DB) error
LockShared tries to acquire a session scoped exclusive advisory lock.
func (AdvisoryLock) LockShared ¶
func (l AdvisoryLock) LockShared(ctx context.Context, db *pg.DB) error
LockShared tries to acquire a session scoped shared advisory lock.
func (AdvisoryLock) UnlockExclusive ¶
func (l AdvisoryLock) UnlockExclusive(ctx context.Context, db *pg.DB) error
UnlockExclusive releases an exclusive advisory lock.
func (AdvisoryLock) UnlockShared ¶
func (l AdvisoryLock) UnlockShared(ctx context.Context, db *pg.DB) error
UnlockShared releases a shared advisory lock.
type CSVStorage ¶ added in v0.5.0
type CSVStorage struct {
// contains filtered or unexported fields
}
func NewCSVStorage ¶ added in v0.5.0
func NewCSVStorage(path string, version model.Version, opts CSVStorageOptions) (*CSVStorage, error)
func NewCSVStorageLatest ¶ added in v0.6.6
func NewCSVStorageLatest(path string, opts CSVStorageOptions) (*CSVStorage, error)
func (*CSVStorage) PersistBatch ¶ added in v0.5.0
func (c *CSVStorage) PersistBatch(ctx context.Context, ps ...model.Persistable) error
PersistBatch persists a batch of models to CSV, creating new files if they don't already exist otherwise appending to existing ones.
func (*CSVStorage) WithMetadata ¶ added in v0.7.5
func (c *CSVStorage) WithMetadata(md Metadata) model.Storage
type CSVStorageOptions ¶ added in v0.7.5
func DefaultCSVStorageOptions ¶ added in v0.7.5
func DefaultCSVStorageOptions() CSVStorageOptions
type Catalog ¶ added in v0.6.0
type Catalog struct {
// contains filtered or unexported fields
}
A Catalog holds a list of pre-configured storage systems and can open them when requested.
func NewCatalog ¶ added in v0.6.0
func NewCatalog(cfg config.StorageConf) (*Catalog, error)
type Database ¶
func NewDatabase ¶
func NewDatabaseFromDB ¶ added in v0.7.0
func (*Database) AsORM ¶ added in v0.7.7
func (d *Database) AsORM() *pg.DB
MUST call Connect before using TODO(frrist): this is lazy, but good enough to MVP
func (*Database) Connect ¶
Connect opens a connection to the database and checks that the schema is compatible with the version required by this version of visor. ErrSchemaTooOld is returned if the database schema is older than the current schema, ErrSchemaTooNew if it is newer.
func (*Database) ExecContext ¶ added in v0.7.0
func (*Database) GetSchemaVersions ¶
GetSchemaVersions returns the schema version in the database and the latest schema version defined by the available migrations.
func (*Database) IsConnected ¶ added in v0.6.0
func (*Database) MigrateSchema ¶
MigrateSchema migrates the database schema to the latest version based on the list of migrations available
func (*Database) MigrateSchemaTo ¶
MigrateSchema migrates the database schema to a specific version. Note that downgrading a schema to an earlier version is destructive and may result in the loss of data.
func (*Database) PersistBatch ¶ added in v0.5.0
PersistBatch persists a batch of persistables in a single transaction
func (*Database) SchemaConfig ¶ added in v0.7.5
type MemStorage ¶ added in v0.5.2
type MemStorage struct { // TODO parallel map? Data map[string][]interface{} DataMu sync.Mutex Version model.Version }
func NewMemStorage ¶ added in v0.5.2
func NewMemStorage(version model.Version) *MemStorage
func NewMemStorageLatest ¶ added in v0.6.6
func NewMemStorageLatest() *MemStorage
func (*MemStorage) PersistBatch ¶ added in v0.5.2
func (j *MemStorage) PersistBatch(ctx context.Context, ps ...model.Persistable) error
func (*MemStorage) PersistModel ¶ added in v0.5.2
func (j *MemStorage) PersistModel(ctx context.Context, m interface{}) error
type Metadata ¶ added in v0.7.5
type Metadata struct {
JobName string // name of the job using the storage
}
Metadata is additional information that a storage may use to annotate the data it writes
type NullStorage ¶ added in v0.5.0
type NullStorage struct { }
A NullStorage ignores any requests to persist a model
func (*NullStorage) PersistBatch ¶ added in v0.5.0
func (*NullStorage) PersistBatch(ctx context.Context, p ...model.Persistable) error
func (*NullStorage) PersistModel ¶ added in v0.5.0
func (*NullStorage) PersistModel(ctx context.Context, m interface{}) error