Skip to content

Commit

Permalink
Set db port automatically based on adapter in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-collins-voormedia committed Jan 3, 2024
1 parent 9759440 commit 6bdc9af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
cmdRestore.Flags().String("b2encrypt", b2encrypt, "Specifies the Backblaze B2 encryption key")
cmdRestore.Flags().String("b2bucket", b2bucketName, "Specifies the Backblaze B2 backup bucket")
cmdRestore.Flags().String("dbconfig", "./config/database.yml", "Specifies the location of the application's database configuration file")
cmdRestore.Flags().String("port", "3306", "Specifies the port to use when restoring the target database")
cmdRestore.Flags().String("port", "", "Specifies the port to use when restoring the target database")
cmdRestore.Flags().String("host", "127.0.0.1", "Specifies the host to use when restoring the target database.")
cmdRestore.Flags().String("user", "root", "Specifies the user to use when restoring the target database. Will be overriden by the database configuration file if applicable.")
cmdRestore.Flags().String("password", "", "Specifies the password to use when restoring the target database. Only used when the database flag is also specified.")
Expand Down
29 changes: 28 additions & 1 deletion pkg/util/dbconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type TargetConfig struct {
Hostname string
Port string
Adapter string
Database string
Username string
Password string
Expand Down Expand Up @@ -90,9 +91,35 @@ func GetDatabaseConfig(database string, environment string, shard string, user s
}

target.Hostname = host
target.Port = port
target.Environment = environment

if port == "" {
switch target.Adapter {
case "mysql2":
{
target.Port = "3306"
}
case "postgresql":
{
target.Port = "5432"
}
case "sqlserver":
{
target.Port = "1433"
}
case "oracle_enhanced":
{
target.Port = "1521"
}
default:
{
target.Port = "3306"
}
}
} else {
target.Port = port
}

if target.Database == "" {
return target, errors.Errorf("Could not find a database belonging to the target")
}
Expand Down

0 comments on commit 6bdc9af

Please sign in to comment.