Documentation ¶
Overview ¶
Package db establishes a connection with a Raft replicated sqlite3 database. External packages can use this module to ensure that the database is at the most current schema and can make thread-safe transactions against the database.
Users of the package have to call db.Connect() at least once to use the database, but multiple calls to db.Connect() will not cause an error. A call to db.Close() will require reconnecting before any additional queries are made. Arbitrary transactions to the database can be executed by using db.BeginTx - the module guards a single connection to the database from multiple go routines opening and closing access to the database.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotconnected = errors.New("not connected to the database") ErrReadOnly = errors.New("connected in readonly mode") ErrNotFound = errors.New("record not found or no rows returned") ErrCannotParseDSN = errors.New("could not parse dsn, specify scheme:///path/to/data.db") ErrUnknownScheme = errors.New("must specify a sqlite3 DSN") )
Functions ¶
func BeginTx ¶
BeginTx creates a transaction with the connected database but returns an error if the database is not connected. If the database is set to readonly mode and the transaction options are not readonly, an error is returned.
func Close ¶
func Close() (err error)
Close the database safely and allow for reconnect after close by resetting the package variables. No errors occur if the database is not connected.
func Connect ¶
Connect to the sqlite3 database specified by the DSN. Connecting in readonly mode is managed by the package, not the database and is enforced by package functions. Subsequent calls to Connect will be ignored even if a different DSN or readonly mode is passed to the function.
func InitializeSchema ¶
Initialize schema applies any unapplied migrations to the database and should be run when the database is first connected to. If empty is true then the migration table is created and all migrations are applied. If it is not true then the current migration of the database is fetched and all unapplied migrations are applied.
Types ¶
type Migration ¶
type Migration struct { ID int // The unique sequence ID of the migration Name string // The human readable name of the migration Version string // The package version when the migration was applied Created time.Time // The timestamp when the migration was applied Path string // The path of the migration in the filesystem }
Migration is used to represent both a SQL migration from the embedded file system and a migration record in the database. These records are compared to ensure the database is as up to date as possible before the application starts.
func Migrations ¶
Migrations returns the migration files from the embedded file system.