mirror of https://github.com/status-im/migrate.git
Add sql.DB to Postgres and Mysql structs to allow closing in Close()
This commit is contained in:
parent
93d53a5ae8
commit
1e4c50b70d
|
@ -31,10 +31,10 @@ func init() {
|
||||||
var DefaultMigrationsTable = "schema_migrations"
|
var DefaultMigrationsTable = "schema_migrations"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrDatabaseDirty = fmt.Errorf("database is dirty")
|
ErrDatabaseDirty = fmt.Errorf("database is dirty")
|
||||||
ErrNilConfig = fmt.Errorf("no config")
|
ErrNilConfig = fmt.Errorf("no config")
|
||||||
ErrNoDatabaseName = fmt.Errorf("no database name")
|
ErrNoDatabaseName = fmt.Errorf("no database name")
|
||||||
ErrAppendPEM = fmt.Errorf("failed to append PEM")
|
ErrAppendPEM = fmt.Errorf("failed to append PEM")
|
||||||
ErrTLSCertKeyConfig = fmt.Errorf("To use TLS client authentication, both x-tls-cert and x-tls-key must not be empty")
|
ErrTLSCertKeyConfig = fmt.Errorf("To use TLS client authentication, both x-tls-cert and x-tls-key must not be empty")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,6 +47,7 @@ type Mysql struct {
|
||||||
// mysql RELEASE_LOCK must be called from the same conn, so
|
// mysql RELEASE_LOCK must be called from the same conn, so
|
||||||
// just do everything over a single conn anyway.
|
// just do everything over a single conn anyway.
|
||||||
conn *sql.Conn
|
conn *sql.Conn
|
||||||
|
db *sql.DB
|
||||||
isLocked bool
|
isLocked bool
|
||||||
|
|
||||||
config *Config
|
config *Config
|
||||||
|
@ -85,6 +86,7 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
|
||||||
|
|
||||||
mx := &Mysql{
|
mx := &Mysql{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
db: instance,
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +195,12 @@ func (m *Mysql) Open(url string) (database.Driver, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mysql) Close() 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 {
|
func (m *Mysql) Lock() error {
|
||||||
|
|
|
@ -40,6 +40,7 @@ type Config struct {
|
||||||
type Postgres struct {
|
type Postgres struct {
|
||||||
// Locking and unlocking need to use the same connection
|
// Locking and unlocking need to use the same connection
|
||||||
conn *sql.Conn
|
conn *sql.Conn
|
||||||
|
db *sql.DB
|
||||||
isLocked bool
|
isLocked bool
|
||||||
|
|
||||||
// Open and WithInstance need to garantuee that config is never nil
|
// 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{
|
px := &Postgres{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
db: instance,
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +119,12 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Postgres) Close() 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
|
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
|
||||||
|
|
Loading…
Reference in New Issue