-
Notifications
You must be signed in to change notification settings - Fork 523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
experiemntal: add StoreController to allowing extending interface #752
Open
mfridman
wants to merge
3
commits into
master
Choose a base branch
from
mf/storecontroller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
) | ||
|
||
// ErrNotSupported is returned when an optional method is not supported by the Store implementation. | ||
var ErrNotSupported = errors.New("not supported") | ||
|
||
// A StoreController is used by the goose package to interact with a database. This type is a | ||
// wrapper around the Store interface, but can be extended to include additional (optional) methods | ||
// that are not part of the core Store interface. | ||
type StoreController struct { | ||
store Store | ||
} | ||
|
||
var _ Store = (*StoreController)(nil) | ||
|
||
// NewStoreController returns a new StoreController that wraps the given Store. | ||
// | ||
// If the Store implements the following optional methods, the StoreController will call them as | ||
// appropriate: | ||
// | ||
// - TableExists(context.Context, DBTxConn) (bool, error) | ||
// | ||
// If the Store does not implement a method, it will either return a [ErrNotSupported] error or fall | ||
// back to the default behavior. | ||
func NewStoreController(store Store) *StoreController { | ||
return &StoreController{store: store} | ||
} | ||
|
||
// TableExists is an optional method that checks if the version table exists in the database. It is | ||
// recommended to implement this method if the database supports it, as it can be used to optimize | ||
// certain operations. | ||
func (c *StoreController) TableExists(ctx context.Context, db *sql.Conn) (bool, error) { | ||
if t, ok := c.store.(interface { | ||
TableExists(ctx context.Context, db *sql.Conn) (bool, error) | ||
}); ok { | ||
return t.TableExists(ctx, db) | ||
} | ||
return false, ErrNotSupported | ||
} | ||
|
||
// Default methods | ||
|
||
func (c *StoreController) Tablename() string { | ||
return c.store.Tablename() | ||
} | ||
|
||
func (c *StoreController) CreateVersionTable(ctx context.Context, db DBTxConn) error { | ||
return c.store.CreateVersionTable(ctx, db) | ||
} | ||
|
||
func (c *StoreController) Insert(ctx context.Context, db DBTxConn, req InsertRequest) error { | ||
return c.store.Insert(ctx, db, req) | ||
} | ||
|
||
func (c *StoreController) Delete(ctx context.Context, db DBTxConn, version int64) error { | ||
return c.store.Delete(ctx, db, version) | ||
} | ||
|
||
func (c *StoreController) GetMigration(ctx context.Context, db DBTxConn, version int64) (*GetMigrationResult, error) { | ||
return c.store.GetMigration(ctx, db, version) | ||
} | ||
|
||
func (c *StoreController) GetLatestVersion(ctx context.Context, db DBTxConn) (int64, error) { | ||
return c.store.GetLatestVersion(ctx, db) | ||
} | ||
|
||
func (c *StoreController) ListMigrations(ctx context.Context, db DBTxConn) ([]*ListMigrationsResult, error) { | ||
return c.store.ListMigrations(ctx, db) | ||
} |
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 |
---|---|---|
@@ -1,28 +1,71 @@ | ||
package dialectquery | ||
|
||
// Querier is the interface that wraps the basic methods to create a dialect | ||
// specific query. | ||
// Querier is the interface that wraps the basic methods to create a dialect specific query. | ||
type Querier interface { | ||
// CreateTable returns the SQL query string to create the db version table. | ||
CreateTable(tableName string) string | ||
|
||
// InsertVersion returns the SQL query string to insert a new version into | ||
// the db version table. | ||
// InsertVersion returns the SQL query string to insert a new version into the db version table. | ||
InsertVersion(tableName string) string | ||
|
||
// DeleteVersion returns the SQL query string to delete a version from | ||
// the db version table. | ||
// DeleteVersion returns the SQL query string to delete a version from the db version table. | ||
DeleteVersion(tableName string) string | ||
|
||
// GetMigrationByVersion returns the SQL query string to get a single | ||
// migration by version. | ||
// GetMigrationByVersion returns the SQL query string to get a single migration by version. | ||
// | ||
// The query should return the timestamp and is_applied columns. | ||
GetMigrationByVersion(tableName string) string | ||
|
||
// ListMigrations returns the SQL query string to list all migrations in | ||
// descending order by id. | ||
// ListMigrations returns the SQL query string to list all migrations in descending order by id. | ||
// | ||
// The query should return the version_id and is_applied columns. | ||
ListMigrations(tableName string) string | ||
} | ||
|
||
var _ Querier = (*QueryController)(nil) | ||
|
||
type QueryController struct { | ||
querier Querier | ||
} | ||
|
||
// NewQueryController returns a new QueryController that wraps the given Querier. | ||
func NewQueryController(querier Querier) *QueryController { | ||
return &QueryController{querier: querier} | ||
} | ||
|
||
// Optional methods | ||
|
||
// TableExists returns the SQL query string to check if the version table exists. If the Querier | ||
// does not implement this method, it will return an empty string. | ||
// | ||
// The query should return a boolean value. | ||
func (c *QueryController) TableExists(tableName string) string { | ||
if t, ok := c.querier.(interface { | ||
TableExists(string) string | ||
}); ok { | ||
return t.TableExists(tableName) | ||
} | ||
return "" | ||
} | ||
|
||
// Default methods | ||
|
||
func (c *QueryController) CreateTable(tableName string) string { | ||
return c.querier.CreateTable(tableName) | ||
} | ||
|
||
func (c *QueryController) InsertVersion(tableName string) string { | ||
return c.querier.InsertVersion(tableName) | ||
} | ||
|
||
func (c *QueryController) DeleteVersion(tableName string) string { | ||
return c.querier.DeleteVersion(tableName) | ||
} | ||
|
||
func (c *QueryController) GetMigrationByVersion(tableName string) string { | ||
return c.querier.GetMigrationByVersion(tableName) | ||
} | ||
|
||
func (c *QueryController) ListMigrations(tableName string) string { | ||
return c.querier.ListMigrations(tableName) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The downside with this pattern is external implementations don't have a type assertion to lean on, and so it's really easy to believe a method is implemented, but in reality the method signature doesn't match. You lose that nice compile-time check.
Another pattern could be to have a second interface like
StoreExtender
that's a superset ofStore
.There's pros/cons to both approaches, need to sleep on this a bit and chat with folks to get feedback.