Documentation
¶
Overview ¶
Package xmigrator executes ordered, transactional SQL migrations using separately installed drivers.
Index ¶
- Variables
- func ValidateSQL(s Script, dialect string) error
- type Action
- type ChecksumPolicy
- type CreateOptions
- type CreatedFiles
- type Direction
- type DownDefinition
- type DownKind
- type Driver
- type Hooks
- type Issue
- type Logger
- type Migration
- type MigrationError
- type MigrationStatus
- type Migrator
- func (m *Migrator[T]) Down(ctx context.Context, steps int) (Result, error)
- func (m *Migrator[T]) DownAll(ctx context.Context) (Result, error)
- func (m *Migrator[T]) Drop(ctx context.Context) error
- func (m *Migrator[T]) PlanRepairDown(ctx context.Context, v Version) (RepairPlan, error)
- func (m *Migrator[T]) RepairDown(ctx context.Context, v Version) (out RepairResult, err error)
- func (m *Migrator[T]) Status(ctx context.Context) (Status, error)
- func (m *Migrator[T]) Up(ctx context.Context, steps int) (Result, error)
- type Option
- type Record
- type RepairPlan
- type RepairResult
- type Result
- type Script
- type Session
- type Source
- type SourceError
- type SourceOption
- type Status
- type Transaction
- type TxOutcome
- type UnknownAppliedPolicy
- type ValidationReport
- type Version
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidConfig = errors.New("xmigrator: invalid configuration") ErrInvalidSource = errors.New("xmigrator: invalid source") ErrInvalidSteps = errors.New("xmigrator: invalid steps") ErrVersionExists = errors.New("xmigrator: version exists") ErrUnknownApplied = errors.New("xmigrator: unknown applied migration") ErrChecksumMismatch = errors.New("xmigrator: checksum mismatch") ErrDefinitionMismatch = errors.New("xmigrator: definition mismatch") ErrHookMismatch = errors.New("xmigrator: hook mismatch") ErrIrreversible = errors.New("xmigrator: irreversible migration") ErrMetadataConflict = errors.New("xmigrator: metadata conflict") ErrMetadataVersion = errors.New("xmigrator: unsupported metadata version") ErrHistoryConflict = errors.New("xmigrator: history precondition failed") ErrNotApplied = errors.New("xmigrator: migration not applied") ErrUnsafeSQL = errors.New("xmigrator: unsafe SQL") ErrCommitOutcomeUnknown = errors.New("xmigrator: commit outcome unknown") ErrDropScope = errors.New("xmigrator: invalid drop scope") ErrForeignKey = errors.New("xmigrator: foreign key violation") )
Functions ¶
func ValidateSQL ¶
ValidateSQL enforces driver transaction ownership using dialect-aware statement boundaries.
Types ¶
type ChecksumPolicy ¶
type ChecksumPolicy uint8
const ( ChecksumStrict ChecksumPolicy = iota ChecksumWarn ChecksumDisabled )
type CreateOptions ¶
type CreatedFiles ¶
func Create ¶
func Create(ctx context.Context, o CreateOptions) (CreatedFiles, error)
Create writes an exclusive up/down pair. A process crash may leave an incomplete pair; validation rejects it. Only files created by this call are removed on failure.
type DownDefinition ¶
type Driver ¶
type Driver[T any] interface { WithSession(context.Context, func(Session[T]) error) error ReadHistorySnapshot(context.Context) ([]Record, error) ValidateScript(Script) error }
Driver owns connection lifetimes and a lock covering the complete callback.
type Logger ¶
type Logger interface {
Debugw(string, ...any)
Infow(string, ...any)
Warnw(string, ...any)
Errorw(string, ...any)
}
func NewSlogLogger ¶
type MigrationError ¶
type MigrationError struct {
Version Version
Name string
Direction Direction
Stage string
Err error
}
MigrationError identifies a failed migration while preserving its original cause.
func (*MigrationError) Error ¶
func (e *MigrationError) Error() string
func (*MigrationError) Unwrap ¶
func (e *MigrationError) Unwrap() error
type MigrationStatus ¶
type Migrator ¶
type Migrator[T any] struct { // contains filtered or unexported fields }
func (*Migrator[T]) PlanRepairDown ¶
func (*Migrator[T]) RepairDown ¶
type Option ¶
func WithChecksumPolicy ¶
func WithChecksumPolicy[T any](p ChecksumPolicy) Option[T]
func WithLogger ¶
func WithUnknownAppliedPolicy ¶
func WithUnknownAppliedPolicy[T any](p UnknownAppliedPolicy) Option[T]
type Record ¶
type Record struct {
Version Version
Name string
UpChecksum [32]byte
DownChecksum [32]byte
DownKind DownKind
UpHookRevision string
DownHookRevision string
AppliedAt time.Time
ApplyOrder int64
}
func (Record) DownDefinition ¶
func (r Record) DownDefinition() DownDefinition
type RepairPlan ¶
type RepairPlan struct {
Version Version
WouldChange bool
Before, After DownDefinition
Issues []Issue
}
type RepairResult ¶
type RepairResult struct {
Version Version
Confirmed, Changed bool
Before, After DownDefinition
Issues []Issue
}
type Script ¶
type Script struct {
SQL string
Checksum [32]byte
Kind DownKind
HookRevision string
ForeignKeys *bool
}
Script is a normalized snapshot. SQL is executed verbatim, never split at semicolons.
func (Script) DownDefinition ¶
func (s Script) DownDefinition() DownDefinition
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
func NewSource ¶
Example ¶
package main
import (
"testing/fstest"
x "github.com/sxwebdev/xmigrator"
)
func main() {
s, _ := x.NewSource(fstest.MapFS{"1_users.up.sql": {Data: []byte("CREATE TABLE users(id INTEGER);")}, "1_users.down.sql": {Data: []byte("DROP TABLE users;")}}, ".")
_ = s
}
Output:
type SourceError ¶
SourceError identifies the migration file that failed validation.
func (*SourceError) Error ¶
func (e *SourceError) Error() string
func (*SourceError) Unwrap ¶
func (e *SourceError) Unwrap() error
type SourceOption ¶
func WithDialect ¶
func WithDialect(dialect string) SourceOption
WithDialect explicitly selects lexical rules for source parsing. An unspecified source dialect falls back to pgx in Snapshot and Validate; built-in runners infer the driver dialect, while the CLI adapter requires an explicit dialect for validation.
type Status ¶
type Status struct {
Migrations []MigrationStatus
Issues []Issue
}
type Transaction ¶
type UnknownAppliedPolicy ¶
type UnknownAppliedPolicy uint8
const ( UnknownAppliedAllow UnknownAppliedPolicy = iota UnknownAppliedError )
type ValidationReport ¶
type ValidationReport struct{ Migrations int }
Directories
¶
| Path | Synopsis |
|---|---|
|
cli
|
|
|
urfavecli
module
|
|
|
driver
|
|
|
pgx
module
|
|
|
internal
|
|
|
contracttest
Package contracttest exercises observable migration behavior against every backend.
|
Package contracttest exercises observable migration behavior against every backend. |
|
metadata
Package metadata defines the shared, versioned history contract.
|
Package metadata defines the shared, versioned history contract. |
|
sqltext
Package sqltext scans SQL without altering statement boundaries or quoted bodies.
|
Package sqltext scans SQL without altering statement boundaries or quoted bodies. |