database

package
v3.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 6 Imported by: 8

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"
	DialectTurso      Dialect = "turso"
	DialectVertica    Dialect = "vertica"
	DialectYdB        Dialect = "ydb"
)

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 name of the version table. This table is used to record applied migrations
	// and must not be an empty string.
	Tablename() string
	// CreateVersionTable creates the version table, which is used to track migrations. When
	// creating this table, the implementation MUST also insert a row for the initial version (0).
	CreateVersionTable(ctx context.Context, db DBTxConn) error
	// Insert a version id into the version table.
	Insert(ctx context.Context, db DBTxConn, req InsertRequest) error
	// Delete 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)
	// GetLatestVersion retrieves the last applied migration version. If no migrations exist, this
	// method must return -1 and no error.
	GetLatestVersion(ctx context.Context, db DBTxConn) (int64, 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)
}

Store is an interface that defines methods for tracking and managing migrations. It is used by the goose package to interact with a database. By defining a Store interface, multiple implementations can be created to support different databases without reimplementing the migration logic.

This package provides several dialects that implement the Store interface. While most users won't need to create their own Store, if you need to support a database that isn't currently supported, you can implement your own!

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