Skip to content

Commit

Permalink
Expand the Store interface and add GetLatestVersion method (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Apr 12, 2024
1 parent 42eab2b commit f4f1a24
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Expand the `Store` interface by adding a `GetLatestVersion` method.

## [v3.19.2] - 2024-03-13

- Remove duckdb support. The driver uses Cgo and we've decided to remove it until we can find a
Expand Down
8 changes: 5 additions & 3 deletions database/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const (
DialectRedshift Dialect = "redshift"
DialectSQLite3 Dialect = "sqlite3"
DialectTiDB Dialect = "tidb"
DialectTurso Dialect = "turso"
DialectVertica Dialect = "vertica"
DialectYdB Dialect = "ydb"
DialectTurso Dialect = "turso"
)

// NewStore returns a new [Store] implementation for the given dialect.
Expand Down Expand Up @@ -62,8 +62,6 @@ type store struct {

var _ Store = (*store)(nil)

func (s *store) private() {}

func (s *store) Tablename() string {
return s.tablename
}
Expand Down Expand Up @@ -111,6 +109,10 @@ func (s *store) GetMigration(
return &result, nil
}

func (s *store) GetLatestVersion(ctx context.Context, db DBTxConn) (int64, error) {
return -1, errors.New("not implemented")
}

func (s *store) ListMigrations(
ctx context.Context,
db DBTxConn,
Expand Down
34 changes: 16 additions & 18 deletions database/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,36 @@ var (
ErrVersionNotFound = errors.New("version not found")
)

// 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.
// 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.
//
// Each database dialect requires a specific implementation of this interface. A dialect represents
// a set of SQL statements specific to a particular database system.
// 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!
type Store interface {
// Tablename is the version table used to record applied migrations. Must not be empty.
// 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. 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 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 inserts a version id into the version table.
// Insert 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 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)

// TODO(mf): remove this method once the Provider is public and a custom Store can be used.
private()
}

type InsertRequest struct {
Expand Down

0 comments on commit f4f1a24

Please sign in to comment.