You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2020/11/24 18:07:28 UTC

[GitHub] [trafficcontrol] rawlinp commented on a change in pull request #5327: Remove use of `psql` from `traffic_ops/db/admin`

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



##########
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:
       So some of our files like `seeds.sql` contain multiple sql queries. Doesn't this only execute a single sql query?




----------------------------------------------------------------
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:
users@infra.apache.org