migration

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MigrationsDir string
	DBType        string
	Verbose       bool
	DryRun        bool
}

Config holds the migration configuration

type DatabaseDriver

type DatabaseDriver interface {
	MigrationTracker
	// ExecuteMigration executes a migration file
	ExecuteMigration(migrationFile string, isUp bool) error
	// GetDBType returns the database type (postgres, mongo)
	GetDBType() string
}

DatabaseDriver defines the interface for database-specific operations

type Manager

type Manager struct {
	Driver DatabaseDriver
	Config *Config
}

Manager handles migration operations

func NewManager

func NewManager(driver DatabaseDriver, config *Config) *Manager

NewManager creates a new migration manager

func (*Manager) CreateMigration

func (m *Manager) CreateMigration(name string) error

CreateMigration creates new migration files

func (*Manager) GetAppliedMigrations

func (m *Manager) GetAppliedMigrations() ([]*Migration, error)

GetAppliedMigrations returns migrations that have been applied

func (*Manager) GetCurrentVersion

func (m *Manager) GetCurrentVersion() (int64, error)

GetCurrentVersion returns the current migration version

func (*Manager) GetMigrationsDir

func (m *Manager) GetMigrationsDir() string

GetMigrationsDir returns the migrations directory for the current database type

func (*Manager) GetPendingMigrations

func (m *Manager) GetPendingMigrations() ([]*Migration, error)

GetPendingMigrations returns migrations that haven't been applied

func (*Manager) GetStatus

func (m *Manager) GetStatus() ([]*Migration, error)

GetStatus returns the migration status

func (*Manager) LoadMigrations

func (m *Manager) LoadMigrations() ([]*Migration, error)

LoadMigrations loads all migration files from the migrations directory

func (*Manager) Log

func (m *Manager) Log(msg string, c *color.Color)

Log prints a message with optional color

func (*Manager) LogError

func (m *Manager) LogError(msg string)

LogError prints an error message in red

func (*Manager) LogInfo

func (m *Manager) LogInfo(msg string)

LogInfo prints an info message in yellow

func (*Manager) LogSuccess

func (m *Manager) LogSuccess(msg string)

LogSuccess prints a success message in green

func (*Manager) MigrateDown

func (m *Manager) MigrateDown(targetVersion int64, count int) error

MigrateDown reverts applied migrations

func (*Manager) MigrateUp

func (m *Manager) MigrateUp(targetVersion int64, count int) error

MigrateUp applies pending migrations

type Migration

type Migration struct {
	Version   int64
	Name      string
	UpFile    string
	DownFile  string
	Applied   bool
	AppliedAt *time.Time
}

Migration represents a single migration file

type MigrationTracker

type MigrationTracker interface {
	// Initialize creates the tracking table/collection if it doesn't exist
	Initialize() error
	// GetAppliedMigrations returns all applied migration versions
	GetAppliedMigrations() ([]int64, error)
	// MarkMigrationApplied marks a migration as applied
	MarkMigrationApplied(version int64) error
	// MarkMigrationReverted removes a migration from applied list
	MarkMigrationReverted(version int64) error
	// GetCurrentVersion returns the latest applied migration version
	GetCurrentVersion() (int64, error)
	// Close closes the database connection
	Close() error
}

MigrationTracker defines the interface for tracking applied migrations

type MongoDriver

type MongoDriver struct {
	// contains filtered or unexported fields
}

MongoDriver implements DatabaseDriver for MongoDB

func NewMongoDriver

func NewMongoDriver() (*MongoDriver, error)

NewMongoDriver creates a new MongoDB driver

func (*MongoDriver) Close

func (m *MongoDriver) Close() error

Close closes the MongoDB connection

func (*MongoDriver) ExecuteMigration

func (m *MongoDriver) ExecuteMigration(migrationFile string, isUp bool) error

ExecuteMigration executes a migration file using mongosh

func (*MongoDriver) GetAppliedMigrations

func (m *MongoDriver) GetAppliedMigrations() ([]int64, error)

GetAppliedMigrations returns all applied migration versions

func (*MongoDriver) GetCurrentVersion

func (m *MongoDriver) GetCurrentVersion() (int64, error)

GetCurrentVersion returns the latest applied migration version

func (*MongoDriver) GetDBType

func (m *MongoDriver) GetDBType() string

GetDBType returns the database type

func (*MongoDriver) Initialize

func (m *MongoDriver) Initialize() error

Initialize creates the schema_migrations collection if it doesn't exist

func (*MongoDriver) MarkMigrationApplied

func (m *MongoDriver) MarkMigrationApplied(version int64) error

MarkMigrationApplied marks a migration as applied

func (*MongoDriver) MarkMigrationReverted

func (m *MongoDriver) MarkMigrationReverted(version int64) error

MarkMigrationReverted removes a migration from applied list

type PostgresDriver

type PostgresDriver struct {
	// contains filtered or unexported fields
}

PostgresDriver implements DatabaseDriver for PostgreSQL

func NewPostgresDriver

func NewPostgresDriver() (*PostgresDriver, error)

NewPostgresDriver creates a new PostgreSQL driver

func (*PostgresDriver) Close

func (p *PostgresDriver) Close() error

Close closes the database connection

func (*PostgresDriver) ExecuteMigration

func (p *PostgresDriver) ExecuteMigration(migrationFile string, isUp bool) error

ExecuteMigration executes a migration file

func (*PostgresDriver) GetAppliedMigrations

func (p *PostgresDriver) GetAppliedMigrations() ([]int64, error)

GetAppliedMigrations returns all applied migration versions

func (*PostgresDriver) GetCurrentVersion

func (p *PostgresDriver) GetCurrentVersion() (int64, error)

GetCurrentVersion returns the latest applied migration version

func (*PostgresDriver) GetDBType

func (p *PostgresDriver) GetDBType() string

GetDBType returns the database type

func (*PostgresDriver) Initialize

func (p *PostgresDriver) Initialize() error

Initialize creates the schema_migrations table if it doesn't exist

func (*PostgresDriver) MarkMigrationApplied

func (p *PostgresDriver) MarkMigrationApplied(version int64) error

MarkMigrationApplied marks a migration as applied

func (*PostgresDriver) MarkMigrationReverted

func (p *PostgresDriver) MarkMigrationReverted(version int64) error

MarkMigrationReverted removes a migration from applied list

type SchemaMigration

type SchemaMigration struct {
	Version   int64     `bson:"version"`
	AppliedAt time.Time `bson:"applied_at"`
}

SchemaMigration represents a document in the schema_migrations collection

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL