-
Notifications
You must be signed in to change notification settings - Fork 10
Cleanups postgres connections logic #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kunrex
wants to merge
8
commits into
sdslabs:bl4ze/dev
Choose a base branch
from
kunrex:cleanup-postgres
base: bl4ze/dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd9c2eb
Cleanup gorm errors clutter
kunrex fc742d3
Add graceful database close
kunrex a9b8b83
Clean terminate database connections
kunrex 5662c24
Fix postgres dns
kunrex 6693449
Remove unused var
kunrex 2e584c2
Cleanup Db var
kunrex 1fb2fb0
Fix formatting
kunrex 4fa83f8
Exec remove from reset database
kunrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ import ( | |
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/BurntSushi/toml" | ||
| _ "github.com/jackc/pgx/v5/stdlib" | ||
| "github.com/lib/pq" | ||
| "github.com/sdslabs/beastv4/core" | ||
|
|
@@ -125,11 +124,11 @@ func dbUserCheck() (bool, error) { | |
| func initDb() error { | ||
| log.Infoln("Initializing database...") | ||
|
|
||
| var configuration config.BeastConfig | ||
| _, err := toml.DecodeFile(BEAST_GLOBAL_CONFIG, &configuration) | ||
| err := config.ReloadBeastConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| configuration := config.Cfg.PsqlConf | ||
|
|
||
| isPostgres, err := dbUserCheck() | ||
| if err != nil { | ||
|
|
@@ -140,7 +139,7 @@ func initDb() error { | |
| if isPostgres { | ||
| log.Infoln("Attempting to connect to postgres as postgres super user...") | ||
|
|
||
| dsn := fmt.Sprintf("user=%s dbname=%s sslmode=%s", "postgres", "postgres", "disable") | ||
| dsn := fmt.Sprintf("user=%s dbname=%s host=%s port=%s sslmode=%s", "postgres", "postgres", configuration.Host, configuration.Port, "disable") | ||
| db, err = sql.Open("pgx", dsn) | ||
|
|
||
| if err != nil { | ||
|
|
@@ -152,7 +151,7 @@ func initDb() error { | |
| if utils.PromptBinary("Do you use password authentication for the postgres super user?") { | ||
| password := utils.PromptSecret("Enter postgres super user password (leave blank if none):") | ||
|
|
||
| dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s", "postgres", password, "postgres", "disable") | ||
| dsn := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=%s", "postgres", password, "postgres", configuration.Host, configuration.Port, "disable") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
| db, err = sql.Open("pgx", dsn) | ||
|
|
||
| if err != nil { | ||
|
|
@@ -167,41 +166,41 @@ func initDb() error { | |
| defer db.Close() | ||
|
|
||
| var exists int | ||
| err = db.QueryRow("SELECT 1 FROM pg_roles WHERE rolname = $1", configuration.PsqlConf.User).Scan(&exists) | ||
| err = db.QueryRow("SELECT 1 FROM pg_roles WHERE rolname = $1", configuration.User).Scan(&exists) | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| if err = createBeastDbUser(db, &configuration.PsqlConf); err != nil { | ||
| if err = createBeastDbUser(db, &configuration); err != nil { | ||
| return err | ||
| } | ||
| } else if err != nil { | ||
| return err | ||
| } else { | ||
| log.Infoln(fmt.Sprintf("User %s already exists", configuration.PsqlConf.User)) | ||
| log.Infoln(fmt.Sprintf("User %s already exists", configuration.User)) | ||
| } | ||
|
|
||
| log.Infoln(fmt.Sprintf("Changing password for user %s", configuration.PsqlConf.User)) | ||
| query := fmt.Sprintf("ALTER USER %s WITH PASSWORD %s", pq.QuoteIdentifier(configuration.PsqlConf.User), utils.QuoteLiteral(configuration.PsqlConf.Password)) | ||
| log.Infoln(fmt.Sprintf("Changing password for user %s", configuration.User)) | ||
| query := fmt.Sprintf("ALTER USER %s WITH PASSWORD %s", pq.QuoteIdentifier(configuration.User), utils.QuoteLiteral(configuration.Password)) | ||
| _, err = db.Exec(query) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = db.QueryRow("SELECT 1 FROM pg_database WHERE datname = $1", configuration.PsqlConf.Dbname).Scan(&exists) | ||
| err = db.QueryRow("SELECT 1 FROM pg_database WHERE datname = $1", configuration.Dbname).Scan(&exists) | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| if err = createBeastDatabase(db, &configuration.PsqlConf); err != nil { | ||
| if err = createBeastDatabase(db, &configuration); err != nil { | ||
| return err | ||
| } | ||
| } else if err != nil { | ||
| return err | ||
| } else { | ||
| log.Infoln(fmt.Sprintf("Database %s already exists", configuration.PsqlConf.Dbname)) | ||
| log.Infoln(fmt.Sprintf("Database %s already exists", configuration.Dbname)) | ||
| } | ||
|
|
||
| _, err = db.Exec(fmt.Sprintf("ALTER DATABASE %s OWNER TO %s", pq.QuoteIdentifier(configuration.PsqlConf.Dbname), pq.QuoteIdentifier(configuration.PsqlConf.User))) | ||
| _, err = db.Exec(fmt.Sprintf("ALTER DATABASE %s OWNER TO %s", pq.QuoteIdentifier(configuration.Dbname), pq.QuoteIdentifier(configuration.User))) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| log.Infoln(fmt.Sprintf("%s set as owner of database %s", configuration.PsqlConf.User, configuration.PsqlConf.Dbname)) | ||
| log.Infoln(fmt.Sprintf("%s set as owner of database %s", configuration.User, configuration.Dbname)) | ||
| return nil | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can add user adn dbname from config too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is intended, at this point the db and user dont exist, so have to connect to postgres database as postgres first to create them.