schema

package
v0.0.0-...-8dc4a41 Latest Latest
Warning

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

Go to latest
Published: May 25, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package schema offers utilities to create and maintain a database schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook func(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 Empty

func Empty() *Schema

Empty creates a new schema with no updates.

func New

func New(updates []Update) *Schema

New creates a new schema Schema with the given 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 explicitely,
see also PR #3704.

func (*Schema) Add

func (s *Schema) Add(update Update)

Add a new update to the schema. It will be appended at the end of the existing series.

func (*Schema) Dump

func (s *Schema) Dump(db *sql.DB) (string, error)

Dump returns a text of SQL commands that can be used to create this schema from scratch in one go, without going thorugh individual patches (essentially flattening them).

It requires that all patches in this schema have been applied, otherwise an error will be returned.

func (*Schema) Ensure

func (s *Schema) Ensure(db *sql.DB) 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).

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(*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