Documentation
¶
Index ¶
- func ValidateMigrationSignature(signature string, format SignatureFormat) error
- func ValidateTableName(name string) error
- type BaseMigration
- type MigrationInterface
- type MigrationStatus
- 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() ([]MigrationStatus, 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 struct {
ID string `json:"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", "completed", "failed"
}
MigrationStatus represents the status of a migration returned to users This is a DTO/response type derived from MigrationTracker data
type MigrationTracker ¶
type MigrationTracker struct {
ID string // The migration signature (e.g., "2024_06_15_120000_create_users_table")
Batch int // Timestamp ID (YYYYMMDDHHMMSS). Groups the run
Description string // The migration description from Description() method
StartedAt time.Time // When the migration started
CompletedAt time.Time // When the migration finished
}
MigrationTracker represents a migration record stored in the migration_tracker table This is the database model/entity used for persistence
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() ([]MigrationStatus, 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() ([]MigrationStatus, 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" )