schema

package
v0.0.0-pre.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGracefulAbort = fmt.Errorf("schema check gracefully aborted")

ErrGracefulAbort is a special error that can be returned by a Check function to force Schema.Ensure to abort gracefully.

Every change performed so by the Check will be committed, although ErrGracefulAbort will be returned.

Functions

func DoesSchemaTableExist

func DoesSchemaTableExist(ctx context.Context, tx *sql.Tx) (bool, error)

DoesSchemaTableExist return whether the schema table is present in the database.

Types

type Check

type Check func(context.Context, int, *sql.Tx) error

Check is a callback that gets fired all the times Schema.Ensure is invoked, before applying any update. It gets passed the version that the schema is currently at and a handle to the transaction. If it returns nil, the update proceeds normally, otherwise it's aborted. If ErrGracefulAbort is returned, the transaction will still be committed, giving chance to this function to perform state changes.

type Hook

type Hook func(context.Context, int, *sql.Tx) error

Hook is a callback that gets fired when a update gets applied.

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema captures the schema of a database in terms of a series of ordered updates.

func NewFromMap

func NewFromMap(versionsToUpdates map[int]Update) *Schema

NewFromMap creates a new schema Schema with the updates specified in the given map. The keys of the map are schema versions that when upgraded will trigger the associated Update value. It's required that the minimum key in the map is 1, and if key N is present then N-1 is present too, with N>1 (i.e. there are no missing versions).

NOTE: the regular New() constructor would be formally enough, but for extra clarity we also support a map that indicates the version explicitly, see also PR #3704.

func (*Schema) Ensure

func (s *Schema) Ensure(db *sql.DB) (int, error)

Ensure makes sure that the actual schema in the given database matches the one defined by our updates.

All updates are applied transactionally. In case any error occurs the transaction will be rolled back and the database will remain unchanged.

A update will be applied only if it hasn't been before (currently applied updates are tracked in the a 'shema' table, which gets automatically created).

If no error occurs, the integer returned by this method is the initial version that the schema has been upgraded from.

func (*Schema) File

func (s *Schema) File(path string)

File extra queries from a file. If the file is exists, all SQL queries in it will be executed transactionally at the very start of Ensure(), before anything else is done.

If a schema hook was set with Hook(), it will be run before running the queries in the file and it will be passed a patch version equals to -1.

func (*Schema) Fresh

func (s *Schema) Fresh(statement string)

Fresh sets a statement that will be used to create the schema from scratch when bootstraping an empty database. It should be a "flattening" of the available updates, generated using the Dump() method. If not given, all patches will be applied in order.

func (*Schema) Hook

func (s *Schema) Hook(hook Hook)

Hook instructs the schema to invoke the given function whenever a update is about to be applied. The function gets passed the update version number and the running transaction, and if it returns an error it will cause the schema transaction to be rolled back. Any previously installed hook will be replaced.

type Update

type Update func(context.Context, *sql.Tx) error

Update applies a specific schema change to a database, and returns an error if anything goes wrong.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL