migrator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 10 Imported by: 0

README

Migrator

Migrator is a golang library to apply migrations on a database.

Why another tools?

Existing tools provide a lot of features but are complex and come with many dependencies. The goal of this tool is to be simple, and without any dependencies.

Usage

Create a migration like this:

-- +migrate Up
CREATE TABLE test_table (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);
package main

import (
    "database.sql"
    "os"

    "github.com/erdnaxeli/migrator"
    _ "modernc.org/sqlite"
)

func main() {
    // Note: in this example errors are not checked, but you should do it
    db, _ := sql.Open("sqlite", "db.sqlite")
    defer db.Close()

    directory := os.DirFS("migrations")
    migrator, _ := migrator.New(db, directory)

    migrator.Migrate()
}

Features

Current features:

  • Apply up migrations.
  • Multiple statements in a migration.
  • Each migration is applied in his own transaction. If one migration fails, nothing is applied and it stops.
  • Support any database compatible with sql.DB.
  • Support any migrations source compatible with fs.FS

No implemented:

  • A CLI.
  • Down migrations.
  • Code migrations.

Documentation

Overview

Package migrator implements database migration functionalities.

Index

Constants

This section is empty.

Variables

View Source
var FilenameRgx = regexp.MustCompile(`^(\d+)_(.*)\.sql$`)

FilenameRgx is the regular expression to match migration filenames.

Functions

This section is empty.

Types

type DuplicateMigrationVersionError

type DuplicateMigrationVersionError struct {
	Version int
}

DuplicateMigrationVersionError is returned when there are multiple migrations with the same version.

func (DuplicateMigrationVersionError) Error

type EmptyMigrationError

type EmptyMigrationError struct {
	Filename string
}

EmptyMigrationError is returned when a migration file is empty.

func (EmptyMigrationError) Error

func (e EmptyMigrationError) Error() string

type InvalidCurrentVersionError

type InvalidCurrentVersionError struct {
	Version int
}

InvalidCurrentVersionError is returned when the current database version does not correspond to any migration.

func (InvalidCurrentVersionError) Error

type InvalidMigrationFileError

type InvalidMigrationFileError struct {
	Filename string
}

InvalidMigrationFileError is returned when a migration file foramt is invalid.

func (InvalidMigrationFileError) Error

type InvalidMigrationFilenameError

type InvalidMigrationFilenameError struct {
	Filename string
}

InvalidMigrationFilenameError is returned when a migration filename does not match the expected pattern.

func (InvalidMigrationFilenameError) Error

type Migration

type Migration struct {
	// contains filtered or unexported fields
}

Migration represents a database migration.

type Migrator

type Migrator interface {
	// Migrate applies all pending database migrations.
	Migrate() error

	// Version returns the current version of the database schema.
	Version() (int, error)
}

Migrator is the interface to manage database migrations.

func New

func New(db *sql.DB, fs fs.FS) (Migrator, error)

New creates a new Migrator instance.

It loads migrations from the provided fs.FS and checks the current database version. If the schema_migrations table does not exist, it creates it.

It can returns the following errors:

  • InvalidMigrationFilenameError
  • InvalidMigrationFileError
  • EmptyMigrationError
  • DuplicateMigrationVersionError
  • MissingMigrationVersionError

type MissingMigrationVersionError

type MissingMigrationVersionError struct {
	Version int
}

MissingMigrationVersionError is returned when a migration version is missing in the sequence.

func (MissingMigrationVersionError) Error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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