Skip to content
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

Store in a raw json object #9

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading