Documentation
¶
Index ¶
- func ValidateMigrationSignature(signature string, format SignatureFormat) error
- func ValidateTableName(name string) error
- type BaseMigration
- type MigrationInterface
- type MigrationStatus
- type MigrationStatusResponse
- type MigrationTracker
- type Migrator
- func (s *Migrator) AddMigration(migration MigrationInterface) error
- func (s *Migrator) AddMigrations(migrations []MigrationInterface) error
- func (s *Migrator) Down(ctx context.Context) error
- func (s *Migrator) Fresh(ctx context.Context) error
- func (s *Migrator) Reset(ctx context.Context) error
- func (s *Migrator) RollbackSteps(ctx context.Context, steps int) error
- func (s *Migrator) RollbackToBatch(ctx context.Context, batch int) error
- func (s *Migrator) SetLexicographicalOrdering(enabled bool)
- func (s *Migrator) SetSignatureValidation(enabled bool, format SignatureFormat)
- func (s *Migrator) SetTableName(name string) error
- func (s *Migrator) SetTransactionIsolationLevel(level string)
- func (s *Migrator) SetTransactionsEnabled(enabled bool)
- func (s *Migrator) Status() ([]MigrationStatusResponse, error)
- func (s *Migrator) Up(ctx context.Context) error
- type MigratorInterface
- type Options
- type SignatureFormat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateMigrationSignature ¶
func ValidateMigrationSignature(signature string, format SignatureFormat) error
ValidateMigrationSignature validates that a migration signature follows the specified format.
Supported formats:
- YYYY_MM_DD_HHMM_description (for datetime format)
- YYYY_MM_DD_NNN_description (for date format)
- unix_timestamp_description (for unix format)
- custom (no prefix format restriction, only length and non-empty)
Business Logic:
- Enforces maximum length of 255 characters
- Rejects empty signatures
- For "custom" format: only validates length and non-empty
- For other formats: requires at least 5 underscore-separated parts (or 2 for unix)
- Validates date part (YYYY_MM_DD) is a valid calendar date
- Validates time part (HHMM) or sequence part (NNN) based on format
- Validates description exists and is within length limits
func ValidateTableName ¶
ValidateTableName ensures the table name contains only safe characters. Exported to allow external validation of table names before creating a migrator instance.
Types ¶
type BaseMigration ¶
type BaseMigration struct {
// contains filtered or unexported fields
}
BaseMigration provides common functionality for all migrations.
func (*BaseMigration) Description ¶
func (b *BaseMigration) Description() string
Description Get a human-readable description of what this migration does.
func (*BaseMigration) GetSchema ¶
func (b *BaseMigration) GetSchema() contractsschema.Schema
GetSchema returns the schema for this migration.
func (*BaseMigration) SetSchema ¶
func (b *BaseMigration) SetSchema(schema contractsschema.Schema)
SetSchema sets the schema for this migration.
func (*BaseMigration) Signature ¶
func (b *BaseMigration) Signature() string
Signature Get the migration signature.
type MigrationInterface ¶
type MigrationInterface interface {
// Signature Get the migration signature.
Signature() string
// Description Get a human-readable description of what this migration does.
Description() string
// Up Run the migrations.
Up() error
// Down Reverse the migrations.
Down() error
// SetSchema sets the schema for this migration.
SetSchema(schema contractsschema.Schema)
// GetSchema returns the schema for this migration.
GetSchema() contractsschema.Schema
}
MigrationInterface defines the contract for a single migration.
type MigrationStatus ¶
type MigrationStatus string
MigrationStatus is the status value stored in the status column. A named type so only the declared constants can be assigned to MigrationTracker.Status — a misspelled literal is a compile error.
const ( MigrationStatusRunning MigrationStatus = "running" MigrationStatusCompleted MigrationStatus = "completed" MigrationStatusFailed MigrationStatus = "failed" // RolledBack marks a migration that was rolled back via Down(). MigrationStatusRolledBack MigrationStatus = "rolled_back" // RollbackFailed marks a migration whose Down() failed. The schema change // is still applied, so the migration counts as ran for Up() but remains // eligible for a rollback retry. MigrationStatusRollbackFailed MigrationStatus = "rollback_failed" )
Migration tracker status values stored in the status column.
type MigrationStatusResponse ¶ added in v0.53.0
type MigrationStatusResponse struct {
Signature string `json:"signature"` // The migration signature (e.g. "2026_06_15_120000_create_users"), not a tracker row ID
Description string `json:"description"`
Batch int `json:"batch"`
StartedAt time.Time `json:"started_at"`
CompletedAt time.Time `json:"completed_at"`
State string `json:"state"` // "pending", "running", "completed", "failed", "rollback_failed"
Error string `json:"error,omitempty"` // Error message when State is "failed" or "rollback_failed"
}
MigrationStatusResponse is the DTO returned by Migrator.Status(), derived from MigrationTracker rows and the registered (pending) migrations.
type MigrationTracker ¶
type MigrationTracker struct {
ID string // Unique, time-ordered record ID for this execution attempt
Batch int // Incrementing batch number grouping one Up() run; rollback rows keep the batch they reverse
Status MigrationStatus // one of the MigrationStatus* values (empty = completed, legacy rows — see note below)
Migration string // The migration signature (e.g., "2024_06_15_120000_create_users_table")
Description string // The migration description from Description() method
StartedAt time.Time // When the migration started
CompletedAt time.Time // When the migration attempt finished (success or failure)
ErrorMessage string // The error text when Status is "failed" or "rollback_failed"
}
MigrationTracker represents a migration record stored in the migration_tracker table This is the database model/entity used for persistence. The table is an append-only history: each Up or Down attempt adds a row, and a migration's current state is its most recent row.
Legacy note: rows written before the status column existed have Status = "" and are treated as "completed" at the read sites. This compatibility case (Status == "") should be removed after October 2027, once deployments have had a year to write a status on every new row.
func (MigrationTracker) MigrationName ¶ added in v0.55.0
func (t MigrationTracker) MigrationName() string
MigrationName returns the migration signature, falling back to ID if Migration is empty (legacy rows).
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator handles execution and tracking of interface-based migrations
func (*Migrator) AddMigration ¶
func (s *Migrator) AddMigration(migration MigrationInterface) error
AddMigration adds a new migration to the list. Returns an error if the migration signature is empty or already registered.
func (*Migrator) AddMigrations ¶
func (s *Migrator) AddMigrations(migrations []MigrationInterface) error
AddMigrations adds multiple migrations to the runner. Returns an error if any migration has an empty or duplicate signature.
func (*Migrator) RollbackSteps ¶
RollbackSteps rolls back the specified number of migrations
func (*Migrator) RollbackToBatch ¶
RollbackToBatch rolls back all migrations to the specified batch
func (*Migrator) SetLexicographicalOrdering ¶ added in v0.18.0
SetLexicographicalOrdering enables or disables lexicographical ordering of migrations. When enabled, migrations are sorted by signature before execution. Default is enabled.
func (*Migrator) SetSignatureValidation ¶
func (s *Migrator) SetSignatureValidation(enabled bool, format SignatureFormat)
SetSignatureValidation enables or disables signature format validation. When enabled, each migration signature is validated against the specified format before execution. Default is disabled.
func (*Migrator) SetTableName ¶
SetTableName sets the name of the migration tracking table. The name is validated to prevent SQL injection.
func (*Migrator) SetTransactionIsolationLevel ¶
SetTransactionIsolationLevel sets the transaction isolation level for migration operations
func (*Migrator) SetTransactionsEnabled ¶
SetTransactionsEnabled enables or disables transaction wrapping for migration operations
func (*Migrator) Status ¶
func (s *Migrator) Status() ([]MigrationStatusResponse, error)
Status returns migration status for all registered migrations. Includes both completed migrations (from the tracker table) and pending migrations (registered but not yet run).
type MigratorInterface ¶
type MigratorInterface interface {
AddMigration(migration MigrationInterface) error
AddMigrations(migrations []MigrationInterface) error
Up(ctx context.Context) error
Down(ctx context.Context) error
RollbackSteps(ctx context.Context, steps int) error
RollbackToBatch(ctx context.Context, batch int) error
Status() ([]MigrationStatusResponse, error)
Fresh(ctx context.Context) error
Reset(ctx context.Context) error
SetTransactionsEnabled(enabled bool)
SetTransactionIsolationLevel(level string)
SetTableName(name string) error
SetSignatureValidation(enabled bool, format SignatureFormat)
SetLexicographicalOrdering(enabled bool)
}
MigratorInterface defines the contract for migration management
func NewMigrator ¶
func NewMigrator(db *database.Database) MigratorInterface
NewMigrator creates a new Migrator instance Takes neat db instance as dependency, extracts schema and orm internally
func NewMigratorWithOptions ¶ added in v0.19.0
func NewMigratorWithOptions(db *database.Database, opts *Options) (MigratorInterface, error)
NewMigratorWithOptions creates a Migrator with the given options.
type Options ¶ added in v0.19.0
type Options struct {
TableName string
TransactionIsolationLevel string
LexicographicalOrdering bool
SignatureValidationEnabled bool
SignatureValidationFormat SignatureFormat
}
Options configures the Migrator at construction time.
type SignatureFormat ¶
type SignatureFormat string
SignatureFormat defines the format for migration signatures
const ( // SignatureFormatDateTime uses timestamp-based format (default) // Example: 2026_06_14_1200_create_users_table SignatureFormatDateTime SignatureFormat = "datetime" // SignatureFormatDate uses sequence-based format // Example: 2026_06_14_001_create_users_table SignatureFormatDate SignatureFormat = "date" // SignatureFormatUnix uses unix timestamp format (legacy) // Example: 1717080000_create_users_table SignatureFormatUnix SignatureFormat = "unix" // SignatureFormatCustom uses no prefix format restriction SignatureFormatCustom SignatureFormat = "custom" )