migration

package
v0.0.0-...-1e0776f Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSchema

func CreateSchema(db *sql.DB, targetDriver, migrationsPath string) error

CreateSchema runs migrations to create the schema in the target database. It determines the correct migrations path based on the target driver and runs all available migrations.

func DropAllTables

func DropAllTables(db *sql.DB, driver string) error

DropAllTables drops all tables in the target database. This is used when migration fails and we need to clean up.

func GetMigrationsPath

func GetMigrationsPath() string

GetMigrationsPath returns the default migrations path for the application.

func OpenTargetDB

func OpenTargetDB(config TargetConfig) (*sql.DB, error)

OpenTargetDB opens a connection to the target database.

func QuickVerify

func QuickVerify(
	ctx context.Context,
	sourceDB *sql.DB,
	sourceDriver string,
	targetDB *sql.DB,
	targetDriver string,
) (bool, error)

QuickVerify performs a quick verification by comparing total row counts. This is faster than full verification but less thorough.

func TransferData

func TransferData(
	ctx context.Context,
	sourceDB *sql.DB,
	sourceDriver string,
	targetDB *sql.DB,
	targetDriver string,
	onProgress ProgressCallback,
) error

TransferData copies all data from source to target database.

func VerifySchema

func VerifySchema(ctx context.Context, db *sql.DB, driver string) error

VerifySchema verifies that the target database has the expected schema.

Types

type ConfigSaver

type ConfigSaver func(driver string, pgHost string, pgPort int, pgUser, pgDatabase, pgSSLMode string) error

ConfigSaver is a function that saves database configuration after migration. This is used to break the import cycle with the config package.

type ConnectionTestDetails

type ConnectionTestDetails struct {
	Version        string    `json:"version"`
	ServerTime     time.Time `json:"serverTime"`
	IsEmpty        bool      `json:"isEmpty"`
	ExistingTables int       `json:"existingTables"`
}

ConnectionTestDetails contains detailed connection information.

func GetConnectionDetails

func GetConnectionDetails(ctx context.Context, db *sql.DB, driver string) (*ConnectionTestDetails, error)

GetConnectionDetails gets details about a database connection.

type ConnectionTestResult

type ConnectionTestResult struct {
	Success bool                   `json:"success"`
	Message string                 `json:"message"`
	Error   string                 `json:"error,omitempty"`
	Details *ConnectionTestDetails `json:"details,omitempty"`
}

ConnectionTestResult contains the result of a connection test.

func TestConnection

func TestConnection(ctx context.Context, config TargetConfig) ConnectionTestResult

TestConnection tests a connection to the target database.

type Error

type Error struct {
	Phase   Phase  `json:"phase"`
	Table   string `json:"table,omitempty"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

Error contains migration error information.

type EstimateInfo

type EstimateInfo struct {
	DurationSeconds int         `json:"durationSeconds"`
	DurationHuman   string      `json:"durationHuman"`
	DataSizeBytes   int64       `json:"dataSizeBytes"`
	Tables          []TableInfo `json:"tables"`
}

EstimateInfo contains estimated migration metrics.

type EstimateRequest

type EstimateRequest struct {
	TargetDriver string `json:"targetDriver"`
}

EstimateRequest contains the request for migration estimation.

type EstimateResponse

type EstimateResponse struct {
	Source   SourceInfo   `json:"source"`
	Estimate EstimateInfo `json:"estimate"`
	Warnings []string     `json:"warnings"`
}

EstimateResponse contains the migration estimation result.

func EstimateMigration

func EstimateMigration(ctx context.Context, db *sql.DB, sourceDriver, targetDriver string) (*EstimateResponse, error)

EstimateMigration estimates the migration time and data size.

type MigrationRequest

type MigrationRequest struct {
	TargetDriver string          `json:"targetDriver"`
	Postgres     *PostgresConfig `json:"postgres,omitempty"`
	SQLite       *SQLiteConfig   `json:"sqlite,omitempty"`
}

MigrationRequest contains the request to start a migration.

type MigrationStartResponse

type MigrationStartResponse struct {
	Started     bool   `json:"started"`
	MigrationID string `json:"migrationId,omitempty"`
	Message     string `json:"message,omitempty"`
	Error       string `json:"error,omitempty"`
}

MigrationStartResponse contains the response when starting a migration.

type Phase

type Phase string

Phase represents a migration phase.

const (
	PhaseMaintenanceMode Phase = "maintenance_mode"
	PhaseBackup          Phase = "backup"
	PhaseConnectTarget   Phase = "connect_target"
	PhaseCreateSchema    Phase = "create_schema"
	PhaseCopying         Phase = "copying"
	PhaseVerification    Phase = "verification"
	PhaseUpdateConfig    Phase = "update_config"
)

type PhaseInfo

type PhaseInfo struct {
	Name   Phase       `json:"name"`
	Status PhaseStatus `json:"status"`
}

PhaseInfo contains information about a migration phase.

type PhaseStatus

type PhaseStatus string

PhaseStatus represents the status of a migration phase.

const (
	PhaseStatusPending    PhaseStatus = "pending"
	PhaseStatusInProgress PhaseStatus = "in_progress"
	PhaseStatusCompleted  PhaseStatus = "completed"
	PhaseStatusFailed     PhaseStatus = "failed"
	PhaseStatusSkipped    PhaseStatus = "skipped"
)

type PostgresConfig

type PostgresConfig struct {
	Host     string `json:"host"`
	Port     int    `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
	Database string `json:"database"`
	SSLMode  string `json:"sslMode"`
}

PostgresConfig contains PostgreSQL connection settings.

type Progress

type Progress struct {
	CurrentTable              string `json:"currentTable,omitempty"`
	TablesCompleted           int    `json:"tablesCompleted"`
	TablesTotal               int    `json:"tablesTotal"`
	RowsCopied                int64  `json:"rowsCopied"`
	RowsTotal                 int64  `json:"rowsTotal"`
	BytesCopied               int64  `json:"bytesCopied"`
	BytesTotal                int64  `json:"bytesTotal"`
	PercentComplete           int    `json:"percentComplete"`
	ElapsedSeconds            int    `json:"elapsedSeconds"`
	EstimatedRemainingSeconds int    `json:"estimatedRemainingSeconds"`
}

Progress contains migration progress information.

type ProgressCallback

type ProgressCallback func(progress TransferProgress)

ProgressCallback is called during transfer to report progress.

type Result

type Result struct {
	TablesMigrated     int    `json:"tablesMigrated"`
	RowsMigrated       int64  `json:"rowsMigrated"`
	VerificationPassed bool   `json:"verificationPassed"`
	OldDatabasePath    string `json:"oldDatabasePath,omitempty"`
	RequiresRestart    bool   `json:"requiresRestart"`
	DurationSeconds    int    `json:"durationSeconds"`
}

Result contains migration result information.

type SQLiteConfig

type SQLiteConfig struct {
	Path string `json:"path"`
}

SQLiteConfig contains SQLite connection settings.

type Service

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

Service orchestrates database migrations.

func NewService

func NewService(sourceDB *sql.DB, sourceDriver string, maintenanceMgr *system.MaintenanceManager, configSaver ConfigSaver) *Service

NewService creates a new migration service.

func (*Service) Estimate

func (s *Service) Estimate(ctx context.Context, targetDriver string) (*EstimateResponse, error)

Estimate estimates the migration time and data size.

func (*Service) GetState

func (s *Service) GetState() State

GetState returns the current migration state.

func (*Service) IsInProgress

func (s *Service) IsInProgress() bool

IsInProgress returns whether a migration is currently in progress.

func (*Service) SetOnChange

func (s *Service) SetOnChange(fn func(state State))

SetOnChange sets a callback for state changes.

func (*Service) Start

Start begins a database migration.

func (*Service) TestConnection

func (s *Service) TestConnection(ctx context.Context, config TargetConfig) ConnectionTestResult

TestConnection tests a connection to the target database.

type SourceInfo

type SourceInfo struct {
	Driver     string `json:"driver"`
	SizeBytes  int64  `json:"sizeBytes"`
	TableCount int    `json:"tableCount"`
	TotalRows  int64  `json:"totalRows"`
}

SourceInfo contains information about the source database.

type State

type State struct {
	Status      Status      `json:"status"`
	MigrationID string      `json:"migrationId,omitempty"`
	Phase       Phase       `json:"phase,omitempty"`
	StartedAt   *time.Time  `json:"startedAt,omitempty"`
	CompletedAt *time.Time  `json:"completedAt,omitempty"`
	FailedAt    *time.Time  `json:"failedAt,omitempty"`
	Progress    *Progress   `json:"progress,omitempty"`
	Phases      []PhaseInfo `json:"phases,omitempty"`
	Result      *Result     `json:"result,omitempty"`
	Error       *Error      `json:"error,omitempty"`
}

State represents the current migration state.

type Status

type Status string

Status represents the current migration status.

const (
	StatusIdle       Status = "idle"
	StatusInProgress Status = "in_progress"
	StatusCompleted  Status = "completed"
	StatusFailed     Status = "failed"
)

type TableInfo

type TableInfo struct {
	Name      string `json:"name"`
	Rows      int64  `json:"rows"`
	SizeBytes int64  `json:"sizeBytes"`
}

TableInfo contains information about a table.

func GetTableInfo

func GetTableInfo(ctx context.Context, db *sql.DB, driver string) ([]TableInfo, error)

GetTableInfo gets information about tables in a database.

type TableVerificationResult

type TableVerificationResult struct {
	TableName  string `json:"tableName"`
	SourceRows int64  `json:"sourceRows"`
	TargetRows int64  `json:"targetRows"`
	Match      bool   `json:"match"`
	Error      string `json:"error,omitempty"`
}

TableVerificationResult contains verification result for a single table.

type TargetConfig

type TargetConfig struct {
	Driver   string          `json:"driver"`
	Postgres *PostgresConfig `json:"postgres,omitempty"`
	SQLite   *SQLiteConfig   `json:"sqlite,omitempty"`
}

TargetConfig contains configuration for the target database.

type TransferProgress

type TransferProgress struct {
	CurrentTable    string
	TablesCompleted int
	TablesTotal     int
	RowsCopied      int64
	RowsTotal       int64
}

TransferProgress tracks progress during data transfer.

type VerificationResult

type VerificationResult struct {
	Success      bool                      `json:"success"`
	TableResults []TableVerificationResult `json:"tableResults"`
	Errors       []string                  `json:"errors,omitempty"`
}

VerificationResult contains the result of migration verification.

func VerifyMigration

func VerifyMigration(
	ctx context.Context,
	sourceDB *sql.DB,
	sourceDriver string,
	targetDB *sql.DB,
	targetDriver string,
) (*VerificationResult, error)

VerifyMigration verifies that data was correctly migrated by comparing row counts. It only verifies tables that exist in BOTH source AND target (some source tables like plugin tables may not exist in target).

Jump to

Keyboard shortcuts

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