Documentation
¶
Index ¶
- Constants
- func DSN(path string, readOnly bool) string
- func EnsureExists(path string) error
- func InTx(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error
- func IsForeignKeyConstraint(err error) bool
- func IsTriggerConstraint(err error) bool
- func IsUniqueConstraint(err error) bool
- func SchemaVersion(ctx context.Context, p *goose.Provider) (int64, error)
- func Setup(path string) (*sql.DB, error)
- func SetupReadOnly(path string) (*sql.DB, error)
- func WithTx(ctx context.Context, db *sql.DB, fn func(q *dbq.Queries) error) error
- type Schema
- func (s *Schema) CurrentVersion(ctx context.Context, db *sql.DB) (int64, error)
- func (s *Schema) LatestVersion() (int64, error)
- func (s *Schema) Migrate(ctx context.Context, db *sql.DB) error
- func (s *Schema) Open(ctx context.Context, path string) (*sql.DB, error)
- func (s *Schema) Provider(db *sql.DB) (*goose.Provider, error)
- type SqliteStore
Constants ¶
const DB_PRAGMAS = "?_foreign_keys=ON&_journal_mode=WAL&_synchronous=NORMAL"
DB_PRAGMAS are the connection parameters for a read-write database: foreign keys enforced, write-ahead logging and synchronous NORMAL. The driver runs each of them as a PRAGMA when a connection is opened.
const DB_PRAGMAS_READONLY = "?_foreign_keys=ON&mode=ro"
DB_PRAGMAS_READONLY are the connection parameters for a read-only database. There is deliberately no journal mode: a read-only connection can't change it, and asking for WAL on a database that is stored in rollback-journal mode fails with "attempt to write a readonly database". That is exactly how the databases inside an export are stored, since VACUUM INTO writes them that way.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
DSN returns the connection string for the SQLite database at path, which every database open in modctl should use rather than building its own.
It is a file: URI with an escaped path. With a plain path the driver cuts the query string off before SQLite sees it, which silently drops mode=ro (the connection stays writable), and a path that contains a '?' is cut short at it. The read-only variant is described at DB_PRAGMAS_READONLY.
func EnsureExists ¶
EnsureExists verifies that the database file at path exists and is a regular file. If not, it returns a user-friendly error.
func InTx ¶
InTx runs fn in a transaction on db. It is committed if fn returns nil, and rolled back if fn returns an error (which is returned as it is) or panics (as it goes up). A transaction that can't be committed or can't be started is an error that says so ("commit: ..." and "begin transaction: ..."), and in the second case fn is not called.
fn must not use db for what has to be in the transaction: the transaction is its own connection, and what is done through db is not part of it.
func IsForeignKeyConstraint ¶
IsForeignKeyConstraint reports whether err is SQLite complaining that a FOREIGN KEY constraint would have been violated: a row that refers to something that isn't there, or the deletion of something that is still referred to.
func IsTriggerConstraint ¶
IsTriggerConstraint reports whether err is a trigger of ours stopping the statement with RAISE(ABORT, ...) and the like.
func IsUniqueConstraint ¶
IsUniqueConstraint reports whether err is SQLite complaining that a UNIQUE constraint (or unique index) would have been violated. A PRIMARY KEY violation is a different error and is not matched.
func SchemaVersion ¶
SchemaVersion returns the applied migration version as seen by p. It is split out from Schema.CurrentVersion so that a test can have a provider that is controlled further than a Schema is (how far it has migrated, say).
func Setup ¶
Setup opens the read-write database at path, with the migrations not applied (see Migrate and Open). Nothing is done to the file until the first statement is: the driver is lazy, so a path that is not there is no error here.
func SetupReadOnly ¶
SetupReadOnly is Setup for a database that is only read: the connection can't change it, and it is not created by looking for it.
Types ¶
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
A Schema is the schema of the database: the migrations that make it. They are in a directory called "migrations" of the file system that the Schema is made with, which in the binary is the ones that are embedded in it (main has them, since they are not in a directory that this package can embed them from).
What works on a database is given the Schema that it brings the database to, and does not find one: a test has the Schema of the repository, or one of its own, and none of them is anything but what it is given.
func NewSchema ¶
NewSchema is the Schema that is made by the migrations in the "migrations" directory of root. A root that is nil has no migrations: every use of the Schema is an error that says so (and so is every use of a Schema that is nil).
func (*Schema) CurrentVersion ¶
CurrentVersion returns the version of the latest migration that has been applied to db. This is what the database itself is at, which is not necessarily the latest migration of the Schema: a new database reports 0 and one that has not been migrated since an upgrade reports its older version.
func (*Schema) LatestVersion ¶
LatestVersion returns the version of the latest migration of the Schema: the highest version among its migrations. It does not look at any database, so it is the version a database ends up at once it has been fully migrated, and what a bundle's schema version has to be compared against to tell whether this build can import it.
type SqliteStore ¶
A custom goose store to let us override the schema migrations table to use more idiomatic sqlite