Skip to content

Commit

Permalink
Add GuessByRegexp.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabokuro committed Oct 11, 2018
1 parent 07429be commit b118897
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/hakagi/hakagi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
var ruleToGuesser = map[string]guess.GuessOption{
"primarykey": guess.GuessByPrimaryKey(),
"tableandcolumn": guess.GuessByTableAndColumn(),
"regexp": guess.GuessByRegexp([]guess.ForeignKeyRegexp{
guess.NewForeignKeyRegexp("^(seller|buyer|invited_friend|from_user|to_user|extracted_user|inviter_user|invitee_user|friend|reporter|reportee|related_user|invited_friend|start|user|last_user)_id$", "users", "id"),
guess.NewForeignKeyRegexp("^(require|coupon_attached|child)_item_id$", "items", "id"),
}),
}

func main() {
Expand All @@ -24,7 +28,7 @@ func main() {
dbPort := flag.Int("dbport", 3306, "database port")

targets := flag.String("targets", "", "analysing target databases(comma-separated)")
rules := flag.String("rules", "primarykey,tableandcolumn", "analysing rules(comma-separated)")
rules := flag.String("rules", "primarykey,tableandcolumn,regexp", "analysing rules(comma-separated)")

flag.Parse()

Expand Down
34 changes: 30 additions & 4 deletions src/guess/guess.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package guess

import (
"strings"
"regexp"

"github.com/jinzhu/inflection"
"github.com/syucream/hakagi/src/constraint"
Expand All @@ -15,10 +15,23 @@ const (

type GuessOption func(database.Column, string, database.Column) bool

type ForeignKeyRegexp struct {
regexp *regexp.Regexp
table string
column string
}

func NewForeignKeyRegexp(pattern string, table string, column string) ForeignKeyRegexp {
return ForeignKeyRegexp{regexp.MustCompile(pattern), table, column}
}

func isAcceptableAsPrimaryKey(columnType, primaryKeyType string) bool {
colIsOk := strings.Index(columnType, "int") != -1
pkIsOk := strings.Index(primaryKeyType, "int") != -1
return colIsOk && pkIsOk && columnType == primaryKeyType
/*
colIsOk := strings.Index(columnType, "int") != -1
pkIsOk := strings.Index(primaryKeyType, "int") != -1
return colIsOk && pkIsOk && columnType == primaryKeyType
*/
return columnType == primaryKeyType
}

// Recongnize a column thats same name of other table's primary key is a foreign key
Expand Down Expand Up @@ -46,6 +59,19 @@ func GuessByTableAndColumn() GuessOption {
}
}

func GuessByRegexp(foreignKeyRegexpList []ForeignKeyRegexp) GuessOption {
return func(i database.Column, table string, pk database.Column) bool {
for _, foreignKeyRegexp := range foreignKeyRegexpList {
if table == foreignKeyRegexp.table &&
pk.Name == foreignKeyRegexp.column &&
foreignKeyRegexp.regexp.MatchString(i.Name) {
return true
}
}
return false
}
}

// GuessConstraints guesses foreign key constraints from primary keys and indexes.
// NOTE composite primary keys are not supported.
func GuessConstraints(indexes database.Indexes, primaryKeys database.PrimaryKeys, guessOptions ...GuessOption) []constraint.Constraint {
Expand Down

0 comments on commit b118897

Please sign in to comment.