MylesBock commented on a change in pull request #5327:
URL: https://github.com/apache/trafficcontrol/pull/5327#discussion_r529784550



##########
File path: traffic_ops/app/db/admin.go
##########
@@ -171,68 +173,73 @@ func parseDBConfig() error {
        return nil
 }
 
-func createDB() {
-       dbExistsCmd := exec.Command("psql", "-h", HostIP, "-U", DBSuperUser, 
"-p", HostPort, "-tAc", "SELECT 1 FROM pg_database WHERE datname='"+DBName+"'")
-       out, err := dbExistsCmd.Output()
-       if err != nil {
-               die("unable to check if DB already exists: " + err.Error())
-       }
-       if len(out) > 0 {
-               fmt.Println("Database " + DBName + " already exists")
-               return
-       }
-       createDBCmd := exec.Command("createdb", "-h", HostIP, "-p", HostPort, 
"-U", DBSuperUser, "-e", "--owner", DBUser, DBName)
-       out, err = createDBCmd.CombinedOutput()
-       fmt.Printf("%s", out)
-       if err != nil {
-               die("Can't create db " + DBName)
+func connectAndExecute(query string, queryErrPrefix string, returnRows bool) 
*sql.Rows {
+       //todo prepared statements
+       //todo inspect results of executing the above query for all possible 
cases (db exists, db dne, db inaccessible)
+       //todo determine test coverage of this portion
+       if db, err := dburl.Open(fmt.Sprintf("pg://%s:%s@%s:%s/", DBSuperUser, 
DBPassword, HostIP, HostPort)); err == nil {
+               if !returnRows {
+                       if _, err := db.Exec(query); err != nil {

Review comment:
       Correct, but in this case it's reading an entire file in for things like 
patch, seed, create and executing it all at once
   
   This has come up apparently in a few discussions on `lib/pq` and other 
repositories related to golang and postgres, and one project offers a solution 
(https://github.com/gchaincl/dotsql) but it requires more effort




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to