Skip to content

Commit

Permalink
Merge pull request #9 from GSA-TTS/row-count-format-patch
Browse files Browse the repository at this point in the history
Store in a raw json object
  • Loading branch information
jadudm authored Aug 29, 2024
2 parents d0f54e0 + f765985 commit 157a7ea
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions cmd/row_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd
import (
"bufio"
"database/sql"
"encoding/json"
"fmt"
"os"
"strconv"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 157a7ea

Please sign in to comment.