Documentation
¶
Index ¶
- func ClearMigrationRunners()
- func ListMigrationRunners() []string
- func RegisterMigrationRunner(id string, runner MigrationRunner)
- func Rollback(ctx context.Context, db *sql.DB, gobaseChangelog io.Reader, targetID string) error
- func RollbackWithFS(ctx context.Context, db *sql.DB, gobaseChangelog io.Reader, targetID string, ...) error
- func RunMigration(ctx context.Context, db *sql.DB, gobaseChangelog io.Reader) error
- func RunMigrationWithFS(ctx context.Context, db *sql.DB, gobaseChangelog io.Reader, fsys fs.FS) error
- func UnregisterMigrationRunner(id string) bool
- type AppliedChange
- type Change
- type Cond
- type Gobase
- type MigrationError
- type MigrationRunner
- type RollbackSQL
- type RunnerRef
- type SqlFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearMigrationRunners ¶
func ClearMigrationRunners()
ClearMigrationRunners removes all registered migration runners. Useful for testing.
func ListMigrationRunners ¶
func ListMigrationRunners() []string
ListMigrationRunners returns a list of all registered runner IDs.
func RegisterMigrationRunner ¶
func RegisterMigrationRunner(id string, runner MigrationRunner)
RegisterMigrationRunner registers a migration runner with the given ID. This should typically be called during package initialization (init functions). Panics if a runner with the same ID is already registered.
func Rollback ¶
Rollback reverts applied changesets up to (but not including) the target changeset ID If targetID is empty, rolls back all changesets Use RollbackWithFS if your changelog contains <sqlFile> elements with rollback paths
func RollbackWithFS ¶
func RollbackWithFS(ctx context.Context, db *sql.DB, gobaseChangelog io.Reader, targetID string, fsys fs.FS) error
RollbackWithFS reverts applied changesets up to (but not including) the target changeset ID If targetID is empty, rolls back all changesets The fsys parameter provides access to SQL files referenced by <sqlFile rollback="..."> attributes
func RunMigration ¶
RunMigration executes pending database migrations from the changelog Use RunMigrationWithFS if your changelog contains <sqlFile> elements
func RunMigrationWithFS ¶
func RunMigrationWithFS(ctx context.Context, db *sql.DB, gobaseChangelog io.Reader, fsys fs.FS) error
RunMigrationWithFS executes pending database migrations from the changelog The fsys parameter provides access to SQL files referenced by <sqlFile> elements Pass an embed.FS or os.DirFS for the directory containing your SQL files
func UnregisterMigrationRunner ¶
UnregisterMigrationRunner removes a migration runner from the registry. Useful for testing. Returns true if the runner was found and removed.
Types ¶
type AppliedChange ¶
type AppliedChange struct {
ID string
Author string
Checksum string
DateExecuted time.Time
OrderExecuted int
Description string
}
AppliedChange represents a record in the changelog tracking table
type Change ¶
type Change struct {
ID string `xml:"id,attr"`
Author string `xml:"author,attr"`
FailOnError *bool `xml:"failOnError,attr"`
Condition *Cond `xml:"cond"`
SQL string `xml:"sql"`
SqlFile *SqlFile `xml:"sqlFile"`
RunnerRef *RunnerRef `xml:"runMigration"`
Rollback *RollbackSQL `xml:"rollback"`
Comment string `xml:"comment"`
}
Change represents a single changeset
type Cond ¶
type Cond struct {
SQL string `xml:"sql"`
}
Cond represents a precondition for a changeset
type Gobase ¶
type Gobase struct {
XMLName xml.Name `xml:"gobase"`
ChangelogTable string `xml:"changelogtable,attr"`
Changes []Change `xml:"change"`
}
Gobase represents the root element of a changelog file
type MigrationError ¶
MigrationError represents an error during migration with changeset context
func (*MigrationError) Error ¶
func (e *MigrationError) Error() string
func (*MigrationError) Unwrap ¶
func (e *MigrationError) Unwrap() error
type MigrationRunner ¶
type MigrationRunner interface {
// Run executes the migration within the given transaction.
// The transaction is managed by gobase - do not commit or rollback.
Run(ctx context.Context, tx *sql.Tx) error
// Rollback reverts the migration within the given transaction.
// The transaction is managed by gobase - do not commit or rollback.
Rollback(ctx context.Context, tx *sql.Tx) error
}
MigrationRunner defines the interface for programmatic migrations. Implement this interface when SQL alone is insufficient (e.g., data transformations, external API calls, complex business logic).
func GetMigrationRunner ¶
func GetMigrationRunner(id string) (MigrationRunner, error)
GetMigrationRunner retrieves a registered migration runner by ID. Returns an error if the runner is not found.
type RollbackSQL ¶
type RollbackSQL struct {
SQL string `xml:",chardata"`
}
RollbackSQL represents rollback SQL for a changeset