Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mocks/authmocks/plugin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions mocks/crudmocks/crud.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions mocks/dbmigratemocks/driver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions mocks/httpservermocks/go_http_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mocks/wsservermocks/protocol.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions mocks/wsservermocks/web_socket_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions pkg/dbsql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"

"github.com/golang-migrate/migrate/v4/database"
// Import migrate file source
_ "github.com/golang-migrate/migrate/v4/source/file"
)
Expand Down Expand Up @@ -139,8 +140,17 @@ func (s *Database) RunAsGroup(ctx context.Context, fn func(ctx context.Context)
return s.CommitTx(ctx, tx, false /* we _are_ the auto-committer */)
}

func (s *Database) applyDBMigrations(ctx context.Context, config config.Section, provider Provider) error {
driver, err := provider.GetMigrationDriver(s.db)
func (s *Database) applyDBMigrations(ctx context.Context, config config.Section, provider Provider) (err error) {
var driver database.Driver
providerClosable, isClosable := provider.(ProviderCloseableMigrationDriver)
if isClosable {
driver, err = providerClosable.GetMigrationDriverClosable(s.db)
defer func() {
_ = driver.Close()
}()
} else {
driver, err = provider.GetMigrationDriver(s.db)
}
Comment on lines +144 to +153
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - think this explains behavior I saw in some of our tests where when closing the migrations, it closed the whole driver, and caused other obscure failures in the tests 😵

if err == nil {
fileURL := "file://" + config.GetString(SQLConfMigrationsDirectory)
log.L(ctx).Infof("Running migrations in: %s", fileURL)
Expand Down Expand Up @@ -184,7 +194,7 @@ func (s *Database) BeginOrUseTx(ctx context.Context) (ctx1 context.Context, tx *
ctx1 = log.WithLogger(ctx, l)
before := time.Now()
l.Tracef("SQL-> begin")
sqlTX, err := s.db.Begin()
sqlTX, err := s.db.BeginTx(ctx /* transaction should auto-rollback on context cancel */, nil /* default options */)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻 - this is what Opus was whispering in my ear about when analyzing this previously. Thanks makes perfect sense

if err != nil {
return ctx1, nil, false, i18n.WrapError(ctx1, err, i18n.MsgDBBeginFailed)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/dbsql/mock_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type MockProvider struct {
mmg *dbmigratemocks.Driver
}

type MockProviderConnScoped struct {
MockProvider
}

type MockProviderConfig struct {
FakePSQLInsert bool
OpenError error
Expand Down Expand Up @@ -108,3 +112,7 @@ func (mp *MockProvider) Open(_ string) (*sql.DB, error) {
func (mp *MockProvider) GetMigrationDriver(_ *sql.DB) (migratedb.Driver, error) {
return mp.mmg, mp.GetMigrationDriverError
}

func (mp *MockProviderConnScoped) GetMigrationDriverConn(_ *sql.DB) (migratedb.Driver, error) {
return mp.mmg, mp.GetMigrationDriverError
}
8 changes: 7 additions & 1 deletion pkg/dbsql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Provider interface {
// Open creates the DB instances
Open(url string) (*sql.DB, error)

// GetDriver returns the driver implementation
// GetDriver returns the driver implementation - preferred if supported to implement GetMigrationDriverConn so the connection can be cleaned up (cleaning this driver up closes the whole DB)
GetMigrationDriver(*sql.DB) (migratedb.Driver, error)

// Features returns database specific configuration switches
Expand All @@ -68,3 +68,9 @@ type Provider interface {
// ApplyInsertQueryCustomizations updates the INSERT query for returning the Sequence, and returns whether it needs to be run as a query to return the Sequence field
ApplyInsertQueryCustomizations(insert sq.InsertBuilder, requestConflictEmptyResult bool) (updatedInsert sq.InsertBuilder, runAsQuery bool)
}

// Implementing this interface allows cleanup of the connection used during migration
type ProviderCloseableMigrationDriver interface {
// Returns the driver implementation that is safe to close. Specifically this means the WithConnection() use in migration, rather than WithInstance().
GetMigrationDriverClosable(*sql.DB) (migratedb.Driver, error)
}
Loading