From f765985e2a003d83b1b08350ae19c257c770e509 Mon Sep 17 00:00:00 2001 From: Alex Steel <130377221+asteel-gsa@users.noreply.github.com> Date: Thu, 29 Aug 2024 08:46:06 -0400 Subject: [PATCH] Store in a raw json object {"TABLEROWCOUNT":[{"Table":"table_name","Rows":"#"},......]} --- cmd/row_count.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/cmd/row_count.go b/cmd/row_count.go index 04a280c..46eb66d 100644 --- a/cmd/row_count.go +++ b/cmd/row_count.go @@ -6,6 +6,7 @@ package cmd import ( "bufio" "database/sql" + "encoding/json" "fmt" "os" "strconv" @@ -20,6 +21,15 @@ var ( row_count_db string ) +type RowCount struct { + Table string `json:"Table"` + Rows string `json:"Rows"` +} + +type connection struct { + RowsCountConnection []*RowCount `json:"TABLEROWCOUNT"` +} + func check_rows_in_db(source_creds vcap.Credentials) { db, err := sql.Open("postgres", source_creds.Get("uri").String()) if err != nil { @@ -54,11 +64,29 @@ func check_rows_in_db(source_creds vcap.Credentials) { // logging.Logger.Printf(fmt.Sprintf("Table: %s | Row Count: %d\n", scanner.Text(), count)) r := strconv.Itoa(count) // Store in row_count_for_tables []string - row_count_for_tables = append(row_count_for_tables, "Table: "+scanner.Text()+" | Rows: "+r) + row_count_for_tables = append(row_count_for_tables, scanner.Text()+" "+r) } - logging.Logger.Println("Row count for tables in manifest...") + + logging.Logger.Printf("Row count for tables in manifest...") + joined_tables := strings.Join(row_count_for_tables[:], "\n") - logging.Logger.Printf("TABLEROWCOUNT\n" + joined_tables) + //logging.Logger.Printf("TABLEROWCOUNT " + joined_tables) + + var rows []*RowCount + for _, joined_tables := range strings.Split(joined_tables, "\n") { + if joined_tables != "" { + s := strings.Split(joined_tables, " ") + rows = append(rows, &RowCount{Table: s[0], Rows: s[1]}) + } + } + + // Raw json object of {"Table":"table_name","Rows":"#"} + raw, _ := json.Marshal(connection{RowsCountConnection: rows}) + logging.Logger.Printf("%s", raw) + // PrettyPrint json object of {"Table":"table_name","Rows":"#"} + //pretty, _ := json.MarshalIndent(connection{RowsCountConnection: rows}, "", " ") + //logging.Logger.Printf("%s\n", pretty) + if err := scanner.Err(); err != nil { logging.Error.Println(err) }