Mayank
Mayank

Reputation: 5569

golang-migrate Close() not closing connection

I am using golang-migrate to migrate schema. Connection open, migration up and down are working fine. However, db connection is not getting closed, not throwing any error and leave idle connection in database server. My code looks like this:

m, err := migrate.New(sourceURL, "database_connection_string")
defer m.Close()
m.Version()

Has anyone faced similar issue ? How can we address this ?

Original Code

Upvotes: 1

Views: 541

Answers (1)

Keith Stiern
Keith Stiern

Reputation: 61

The defer statement will ensure that the m.Close() call is executed after your method returns, so it should not matter where you place the m.Version() call in the method and any attempts to check whether it is closed from within the method will return a false negative.

I have not faced this issue but based on your experience both the Close and Version methods in golang-migrate are suspect. However, that is an open source project, so in your situation I would clone their code, call it from yours, and debug those methods to see what is happening. You will probably find some insights that will help you fix your code, but it is also possible that you find a bug in theirs.

Upvotes: 3

Related Questions