Documentation
¶
Overview ¶
包 migration 提供内部数据迁移执行能力。
该包包含 AgentFlow 在 `internal/migration` 目录下的核心实现。
Index ¶
- func BuildDatabaseURL(dbType DatabaseType, host string, port int, ...) string
- func GetMigrationsPath(dbType DatabaseType) string
- type CLI
- func (c *CLI) RunDown(ctx context.Context) error
- func (c *CLI) RunDownAll(ctx context.Context) error
- func (c *CLI) RunForce(ctx context.Context, version int) error
- func (c *CLI) RunGoto(ctx context.Context, version uint) error
- func (c *CLI) RunInfo(ctx context.Context) error
- func (c *CLI) RunStatus(ctx context.Context) error
- func (c *CLI) RunSteps(ctx context.Context, n int) error
- func (c *CLI) RunUp(ctx context.Context) error
- func (c *CLI) RunVersion(ctx context.Context) error
- func (c *CLI) SetOutput(w io.Writer)
- type Config
- type DatabaseType
- type DefaultMigrator
- func (m *DefaultMigrator) Close() error
- func (m *DefaultMigrator) Down(ctx context.Context) error
- func (m *DefaultMigrator) DownAll(ctx context.Context) error
- func (m *DefaultMigrator) Force(ctx context.Context, version int) error
- func (m *DefaultMigrator) Goto(ctx context.Context, version uint) error
- func (m *DefaultMigrator) Info(ctx context.Context) (*MigrationInfo, error)
- func (m *DefaultMigrator) Status(ctx context.Context) ([]MigrationStatus, error)
- func (m *DefaultMigrator) Steps(ctx context.Context, n int) error
- func (m *DefaultMigrator) Up(ctx context.Context) error
- func (m *DefaultMigrator) Version(ctx context.Context) (uint, bool, error)
- type MigrationInfo
- type MigrationStatus
- type Migrator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildDatabaseURL ¶
func BuildDatabaseURL(dbType DatabaseType, host string, port int, database, username, password, sslMode string) string
BuildDatabaseURL builds a database URL from components
func GetMigrationsPath ¶
func GetMigrationsPath(dbType DatabaseType) string
GetMigrationsPath returns the path to migration files for a database type
Types ¶
type CLI ¶
type CLI struct {
// contains filtered or unexported fields
}
CLI provides command-line interface functionality for migrations
func (*CLI) RunDownAll ¶
RunDownAll rolls back all migrations
func (*CLI) RunVersion ¶
RunVersion shows the current migration version
type Config ¶
type Config struct {
// DatabaseType specifies the type of database (postgres, mysql, sqlite)
DatabaseType DatabaseType
// DatabaseURL is the connection string for the database
// Format depends on database type:
// - PostgreSQL: postgres://user:password@host:port/dbname?sslmode=disable
// - MySQL: user:password@tcp(host:port)/dbname?parseTime=true
// - SQLite: file:path/to/db.sqlite?mode=rwc
DatabaseURL string
// MigrationsPath is the path to migration files (optional, uses embedded by default)
MigrationsPath string
// TableName is the name of the migrations table (default: schema_migrations)
TableName string
// LockTimeout is the timeout for acquiring migration lock
LockTimeout time.Duration
}
Config holds the configuration for the migrator
type DatabaseType ¶
type DatabaseType string
DatabaseType represents the type of database
const ( // DatabaseTypePostgres represents PostgreSQL database DatabaseTypePostgres DatabaseType = "postgres" // DatabaseTypeMySQL represents MySQL database DatabaseTypeMySQL DatabaseType = "mysql" // DatabaseTypeSQLite represents SQLite database DatabaseTypeSQLite DatabaseType = "sqlite" )
func ParseDatabaseType ¶
func ParseDatabaseType(s string) (DatabaseType, error)
ParseDatabaseType parses a database type string
type DefaultMigrator ¶
type DefaultMigrator struct {
// contains filtered or unexported fields
}
DefaultMigrator implements the Migrator interface using golang-migrate
func NewMigrator ¶
func NewMigrator(cfg *Config) (*DefaultMigrator, error)
NewMigrator creates a new migrator instance
func NewMigratorFromConfig ¶
func NewMigratorFromConfig(cfg *appconfig.Config) (*DefaultMigrator, error)
NewMigratorFromConfig creates a new migrator from application configuration
func NewMigratorFromDatabaseConfig ¶
func NewMigratorFromDatabaseConfig(dbCfg appconfig.DatabaseConfig) (*DefaultMigrator, error)
NewMigratorFromDatabaseConfig creates a new migrator from database configuration
func NewMigratorFromURL ¶
func NewMigratorFromURL(dbType, dbURL string) (*DefaultMigrator, error)
NewMigratorFromURL creates a new migrator from a database URL
func (*DefaultMigrator) Close ¶
func (m *DefaultMigrator) Close() error
Close closes the migrator and releases resources
func (*DefaultMigrator) Down ¶
func (m *DefaultMigrator) Down(ctx context.Context) error
Down rolls back the last migration
func (*DefaultMigrator) DownAll ¶
func (m *DefaultMigrator) DownAll(ctx context.Context) error
DownAll rolls back all migrations
func (*DefaultMigrator) Force ¶
func (m *DefaultMigrator) Force(ctx context.Context, version int) error
Force sets the migration version without running migrations
func (*DefaultMigrator) Goto ¶
func (m *DefaultMigrator) Goto(ctx context.Context, version uint) error
Goto migrates to a specific version
func (*DefaultMigrator) Info ¶
func (m *DefaultMigrator) Info(ctx context.Context) (*MigrationInfo, error)
Info returns information about the current migration state
func (*DefaultMigrator) Status ¶
func (m *DefaultMigrator) Status(ctx context.Context) ([]MigrationStatus, error)
Status returns the status of all migrations
func (*DefaultMigrator) Steps ¶
func (m *DefaultMigrator) Steps(ctx context.Context, n int) error
Steps applies or rolls back n migrations
type MigrationInfo ¶
type MigrationInfo struct {
CurrentVersion uint
Dirty bool
TotalMigrations int
AppliedMigrations int
PendingMigrations int
}
MigrationInfo contains information about the current migration state
type MigrationStatus ¶
type MigrationStatus struct {
Version uint
Name string
Applied bool
AppliedAt *time.Time
Dirty bool
}
MigrationStatus represents the status of a migration
type Migrator ¶
type Migrator interface {
// Up applies all pending migrations
Up(ctx context.Context) error
// Down rolls back the last migration
Down(ctx context.Context) error
// DownAll rolls back all migrations
DownAll(ctx context.Context) error
// Steps applies or rolls back n migrations
// Positive n applies migrations, negative n rolls back
Steps(ctx context.Context, n int) error
// Goto migrates to a specific version
Goto(ctx context.Context, version uint) error
// Force sets the migration version without running migrations
Force(ctx context.Context, version int) error
// Version returns the current migration version
Version(ctx context.Context) (uint, bool, error)
// Status returns the status of all migrations
Status(ctx context.Context) ([]MigrationStatus, error)
// Info returns information about the current migration state
Info(ctx context.Context) (*MigrationInfo, error)
// Close closes the migrator and releases resources
Close() error
}
Migrator defines the interface for database migrations