- Notifications
You must be signed in to change notification settings - Fork1k
Fix socket leak, clean up single use postgres databases#2413
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package database | ||
import ( | ||
"context" | ||
"database/sql" | ||
"embed" | ||
"errors" | ||
@@ -17,12 +18,21 @@ import ( | ||
var migrations embed.FS | ||
func migrateSetup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { | ||
ctx := context.Background() | ||
sourceDriver, err := iofs.New(migrations, "migrations") | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("create iofs: %w", err) | ||
} | ||
// there is a postgres.WithInstance() method that takes the DB instance, | ||
// but, when you close the resulting Migrate, it closes the DB, which | ||
// we don't want. Instead, create just a connection that will get closed | ||
// when migration is done. | ||
conn, err := db.Conn(ctx) | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("postgres connection: %w", err) | ||
} | ||
dbDriver, err := postgres.WithConnection(ctx, conn, &postgres.Config{}) | ||
if err != nil { | ||
return nil, nil, xerrors.Errorf("wrap postgres connection: %w", err) | ||
} | ||
@@ -36,11 +46,22 @@ func migrateSetup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { | ||
} | ||
// MigrateUp runs SQL migrations to ensure the database schema is up-to-date. | ||
func MigrateUp(db *sql.DB)(retErrerror) { | ||
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. Tip (non-blocking): It would be OK to call this | ||
_, m, err := migrateSetup(db) | ||
if err != nil { | ||
return xerrors.Errorf("migrate setup: %w", err) | ||
} | ||
defer func() { | ||
srcErr, dbErr := m.Close() | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if retErr != nil { | ||
return | ||
} | ||
if dbErr != nil { | ||
retErr = dbErr | ||
return | ||
} | ||
retErr = srcErr | ||
}() | ||
err = m.Up() | ||
if err != nil { | ||