dbmate

package
v2.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 14 Imported by: 18

Documentation

Index

Constants

View Source
const Version = "2.15.0"

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'")
	ErrParseMissingDown    = errors.New("dbmate requires each migration to define a down block with '-- migrate:down'")
	ErrParseWrongOrder     = errors.New("dbmate requires '-- migrate:up' to appear before '-- migrate:down'")
	ErrParseUnexpectedStmt = errors.New("dbmate does not support statements preceding the '-- migrate:up' block")
)

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 generates schema.sql after each action
	AutoDumpSchema bool
	// DatabaseURL is the database connection string
	DatabaseURL *url.URL
	// FS specifies the filesystem, or nil for OS filesystem
	FS fs.FS
	// Log is the interface to write stdout
	Log io.Writer
	// MigrationsDir specifies the directory or directories to find migration files
	MigrationsDir []string
	// MigrationsTableName specifies the database table to record migrations in
	MigrationsTableName string
	// SchemaFile specifies the location for schema.sql file
	SchemaFile string
	// Fail if migrations would be applied out of order
	Strict bool
	// Verbose prints the result of each statement execution
	Verbose bool
	// WaitBefore will wait for database to become available before running any actions
	WaitBefore bool
	// WaitInterval specifies length of time between connection attempts
	WaitInterval time.Duration
	// WaitTimeout specifies maximum time for connection attempts
	WaitTimeout time.Duration
}

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) 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) Driver

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

Driver initializes the appropriate database driver

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) FindMigrations

func (db *DB) FindMigrations() ([]Migration, error)

FindMigrations lists all available migrations

func (*DB) LoadSchema added in v2.9.0

func (db *DB) LoadSchema() error

LoadSchema loads schema file to the current database

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

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

Status shows the status of all migrations

func (*DB) Wait

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
	QueryError(string, error) error
}

Driver provides top level database functions

type DriverConfig

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

DriverConfig holds configuration passed to driver constructors

type DriverFunc

type DriverFunc func(DriverConfig) Driver

DriverFunc represents a driver constructor

type Migration

type Migration struct {
	Applied  bool
	FileName string
	FilePath string
	FS       fs.FS
	Version  string
}

Migration represents an available migration and status

func (*Migration) Parse

func (m *Migration) Parse() (*ParsedMigration, error)

Parse a migration

type ParsedMigration

type ParsedMigration struct {
	Up          string
	UpOptions   ParsedMigrationOptions
	Down        string
	DownOptions ParsedMigrationOptions
}

ParsedMigration contains the migration contents and options

type ParsedMigrationOptions

type ParsedMigrationOptions interface {
	Transaction() bool
}

ParsedMigrationOptions is an interface for accessing migration options

type QueryError added in v2.8.0

type QueryError struct {
	Err      error
	Query    string
	Position int
}

func (*QueryError) Error added in v2.8.0

func (e *QueryError) Error() string

type StatusResult

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