Skip to content

Commit

Permalink
feat: Block Target DB versions less than 9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
13rac1 committed Apr 16, 2020
1 parent 9f23ed3 commit d58f781
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmd/axon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func main() {
logger.WithError(err).Fatal("unable to connect to target database")
}

err = checkTargetVersion(targetDBConn)
if err != nil {
logger.WithError(err).Fatal("unable to use to target database")
}

err = loadPrimaryKeys(targetDBConn)
if err != nil {
logger.WithError(err).Fatal("unable to load target DB primary keys")
Expand Down
35 changes: 31 additions & 4 deletions cmd/axon/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
Expand All @@ -11,17 +14,41 @@ import (
// maps primary key columns by table
var primaryKeys = make(map[string][]string)

func checkTargetVersion(conn *sqlx.DB) error {
var serverVersion string
err := conn.QueryRow("SHOW server_version;").Scan(&serverVersion)
if err != nil {
return err
}

version := strings.Split(serverVersion, ".")
// TODO: More thorough version checks
major, err := strconv.Atoi(version[00])
if err != nil {
return err
}
minor, err := strconv.Atoi(version[1])
if err != nil {
return err
}
if major == 9 && minor < 5 {
return fmt.Errorf("Target DB Unsupported Version: %s", serverVersion)
}
log.Printf("Target DB Version: %s", serverVersion)
return nil
}

func loadPrimaryKeys(conn *sqlx.DB) error {
var rows []struct {
TableName string `db:"table_name"`
PrimaryKey pq.StringArray `db:"primary_key"`
}
err := conn.Select(&rows, `
SELECT
t.table_name,
SELECT
t.table_name,
string_to_array(string_agg(c.column_name, ','), ',') AS primary_key
FROM information_schema.key_column_usage AS c
LEFT JOIN information_schema.table_constraints AS t
FROM information_schema.key_column_usage AS c
LEFT JOIN information_schema.table_constraints AS t
ON c.constraint_name = t.constraint_name
AND t.constraint_type = 'PRIMARY KEY'
WHERE t.table_name != ''
Expand Down

0 comments on commit d58f781

Please sign in to comment.