Documentation ¶
Overview ¶
Package schema provides tools to manage database schema changes ("migrations") as embedded functionality inside another application which is using a database/sql
Basic usage instructions involve creating a schema.Migrator via the schema.NewMigrator() function, and then passing your *sql.DB to its .Apply() method.
See the package's README.md file for more usage instructions.
Index ¶
- Constants
- Variables
- func MigrationIDFromFilename(filename string) string
- func SortMigrations(migrations []*Migration)
- type AppliedMigration
- type Connection
- type DB
- type Dialect
- type File
- type Locker
- type Logger
- type Migration
- func FSMigrations(filesystem fs.FS, glob string) (migrations []*Migration, err error)
- func MigrationFromFile(file File) (migration *Migration, err error)
- func MigrationFromFilePath(filename string) (migration *Migration, err error)
- func MigrationsFromDirectoryPath(dirPath string) (migrations []*Migration, err error)
- type Migrator
- type Option
- type Queryer
- type Transactor
Constants ¶
const DefaultTableName = "schema_migrations"
DefaultTableName defines the name of the database table which will hold the status of applied migrations
Variables ¶
var ErrNilDB = errors.New("DB pointer is nil")
ErrNilDB is thrown when the database pointer is nil
var MSSQL = mssqlDialect{}
MSSQL is the dialect for MS SQL-compatible databases
var MySQL = mysqlDialect{}
MySQL is the dialect which should be used for MySQL/MariaDB databases
var Postgres = postgresDialect{}
Postgres is the dialect for Postgres-compatible databases
var SQLite = &sqliteDialect{}
SQLite is the dialect for sqlite3 databases
Functions ¶
func MigrationIDFromFilename ¶
MigrationIDFromFilename removes directory paths and extensions from the filename to make a friendlier Migration ID
func SortMigrations ¶
func SortMigrations(migrations []*Migration)
SortMigrations sorts a slice of migrations by their IDs
Types ¶
type AppliedMigration ¶
type AppliedMigration struct { Migration // Checksum is the MD5 hash of the Script for this migration Checksum string // ExecutionTimeInMillis is populated after the migration is run, indicating // how much time elapsed while the Script was executing. ExecutionTimeInMillis int64 // AppliedAt is the time at which this particular migration's Script began // executing (not when it completed executing). AppliedAt time.Time }
AppliedMigration represents a successfully-executed migration. It embeds Migration, and adds fields for execution results. This type is what records persisted in the schema_migrations table align with.
type Connection ¶ added in v1.2.0
type Connection interface { Transactor Queryer }
Connection defines the interface for a *sql.Conn, which can both start a new transaction and run queries.
type DB ¶ added in v1.2.0
DB defines the interface for a *sql.DB, which can be used to get a concrete connection to the database.
type Dialect ¶
type Dialect interface { QuotedTableName(schemaName, tableName string) string CreateMigrationsTable(ctx context.Context, tx Queryer, tableName string) error GetAppliedMigrations(ctx context.Context, tx Queryer, tableName string) (applied []*AppliedMigration, err error) InsertAppliedMigration(ctx context.Context, tx Queryer, tableName string, migration *AppliedMigration) error }
Dialect defines the minimal interface for a database dialect. All dialects must implement functions to create the migrations table, get all applied migrations, insert a new migration tracking record, and perform escaping for the tracking table's name
type Locker ¶ added in v1.2.0
type Locker interface { Lock(ctx context.Context, tx Queryer, tableName string) error Unlock(ctx context.Context, tx Queryer, tableName string) error }
Locker defines an optional Dialect extension for obtaining and releasing a global database lock during the running of migrations. This feature is supported by PostgreSQL and MySQL, but not SQLite.
type Logger ¶ added in v1.1.9
type Logger interface {
Print(...interface{})
}
Logger is the interface for logging operations of the logger. By default the migrator operates silently. Providing a Logger enables output of the migrator's operations.
type Migration ¶
Migration is a yet-to-be-run change to the schema. This is the type which is provided to Migrator.Apply to request a schema change.
func FSMigrations ¶ added in v1.2.0
FSMigrations receives a filesystem (such as an embed.FS) and extracts all files matching the provided glob as Migrations, with the filename (without extension) being the ID and the file's contents being the Script.
Example usage:
FSMigrations(embeddedFS, "my-migrations/*.sql")
func MigrationFromFile ¶
MigrationFromFile builds a migration by reading from an open File-like object. The migration's ID will be based on the file's name. The file will *not* be closed after being read.
func MigrationFromFilePath ¶
MigrationFromFilePath creates a Migration from a path on disk
func MigrationsFromDirectoryPath ¶
MigrationsFromDirectoryPath retrieves a slice of Migrations from the contents of the directory. Only .sql files are read
type Migrator ¶
type Migrator struct { SchemaName string TableName string Dialect Dialect Logger Logger // contains filtered or unexported fields }
Migrator is an instance customized to perform migrations on a particular database against a particular tracking table and with a particular dialect defined.
func NewMigrator ¶
NewMigrator creates a new Migrator with the supplied options
func (*Migrator) Apply ¶
Apply takes a slice of Migrations and applies any which have not yet been applied against the provided database. Apply can be re-called sequentially with the same Migrations and different databases, but it is not threadsafe... if concurrent applies are desired, multiple Migrators should be used.
func (Migrator) GetAppliedMigrations ¶
func (m Migrator) GetAppliedMigrations(db Queryer) (applied map[string]*AppliedMigration, err error)
GetAppliedMigrations retrieves all already-applied migrations in a map keyed by the migration IDs
func (*Migrator) QuotedTableName ¶
QuotedTableName returns the dialect-quoted fully-qualified name for the migrations tracking table
type Option ¶
Option supports option chaining when creating a Migrator. An Option is a function which takes a Migrator and returns a Migrator with an Option modified.
func WithContext ¶ added in v1.2.0
WithContext is an Option which sets the Migrator to run within the provided Context
func WithDialect ¶
WithDialect builds an Option which will set the supplied dialect on a Migrator. Usage: NewMigrator(WithDialect(MySQL))
func WithLogger ¶ added in v1.1.9
WithLogger builds an Option which will set the supplied Logger on a Migrator. Usage: NewMigrator(WithLogger(logrus.New()))
func WithTableName ¶
WithTableName is an option which customizes the name of the schema_migrations tracking table. It can be called with either 1 or 2 string arguments. If called with 2 arguments, the first argument is assumed to be a schema qualifier (for example, WithTableName("public", "schema_migrations") would assign the table named "schema_migrations" in the the default "public" schema for Postgres)