Skip to content

Commit

Permalink
Only populate db config values if it's not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
dhui committed Oct 14, 2019
1 parent 57b2ba7 commit 5249df0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
20 changes: 11 additions & 9 deletions database/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
return nil, err
}

query := `SELECT current_database()`
var databaseName string
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if config.DatabaseName == "" {
query := `SELECT current_database()`
var databaseName string
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
}
if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
}

config.DatabaseName = databaseName
config.DatabaseName = databaseName
}

if len(config.MigrationsTable) == 0 {
config.MigrationsTable = DefaultMigrationsTable
Expand Down
20 changes: 11 additions & 9 deletions database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
return nil, err
}

query := `SELECT DATABASE()`
var databaseName sql.NullString
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if config.DatabaseName == "" {
query := `SELECT DATABASE()`
var databaseName sql.NullString
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(databaseName.String) == 0 {
return nil, ErrNoDatabaseName
}
if len(databaseName.String) == 0 {
return nil, ErrNoDatabaseName
}

config.DatabaseName = databaseName.String
config.DatabaseName = databaseName.String
}

if len(config.MigrationsTable) == 0 {
config.MigrationsTable = DefaultMigrationsTable
Expand Down
20 changes: 11 additions & 9 deletions database/redshift/redshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
return nil, err
}

query := `SELECT CURRENT_DATABASE()`
var databaseName string
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if config.DatabaseName == "" {
query := `SELECT CURRENT_DATABASE()`
var databaseName string
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
}
if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
}

config.DatabaseName = databaseName
config.DatabaseName = databaseName
}

if len(config.MigrationsTable) == 0 {
config.MigrationsTable = DefaultMigrationsTable
Expand Down
38 changes: 21 additions & 17 deletions database/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,34 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
return nil, err
}

query := `SELECT DB_NAME()`
var databaseName string
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if config.DatabaseName == "" {
query := `SELECT DB_NAME()`
var databaseName string
if err := instance.QueryRow(query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
}

if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
config.DatabaseName = databaseName
}

config.DatabaseName = databaseName
if config.SchemaName == "" {
query := `SELECT SCHEMA_NAME()`
var schemaName string
if err := instance.QueryRow(query).Scan(&schemaName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}

query = `SELECT SCHEMA_NAME()`
var schemaName string
if err := instance.QueryRow(query).Scan(&schemaName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if len(schemaName) == 0 {
return nil, ErrNoSchema
}

if len(schemaName) == 0 {
return nil, ErrNoSchema
config.SchemaName = schemaName
}

config.SchemaName = schemaName

if len(config.MigrationsTable) == 0 {
config.MigrationsTable = DefaultMigrationsTable
}
Expand Down

0 comments on commit 5249df0

Please sign in to comment.