Browse Source

check single table

tags/0.0.4
Vladimir Smagin 3 weeks ago
parent
commit
cf23e83d94
2 changed files with 22 additions and 4 deletions
  1. +3
    -1
      README.md
  2. +19
    -3
      main.go

+ 3
- 1
README.md View File

@@ -2,7 +2,9 @@

You have servers with master-slave(s) replication and want to be sure what all tables is synced. Program queries count of records for every table in database and shows to you on screen. Used on servers with logical replication and wal-g.

Create `config.yaml` with connection strings and run `compare-pg -config config.yaml`.
Create `config.yaml` with connection strings and run `compare-pg -config config.yaml`. If you want check only single table add argument `-table table_name`.

Sample config file:

```yaml
credentials:


+ 19
- 3
main.go View File

@@ -13,9 +13,10 @@ import (
)

var (
configFile = flag.String("config", "", "Config file")
logger *log.Logger
pg pgConnections
configFile = flag.String("config", "", "Config file")
singleTable = flag.String("table", "", "Table name")
logger *log.Logger
pg pgConnections
)

type config struct {
@@ -79,6 +80,7 @@ func getTableNames(db *sql.DB) []string {
resultSet := []string{}
if err != nil {
logger.Error(err)
os.Exit(1)
}
defer rows.Close()
for rows.Next() {
@@ -86,12 +88,14 @@ func getTableNames(db *sql.DB) []string {
err = rows.Scan(&tableName)
if err != nil {
logger.Error(err)
os.Exit(1)
}
resultSet = append(resultSet, tableName)
}
err = rows.Err()
if err != nil {
logger.Error(err)
os.Exit(1)
}
return resultSet
}
@@ -124,12 +128,14 @@ func main() {
db, err := sql.Open("postgres", pgConnectString)
if err != nil {
logger.Error(err)
os.Exit(1)
}
defer db.Close()

err = db.Ping()
if err != nil {
logger.Error(err)
os.Exit(1)
}
logger.Info(pgName, "successfully connected!")
pgConfigs[pgName] = db
@@ -147,6 +153,16 @@ func main() {
}
}

if *singleTable != "" {
if !inList(tablesList, *singleTable) {
// table not in database
logger.Error("Table not found")
os.Exit(1)
} else {
tablesList = []string{*singleTable}
}
}

// Show records count
for _, tableName := range tablesList {
logger.Info("Table name:", tableName)


Loading…
Cancel
Save