Documentation
¶
Overview ¶
Package gomigrate is a simple migration tool for Go projects. It reads SQL migrations from files and executes them in a transaction. It also keeps track of the executed migrations in a table.
Usage:
package main import ( "context" "database/sql" "embed" "log" "log/slog" "time" _ "modernc.org/sqlite" "github.com/topi314/gomigrate" "github.com/topi314/gomigrate/drivers/sqlite" ) //go:embed migrations/*.sql var migrations embed.FS func main() { // open database db, err := sql.Open("sqlite", "database.db") if err != nil { log.Fatal(err) } defer db.Close() // create context with timeout ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() // run migrations if err = gomigrate.Migrate(ctx, db, sqlite.New, migrations, gomigrate.WithDirectory("migrations"), // set directory for migrations gomigrate.WithTableName("gomigrate"), // set custom table name for migrations gomigrate.WithLogger(slog.Default()), // set custom logger ); err != nil { log.Fatal(err) } // run your code }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoDatabase is returned when no database is provided. ErrNoDatabase = errors.New("no database provided") // ErrNoDriver is returned when no driver is provided. ErrNoDriver = errors.New("no driver provided") )
Functions ¶
func Migrate ¶
func Migrate(ctx context.Context, db Queryer, newDriver NewDriver, disk fs.FS, opts ...Option) error
Migrate reads and executes SQL migrations from the embed.FS to bring the database schema up to date. It keeps track of the executed migrations in a table. If the database schema is ahead of the migrations, it will return an error. Each migration runs in a transaction. If the context is canceled, the transaction for the current migration will be rolled back, and it will return an error.
Types ¶
type Driver ¶
type Driver interface { // Name returns the name of the driver. Name() string // CreateVersionTable creates the versioning table if it does not exist. CreateVersionTable(ctx context.Context) error // GetVersion returns the most recent schema version. GetVersion(ctx context.Context) (int, error) // AddVersion adds a new schema version to the versioning table. AddVersion(ctx context.Context, tx *sql.Tx, version int) error }
Driver allows gomigrate to work with different databases such as SQLite and PostgreSQL.
type MigrateError ¶
MigrateError holds additional information about an error that occurred during migration.
func (*MigrateError) Unwrap ¶
func (e *MigrateError) Unwrap() error
Unwrap returns the underlying error.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option can be used to configure the migrator.
func WithDirectory ¶
WithDirectory sets the directory where the migration files are loaded from.
func WithLogger ¶
WithLogger sets the logger to be used by the migrator.
func WithTableName ¶
WithTableName sets the name of the table where the schema version is stored.
func WithoutDirectory ¶
func WithoutDirectory() Option
WithoutDirectory removed the default directory where the migration files are loaded from.
type Queryer ¶
type Queryer interface { // QueryContext executes a query that returns rows, typically a SELECT. // The args are for any placeholder parameters in the query. QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) // ExecContext executes a query without returning any rows. // The args are for any placeholder parameters in the query. ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) // BeginTx starts a transaction. // // The provided context is used until the transaction is committed or rolled back. // If the context is canceled, the sql package will roll back // the transaction. [Tx.Commit] will return an error if the context provided to // BeginTx is canceled. // // The provided [sql.TxOptions] is optional and may be nil if defaults should be used. // If a non-default isolation level is used that the driver doesn't support, // an error will be returned. BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) }
Queryer is used to execute SQL queries and transactions.