gostgrator

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gostgrator provides database migration capabilities.

Package gostgrator provides database migration capabilities.

Package gostgrator provides database migration capabilities.

Package gostgrator provides database migration capabilities.

Package gostgrator provides database migration capabilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	Version   = "dev"
	GitCommit = "none"
)
View Source
var DefaultConfig = Config{
	SchemaTable:       "schemaversion",
	ValidateChecksums: true,
}

DefaultConfig provides default values for configuration.

Functions

func CreateMigration

func CreateMigration(cfg Config, description string, mode string) error

CreateMigration creates a new pair of migration files (do/undo). description: a human-readable description that will be kebab-cased for the filename. mode: "int" for integer increment (default) or "timestamp" to use the Unix timestamp.

Types

type Client

type Client interface {
	QueryContext(ctx context.Context, query string) (*sql.Rows, error)
	ExecContext(ctx context.Context, script string) (sql.Result, error)
	GetDatabaseVersionSql() string
	HasVersionTable(ctx context.Context) (bool, error)
	EnsureTable(ctx context.Context) error
	GetMd5Sql(m Migration) string
	PersistActionSql(m Migration) string
}

Client defines the interface for migration clients.

func NewClient

func NewClient(cfg Config, db *sql.DB) (Client, error)

NewClient creates a new Client based on the provided configuration and database connection.

func NewPostgresClient

func NewPostgresClient(cfg Config, db *sql.DB) Client

NewPostgresClient creates a new PostgresClient.

func NewSqlite3Client

func NewSqlite3Client(cfg Config, db *sql.DB) Client

NewSqlite3Client creates a new Sqlite3Client.

type Config

type Config struct {
	// Driver is the database driver, e.g., "pg" or "sqlite3".
	Driver string
	// SchemaTable is the name of the migration table.
	SchemaTable string

	// MigrationPattern is the glob pattern for migration files (e.g. "./migrations/*.sql").
	MigrationPattern string

	// Newline is the desired newline style ("LF", "CR", or "CRLF").
	Newline string

	// ValidateChecksums indicates if the tool should validate migration checksums.
	ValidateChecksums bool
}

Config holds settings for migrations.

type Gostgrator

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

Gostgrator is the main orchestrator for running database migrations.

It loads migration files, determines the current database version, validates checksums (if enabled), and runs the necessary migrations to reach a target version.

func NewGostgrator

func NewGostgrator(cfg Config, db *sql.DB) (*Gostgrator, error)

NewGostgrator creates a new Gostgrator instance with the provided configuration and database connection.

func (*Gostgrator) CreateMigration

func (g *Gostgrator) CreateMigration(description, mode string) error

(Optional) If you prefer to expose this functionality as a method on Gostgrator, you can add the following method.

func (*Gostgrator) Down

func (g *Gostgrator) Down(ctx context.Context, steps int) ([]Migration, error)

Down rolls back the migrations by the given number of steps. It computes the target version as the current version minus steps (not going below zero), and then calls Migrate to perform the undo operations.

func (*Gostgrator) GetDatabaseVersion

func (g *Gostgrator) GetDatabaseVersion(ctx context.Context) (int, error)

GetDatabaseVersion returns the current database version. If the migration table is not initialized, it returns 0.

func (*Gostgrator) GetMaxVersion

func (g *Gostgrator) GetMaxVersion() (int, error)

GetMaxVersion returns the highest migration version available.

func (*Gostgrator) GetMigrations

func (g *Gostgrator) GetMigrations() ([]Migration, error)

func (*Gostgrator) GetRunnableMigrations

func (g *Gostgrator) GetRunnableMigrations(databaseVersion, targetVersion int) ([]Migration, error)

func (*Gostgrator) Migrate

func (g *Gostgrator) Migrate(ctx context.Context, target string) ([]Migration, error)

Migrate moves the schema to the target version. If target is "max" or empty, it migrates to the highest available version.

func (*Gostgrator) QueryContext

func (g *Gostgrator) QueryContext(ctx context.Context, query string) (*sql.Rows, error)

QueryContext is a helper to execute a query using the underlying client.

func (*Gostgrator) RunMigrations

func (g *Gostgrator) RunMigrations(ctx context.Context, migrations []Migration) ([]Migration, error)

RunMigrations applies the provided migrations in sequence.

func (*Gostgrator) ValidateMigrations

func (g *Gostgrator) ValidateMigrations(ctx context.Context, databaseVersion int) error

ValidateMigrations verifies that applied migrations have not changed by comparing MD5 checksums.

type Migration

type Migration struct {
	// Version of the migration.
	Version int

	// Action, e.g., "do" or "undo".
	Action string

	// Filename is the path to the migration file.
	Filename string

	// Name is an optional descriptive name of the migration.
	Name string

	// Md5 is the MD5 checksum of the migration file.
	Md5 string
}

Migration represents a single migration file.

func GetMigrations

func GetMigrations(cfg Config) ([]Migration, error)

GetMigrations scans for migration files matching the pattern and loads them.

func (*Migration) GetSQL

func (m *Migration) GetSQL() (string, error)

GetSQL reads the migration file's content.

type PostgresClient

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

PostgresClient implements the Client interface for PostgreSQL.

func (*PostgresClient) EnsureTable

func (c *PostgresClient) EnsureTable(ctx context.Context) error

EnsureTable creates the migration table if it does not exist and adds missing columns.

func (*PostgresClient) ExecContext

func (c *PostgresClient) ExecContext(ctx context.Context, script string) (sql.Result, error)

Exposing ExecContext from the configured db connection.

func (*PostgresClient) GetDatabaseVersionSql

func (c *PostgresClient) GetDatabaseVersionSql() string

GetDatabaseVersionSql returns SQL to fetch the highest applied migration version.

func (*PostgresClient) GetMd5Sql

func (c *PostgresClient) GetMd5Sql(m Migration) string

GetMd5Sql returns SQL to fetch the MD5 checksum for a migration version.

func (*PostgresClient) HasVersionTable

func (c *PostgresClient) HasVersionTable(ctx context.Context) (bool, error)

HasVersionTable checks for the existence of the migration table.

func (*PostgresClient) PersistActionSql

func (c *PostgresClient) PersistActionSql(m Migration) string

PersistActionSql generates SQL to record a migration action.

func (*PostgresClient) QueryContext

func (c *PostgresClient) QueryContext(ctx context.Context, query string) (*sql.Rows, error)

Exposes the QueryContext method from the configured db connection.

type Sqlite3Client

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

Sqlite3Client implements the Client interface for SQLite.

func (*Sqlite3Client) EnsureTable

func (c *Sqlite3Client) EnsureTable(ctx context.Context) error

EnsureTable creates the migration table if it does not exist and adds missing columns.

func (*Sqlite3Client) ExecContext

func (c *Sqlite3Client) ExecContext(ctx context.Context, script string) (sql.Result, error)

Exposing ExecContext from the configured db connection.

func (*Sqlite3Client) GetDatabaseVersionSql

func (c *Sqlite3Client) GetDatabaseVersionSql() string

GetDatabaseVersionSql returns SQL to fetch the highest applied migration version.

func (*Sqlite3Client) GetMd5Sql

func (c *Sqlite3Client) GetMd5Sql(m Migration) string

GetMd5Sql returns SQL to fetch the MD5 checksum for a migration version.

func (*Sqlite3Client) HasVersionTable

func (c *Sqlite3Client) HasVersionTable(ctx context.Context) (bool, error)

HasVersionTable checks for the existence of the migration table.

func (*Sqlite3Client) PersistActionSql

func (c *Sqlite3Client) PersistActionSql(m Migration) string

PersistActionSql generates SQL to record a migration action.

func (*Sqlite3Client) QueryContext

func (c *Sqlite3Client) QueryContext(ctx context.Context, query string) (*sql.Rows, error)

Exposes the QueryContext method from the configured db connection.

Jump to

Keyboard shortcuts

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