-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimental): add package database with Store interface (#623)
- Loading branch information
Showing
12 changed files
with
158 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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. | ||
package database |
6 changes: 3 additions & 3 deletions
6
internal/sqlextended/sqlextended.go → database/sql_extended.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// 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. | ||
type Store interface { | ||
// CreateVersionTable creates the version table. This table is used to record applied | ||
// migrations. | ||
CreateVersionTable(ctx context.Context, db DBTxConn) error | ||
|
||
// InsertOrDelete inserts or deletes a version id from the version table. If direction is true, | ||
// insert the version id. If direction is false, delete the version id. | ||
InsertOrDelete(ctx context.Context, db DBTxConn, direction bool, version int64) error | ||
|
||
// GetMigration retrieves a single migration by version id. This method may return the raw sql | ||
// error if the query fails so the caller can assert for errors such as [sql.ErrNoRows]. | ||
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. | ||
ListMigrations(ctx context.Context, db DBTxConn) ([]*ListMigrationsResult, error) | ||
} | ||
|
||
type GetMigrationResult struct { | ||
Timestamp time.Time | ||
IsApplied bool | ||
} | ||
|
||
type ListMigrationsResult struct { | ||
Version int64 | ||
IsApplied bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.