mirror of https://github.com/status-im/migrate.git
Merge pull request #98 from Kay-Zee/Kay-Zee/close-sql-db-connections-on-driver-close
Add sql.DB to Postgres and Mysql structs to allow closing in Close()
This commit is contained in:
commit
f8c85735f4
|
@ -47,6 +47,7 @@ type Mysql struct {
|
|||
// mysql RELEASE_LOCK must be called from the same conn, so
|
||||
// just do everything over a single conn anyway.
|
||||
conn *sql.Conn
|
||||
db *sql.DB
|
||||
isLocked bool
|
||||
|
||||
config *Config
|
||||
|
@ -85,6 +86,7 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
|
|||
|
||||
mx := &Mysql{
|
||||
conn: conn,
|
||||
db: instance,
|
||||
config: config,
|
||||
}
|
||||
|
||||
|
@ -193,7 +195,12 @@ func (m *Mysql) Open(url string) (database.Driver, error) {
|
|||
}
|
||||
|
||||
func (m *Mysql) Close() error {
|
||||
return m.conn.Close()
|
||||
connErr := m.conn.Close()
|
||||
dbErr := m.db.Close()
|
||||
if connErr != nil || dbErr != nil {
|
||||
return fmt.Errorf("conn: %v, db: %v", connErr, dbErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mysql) Lock() error {
|
||||
|
|
|
@ -40,6 +40,7 @@ type Config struct {
|
|||
type Postgres struct {
|
||||
// Locking and unlocking need to use the same connection
|
||||
conn *sql.Conn
|
||||
db *sql.DB
|
||||
isLocked bool
|
||||
|
||||
// Open and WithInstance need to garantuee that config is never nil
|
||||
|
@ -79,6 +80,7 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
|
|||
|
||||
px := &Postgres{
|
||||
conn: conn,
|
||||
db: instance,
|
||||
config: config,
|
||||
}
|
||||
|
||||
|
@ -117,7 +119,12 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
|
|||
}
|
||||
|
||||
func (p *Postgres) Close() error {
|
||||
return p.conn.Close()
|
||||
connErr := p.conn.Close()
|
||||
dbErr := p.db.Close()
|
||||
if connErr != nil || dbErr != nil {
|
||||
return fmt.Errorf("conn: %v, db: %v", connErr, dbErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
|
||||
|
|
Loading…
Reference in New Issue