Documentation
¶
Index ¶
- Constants
- func CreateMigration(migrationsDir, name string) (string, error)
- func CreateMigrations(migrationsDir string, names []string) ([]string, error)
- func IsApplied(db *database.DB, version string) (bool, string, error)
- func SaveRecord(db *database.DB, version, status string, tx *sql.Tx) error
- func StatusText(status string) string
- type Executor
- type FilenameInfo
- type Migration
- type Record
Constants ¶
const ( // StatusUp represents an up migration status StatusUp = "up" // StatusDown represents a down migration status StatusDown = "down" // StatusSkip represents a skipped migration status StatusSkip = "skip" // StatusPending represents a pending migration status StatusPending = "pending" )
const ( MarkUp = "-- Migrate:UP" MarkDown = "-- Migrate:DOWN" // DateLayout defines the layout for migration filename DateLayout = "20060102-150405" PrefixFormat = "YYYYMMDD-HHMMSS" )
Variables ¶
This section is empty.
Functions ¶
func CreateMigration ¶
CreateMigration creates a new migration file with the specified name
func CreateMigrations ¶
CreateMigrations creates multi migration file with the specified names
func SaveRecord ¶
SaveRecord records a migration in the database
- status=up: insert a new record
- status=down: update the record
func StatusText ¶
StatusText returns the text representation of a migration status
Types ¶
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor handles the execution of migrations
func NewExecutor ¶
NewExecutor creates a new migration executor
func (*Executor) ExecuteDown ¶
ExecuteDown executes the DOWN part of a migration
type FilenameInfo ¶
type FilenameInfo struct {
Time time.Time // parsed time from Date field
Date string // eg: 20251105-102430
Name string // eg: add-age-index
}
FilenameInfo represents the information extracted from a migration filename
eg: 20251105-102430-add-age-index.sql
=> {Time: time.Time{}, Date: "20251105-102430", Name: "add-age-index"}
type Migration ¶
type Migration struct {
FileName string // filename as version
FilePath string // full path
// Contents of migration file
Contents string
// time from filename
Timestamp time.Time
// Version same as filename
Version string
// UpSection UP section contents
UpSection string
DownSection string
}
Migration represents a single migration file
func FindMigrations ¶
FindMigrations finds all migration files in the specified directory, and returns them sorted by timestamp
func MigrationsFrom ¶
MigrationsFrom creates migrations from a list of files
func NewMigration ¶
NewMigration creates a new Migration instance from a file path
func (*Migration) ParseContents ¶
ParseContents parses migration file contents, extracting UP and DOWN sections
type Record ¶
type Record struct {
// is migration filename
Version string `db:"version"`
AppliedAt time.Time `db:"applied_at"`
// up, skip, down.
Status string `db:"status"`
}
Record represents a record in the database migrations table
func GetAppliedSortedByDate ¶
GetAppliedSortedByDate returns applied migrations sorted by application date (most recent first)
func GetMigrationsStatus ¶
GetMigrationsStatus retrieves the status of all migrations