-
Notifications
You must be signed in to change notification settings - Fork 33
fix(ers): lazily register database/sql driver in SQL ERS provider #3543
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
Open
jp-ayyappan
wants to merge
3
commits into
main
Choose a base branch
from
fix/ers-sql-provider-register-postgres-driver
base: main
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
4828bd8
fix(ers): lazily register database/sql driver in SQL ERS provider
jp-ayyappan b2cf821
fix(ers): normalize driver name to lowercase before registration checks
jp-ayyappan 0eed6d9
fix(ers): normalize config.Driver in NewProvider before sql.Open
jp-ayyappan 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 hidden or 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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,15 +5,57 @@ | |||||||||||||||||||
| "database/sql" | ||||||||||||||||||||
| "fmt" | ||||||||||||||||||||
| "strings" | ||||||||||||||||||||
| "sync" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Database drivers would be imported here: | ||||||||||||||||||||
| // _ "github.com/lib/pq" // PostgreSQL driver | ||||||||||||||||||||
| // _ "github.com/go-sql-driver/mysql" // MySQL driver | ||||||||||||||||||||
| // _ "github.com/mattn/go-sqlite3" // SQLite driver | ||||||||||||||||||||
|
|
||||||||||||||||||||
| "github.com/jackc/pgx/v5/stdlib" | ||||||||||||||||||||
| "github.com/opentdf/platform/service/entityresolution/multi-strategy/types" | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| var ( | ||||||||||||||||||||
| // driverRegMu guards lazy driver registration to prevent duplicate-register panics. | ||||||||||||||||||||
| driverRegMu sync.Mutex | ||||||||||||||||||||
| registeredDrivers = make(map[string]struct{}) | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // ensureDriverRegistered lazily registers the named database/sql driver the first | ||||||||||||||||||||
| // time a SQL provider for that driver is created. This avoids the need for | ||||||||||||||||||||
| // consumers to add blank driver imports to their own binaries. | ||||||||||||||||||||
| // | ||||||||||||||||||||
| // Uses pgx/v5/stdlib for postgres (already a platform dependency). Other drivers | ||||||||||||||||||||
| // (mysql, sqlite) are not currently auto-registered and must be imported by the | ||||||||||||||||||||
| // consumer. Consumers that have already registered the driver themselves are | ||||||||||||||||||||
| // handled gracefully via a sql.Drivers() pre-check. | ||||||||||||||||||||
| func ensureDriverRegistered(driver string) { | ||||||||||||||||||||
| // Normalize to lowercase so "Postgres", "POSTGRES", and "postgres" all resolve | ||||||||||||||||||||
| // to the same registered driver name. sql.Register is case-sensitive. | ||||||||||||||||||||
| driver = strings.ToLower(strings.TrimSpace(driver)) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| driverRegMu.Lock() | ||||||||||||||||||||
| defer driverRegMu.Unlock() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if _, ok := registeredDrivers[driver]; ok { | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Check whether the driver was already registered externally (e.g. via a | ||||||||||||||||||||
| // blank import in the consumer binary) before attempting to register it. | ||||||||||||||||||||
| // Use strings.EqualFold so the pre-check is also case-insensitive. | ||||||||||||||||||||
| for _, d := range sql.Drivers() { | ||||||||||||||||||||
| if strings.EqualFold(d, driver) { | ||||||||||||||||||||
| registeredDrivers[driver] = struct{}{} | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| switch driver { | ||||||||||||||||||||
| case "postgres": | ||||||||||||||||||||
| sql.Register("postgres", stdlib.GetDefaultDriver()) | ||||||||||||||||||||
| registeredDrivers[driver] = struct{}{} | ||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| // mysql and sqlite require imports not present in this module's dependencies. | ||||||||||||||||||||
| // Add cases here when those drivers are added to go.mod. | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Provider implements the Provider interface for SQL databases | ||||||||||||||||||||
| type Provider struct { | ||||||||||||||||||||
| name string | ||||||||||||||||||||
|
|
@@ -24,6 +66,15 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| // NewProvider creates a new SQL provider | ||||||||||||||||||||
| func NewProvider(ctx context.Context, name string, config Config) (*Provider, error) { | ||||||||||||||||||||
| // Normalize the driver name so "Postgres", "POSTGRES", and "postgres" all | ||||||||||||||||||||
| // resolve correctly through ensureDriverRegistered and sql.Open, both of | ||||||||||||||||||||
| // which use case-sensitive driver name matching. | ||||||||||||||||||||
| config.Driver = strings.ToLower(strings.TrimSpace(config.Driver)) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Register the database/sql driver for this provider's configured driver name | ||||||||||||||||||||
| // if it has not already been registered. | ||||||||||||||||||||
| ensureDriverRegistered(config.Driver) | ||||||||||||||||||||
|
Comment on lines
68
to
+76
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Normalizing
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| provider := &Provider{ | ||||||||||||||||||||
| name: name, | ||||||||||||||||||||
| config: config, | ||||||||||||||||||||
|
|
||||||||||||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.