database

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package database defines a generic Store interface for goose to use when interacting with the database. It is meant to be generic and not tied to any specific database technology.

At a high level, a Store is responsible for:

  • Creating a version table
  • Inserting and deleting a version
  • Getting a specific version
  • Listing all applied versions

Use the NewStore function to create a Store for one of the supported dialects.

For more advanced use cases, it's possible to implement a custom Store for a database that goose does not support.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrVersionNotFound must be returned by [GetMigration] when a migration version is not found.
	ErrVersionNotFound = errors.New("version not found")
)

Functions

This section is empty.

Types

type DBTxConn

type DBTxConn interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

DBTxConn is a thin interface for common methods that is satisfied by *sql.DB, *sql.Tx and *sql.Conn.

There is a long outstanding issue to formalize a std lib interface, but alas. See: https://github.com/golang/go/issues/14468

type Dialect

type Dialect string

Dialect is the type of database dialect.

const (
	DialectClickHouse  Dialect = "clickhouse"
	DialectMSSQL       Dialect = "mssql"
	DialectMySQL       Dialect = "mysql"
	DialectPostgres    Dialect = "postgres"
	DialectRedshift    Dialect = "redshift"
	DialectSQLite3     Dialect = "sqlite3"
	DialectTiDB        Dialect = "tidb"
	DialectVertica     Dialect = "vertica"
	DialectYdB         Dialect = "ydb"
	DialectSQLAnywhere Dialect = "sqlanywhere"
)

type GetMigrationResult

type GetMigrationResult struct {
	Timestamp time.Time
	IsApplied bool
}

type InsertRequest

type InsertRequest struct {
	Version int64
}

type ListMigrationsResult

type ListMigrationsResult struct {
	Version   int64
	IsApplied bool
}

type Store

type Store interface {
	// Tablename is the version table used to record applied migrations. Must not be empty.
	Tablename() string

	// CreateVersionTable creates the version table. This table is used to record applied
	// migrations. When creating the table, the implementation must also insert a row for the
	// initial version (0).
	CreateVersionTable(ctx context.Context, db DBTxConn) error

	// Insert inserts a version id into the version table.
	Insert(ctx context.Context, db DBTxConn, req InsertRequest) error

	// Delete deletes a version id from the version table.
	Delete(ctx context.Context, db DBTxConn, version int64) error

	// GetMigration retrieves a single migration by version id. If the query succeeds, but the
	// version is not found, this method must return [ErrVersionNotFound].
	GetMigration(ctx context.Context, db DBTxConn, version int64) (*GetMigrationResult, error)

	// ListMigrations retrieves all migrations sorted in descending order by id or timestamp. If
	// there are no migrations, return empty slice with no error. Typically this method will return
	// at least one migration, because the initial version (0) is always inserted into the version
	// table when it is created.
	ListMigrations(ctx context.Context, db DBTxConn) ([]*ListMigrationsResult, error)
	// contains filtered or unexported methods
}

Store is an interface that defines methods for managing database migrations and versioning. By defining a Store interface, we can support multiple databases with consistent functionality.

Each database dialect requires a specific implementation of this interface. A dialect represents a set of SQL statements specific to a particular database system.

func NewStore

func NewStore(dialect Dialect, tablename string) (Store, error)

NewStore returns a new Store implementation for the given dialect.

Jump to

Keyboard shortcuts

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