Documentation
¶
Overview ¶
Package lifecycle is designed to simplify the setup of simple SQL-focused sqlite3 database projects.
A common thing you want to do when handed a sqlite file is 1) Determine if it's already initialized, or needs the tables created 2) Determine if it's the correct schema version 3) Migrate to the newest schema version (if required). The aim of this library is to do the grunt work for that.
Index ¶
- Constants
- Variables
- type Config
- func (cfg *Config) Check() error
- func (cfg *Config) GetParam(eq sqlx.Ext, key string) (string, error)
- func (cfg *Config) Open(dbx *sqlx.DB) error
- func (cfg *Config) OpenTx(eq sqlx.Ext) error
- func (cfg *Config) SetAutoMigrate(autoMigrate bool) *Config
- func (cfg *Config) SetMigrationRecipe(vfrom int, recipe Recipe) *Config
- func (cfg *Config) SetParam(eq sqlx.Ext, param string, value string) error
- func (cfg *Config) SetParamTableName(name string) *Config
- func (cfg *Config) SetSchemaName(schemaName string) *Config
- type LifecycleError
- type Recipe
Constants ¶
const ( PARAMS_KEY_DBVERSION = "dbversion" PARAMS_KEY_DBSCHEMANAME = "dbschemaname" )
Variables ¶
var ( ErrorNeedsUpgrade = LifecycleError{/* contains filtered or unexported fields */} ErrorInvalidMigrationVersion = LifecycleError{/* contains filtered or unexported fields */} ErrorInternal = LifecycleError{/* contains filtered or unexported fields */} ErrorNoMigrationRecipe = LifecycleError{/* contains filtered or unexported fields */} ErrorMigrationFailed = LifecycleError{/* contains filtered or unexported fields */} ErrorSchemaNameMismatch = LifecycleError{/* contains filtered or unexported fields */} ErrDbTooNew = LifecycleError{/* contains filtered or unexported fields */} )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is the configuration for database initialization and migration. It should be created with the Schema() function.
Note that some error checking is done as the configuration is assembled; but those errors cannot be returned immediately; they will only be returned when Config.Open() or Config.Check() are called.
If an error happens in any of the directives, the rest of the directives are skipped.
func Schema ¶
Schema creates a basic configuration: The current database version, and the current schema to create the database.
func (*Config) GetParam ¶
GetParam gets the value of the specified key. If the key doesn't exist, sql.ErrNoRows will be returned (perhaps wrapped).
func (*Config) Open ¶
Open will check whether the database has been initialized, and if so, what version it is. If it cannot read the version, it will attempt to initialize the database with the newest schema version.
func (*Config) OpenTx ¶
OpenTx will do the same thing as open, but with an sqlx.Tx instead. (Alternately, if you want to do the open operation without a transaction, you can call this with the raw database, but that's not recommended.)
func (*Config) SetAutoMigrate ¶
SetAutoMigrate configures whether database schema migration will happen automatically or not. If false, an error will be returned if an old version of the database is opened.
Default: false
func (*Config) SetMigrationRecipe ¶
SetMigrationRecipe configures how to upgrade from version vfrom to vfrom+1. vfrom must be strictly less than the swDbVersion passed to Schema().
func (*Config) SetParam ¶
SetParam sets the key to the specified value (updating they key to the new value if it already exists). It can be passed either a transaction or a plain database.
func (*Config) SetParamTableName ¶
SetParamTableName configures the name of the parameter table which contains the database version. This can be used for other purposes, as long as the key "dbversion" is not used. NB that this table name CANNOT BE MIGRATED, so choose it wisely.
Default: "params"
func (*Config) SetSchemaName ¶
SetSchemaName sets the database schema name. It is recommended this be of the form <packagepath>#<schemaname>; for example, "gitlab.com/martyros/languagedb#words". If set, opened databases will check that it is equivalent when opening; if not set, opened databases will verify that no such key exists.
type LifecycleError ¶
type LifecycleError struct {
// contains filtered or unexported fields
}
func (LifecycleError) Is ¶
func (e LifecycleError) Is(target error) bool
func (LifecycleError) Unwrap ¶
func (e LifecycleError) Unwrap() error
type Recipe ¶
Recipe contains a recipe for doing something with the database; for instance, initializing an empty database, or migrating from version N of a database to version N+1. Sql is sql which is executed (as part of the transaction). Function is a function called with the transaction handle.
If both are present, Sql is executed first, then Function called.
If either one results in an error, the migration will stop and the entire transaction will be rolled back.