Skip to content

Commit

Permalink
Check primary key data types
Browse files Browse the repository at this point in the history
  • Loading branch information
syucream committed May 11, 2018
1 parent bb3d4b4 commit 55e7e30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ FROM
WHERE
TABLE_SCHEMA IN (?);
`
primaryConstraintQueryBase = `SELECT
TABLE_NAME, COLUMN_NAME
primaryConstraintQueryBase = `SELECT
KEY_COLUMN_USAGE.TABLE_NAME, KEY_COLUMN_USAGE.COLUMN_NAME, COLUMNS.DATA_TYPE
FROM
KEY_COLUMN_USAGE
INNER JOIN
COLUMNS
ON
KEY_COLUMN_USAGE.TABLE_NAME = COLUMNS.TABLE_NAME AND
KEY_COLUMN_USAGE.COLUMN_NAME = COLUMNS.COLUMN_NAME
WHERE
CONSTRAINT_NAME = 'PRIMARY' AND
CONSTRAINT_SCHEMA IN (?);
KEY_COLUMN_USAGE.CONSTRAINT_NAME = 'PRIMARY' AND
KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA IN (?);
`
)

Expand All @@ -33,8 +38,9 @@ type Schema struct {
}

type PrimaryKey struct {
Table string
Column string
Table string
Column string
DataType string
}

func ConnectDatabase(user string, pass string, host string, port int) (*sql.DB, error) {
Expand Down Expand Up @@ -81,11 +87,11 @@ func FetchPrimaryKeys(db *sql.DB, targets []string) ([]PrimaryKey, error) {

var primaryKeys []PrimaryKey
for rows.Next() {
var tableName, columnName string
if err := rows.Scan(&tableName, &columnName); err != nil {
var tableName, columnName, dataType string
if err := rows.Scan(&tableName, &columnName, &dataType); err != nil {
return nil, err
}
primaryKeys = append(primaryKeys, PrimaryKey{tableName, columnName})
primaryKeys = append(primaryKeys, PrimaryKey{tableName, columnName, dataType})
}

return primaryKeys, nil
Expand Down
4 changes: 2 additions & 2 deletions src/guess/guess.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func isPrimaryKeyType(dataType string) bool {
// https://github.com/schemaspy/schemaspy/blob/master/src/main/java/org/schemaspy/DbAnalyzer.java
func GuessByPrimaryKey() GuessOption {
return func(s database.Schema, pk database.PrimaryKey) bool {
return isPrimaryKeyType(s.DataType) && s.Column == pk.Column && pk.Column != idColumn
return isPrimaryKeyType(pk.DataType) && s.DataType == pk.DataType && s.Column == pk.Column && pk.Column != idColumn
}
}

func GuessByTableAndColumn() GuessOption {
return func(s database.Schema, pk database.PrimaryKey) bool {
if !isPrimaryKeyType(s.DataType) {
if !isPrimaryKeyType(pk.DataType) && s.DataType != pk.DataType {
return false
}

Expand Down

0 comments on commit 55e7e30

Please sign in to comment.