dbmate

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 12 Imported by: 10

Documentation

Index

Constants

View Source
const DefaultMigrationsDir = "./db/migrations"

DefaultMigrationsDir specifies default directory to find migration files

View Source
const DefaultMigrationsTableName = "schema_migrations"

DefaultMigrationsTableName specifies default database tables to record migraitons in

View Source
const DefaultSchemaFile = "./db/schema.sql"

DefaultSchemaFile specifies default location for schema.sql

View Source
const DefaultWaitInterval = time.Second

DefaultWaitInterval specifies length of time between connection attempts

View Source
const DefaultWaitTimeout = 60 * time.Second

DefaultWaitTimeout specifies maximum time for connection attempts

View Source
const Version = "1.16.2"

Version of dbmate

Variables

View Source
var (
	ErrNoMigrationFiles      = errors.New("no migration files found")
	ErrInvalidURL            = errors.New("invalid url, have you set your --url flag or DATABASE_URL environment variable?")
	ErrNoRollback            = errors.New("can't rollback: no migrations have been applied")
	ErrCantConnect           = errors.New("unable to connect to database")
	ErrUnsupportedDriver     = errors.New("unsupported driver")
	ErrNoMigrationName       = errors.New("please specify a name for the new migration")
	ErrMigrationAlreadyExist = errors.New("file already exists")
	ErrMigrationDirNotFound  = errors.New("could not find migrations directory")
	ErrMigrationNotFound     = errors.New("can't find migration file")
	ErrCreateDirectory       = errors.New("unable to create directory")
)

Error codes

View Source
var (
	ErrParseMissingUp      = errors.New("dbmate requires each migration to define an up block with '-- migrate:up'")
	ErrParseUnexpectedStmt = errors.New("dbmate does not support statements defined outside of the '-- migrate:up' or '-- migrate:down' blocks")
)

Error codes

Functions

func RegisterDriver

func RegisterDriver(f DriverFunc, scheme string)

RegisterDriver registers a driver constructor for a given URL scheme

Types

type DB

type DB struct {
	AutoDumpSchema      bool
	DatabaseURL         *url.URL
	MigrationsDir       string
	MigrationsTableName string
	SchemaFile          string
	Verbose             bool
	WaitBefore          bool
	WaitInterval        time.Duration
	WaitTimeout         time.Duration
	Log                 io.Writer
}

DB allows dbmate actions to be performed on a specified database

func New

func New(databaseURL *url.URL) *DB

New initializes a new dbmate database

func (*DB) CheckMigrationsStatus added in v1.11.0

func (db *DB) CheckMigrationsStatus(drv Driver) ([]StatusResult, error)

CheckMigrationsStatus returns the status of all available mgirations

func (*DB) Create

func (db *DB) Create() error

Create creates the current database

func (*DB) CreateAndMigrate

func (db *DB) CreateAndMigrate() error

CreateAndMigrate creates the database (if necessary) and runs migrations

func (*DB) Drop

func (db *DB) Drop() error

Drop drops the current database (if it exists)

func (*DB) DumpSchema

func (db *DB) DumpSchema() error

DumpSchema writes the current database schema to a file

func (*DB) GetDriver

func (db *DB) GetDriver() (Driver, error)

GetDriver initializes the appropriate database driver

func (*DB) Migrate

func (db *DB) Migrate() error

Migrate migrates database to the latest version

func (*DB) NewMigration

func (db *DB) NewMigration(name string) error

NewMigration creates a new migration file

func (*DB) Rollback

func (db *DB) Rollback() error

Rollback rolls back the most recent migration

func (*DB) Status added in v1.8.0

func (db *DB) Status(quiet bool) (int, error)

Status shows the status of all migrations

func (*DB) Wait added in v1.4.0

func (db *DB) Wait() error

Wait blocks until the database server is available. It does not verify that the specified database exists, only that the host is ready to accept connections.

type Driver

type Driver interface {
	Open() (*sql.DB, error)
	DatabaseExists() (bool, error)
	CreateDatabase() error
	DropDatabase() error
	DumpSchema(*sql.DB) ([]byte, error)
	MigrationsTableExists(*sql.DB) (bool, error)
	CreateMigrationsTable(*sql.DB) error
	SelectMigrations(*sql.DB, int) (map[string]bool, error)
	InsertMigration(dbutil.Transaction, string) error
	DeleteMigration(dbutil.Transaction, string) error
	Ping() error
}

Driver provides top level database functions

type DriverConfig added in v1.11.0

type DriverConfig struct {
	DatabaseURL         *url.URL
	MigrationsTableName string
	Log                 io.Writer
}

DriverConfig holds configuration passed to driver constructors

type DriverFunc added in v1.11.0

type DriverFunc func(DriverConfig) Driver

DriverFunc represents a driver constructor

type Migration added in v1.5.0

type Migration struct {
	Contents string
	Options  MigrationOptions
}

Migration contains the migration contents and options

func NewMigration added in v1.5.0

func NewMigration() Migration

NewMigration constructs a Migration object

type MigrationOptions added in v1.5.0

type MigrationOptions interface {
	Transaction() bool
}

MigrationOptions is an interface for accessing migration options

type StatusResult added in v1.11.0

type StatusResult struct {
	Filename string
	Applied  bool
}

StatusResult represents an available migration status

Jump to

Keyboard shortcuts

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