Skip to content

Commit 5569136

Browse files
committed
Guess by similar table/column names
1 parent 9e0fa79 commit 5569136

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/hakagi/hakagi.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
_ "github.com/go-sql-driver/mysql"
10+
"github.com/syucream/hakagi/src/constraint"
1011
"github.com/syucream/hakagi/src/database"
1112
"github.com/syucream/hakagi/src/formatter"
1213
"github.com/syucream/hakagi/src/guess"
@@ -37,7 +38,9 @@ func main() {
3738
log.Fatalf("Failed to fetch primary keys : %v", err)
3839
}
3940

40-
constraints := guess.GuessByPrimaryKeyName(schemas, primaryKeys)
41+
var constraints []constraint.Constraint
42+
constraints = append(constraints, guess.GuessByPrimaryKeyName(schemas, primaryKeys)...)
43+
constraints = append(constraints, guess.GuessBySimilarColumn(schemas)...)
4144

4245
alterQuery := formatter.FormatSql(constraints)
4346
fmt.Println(alterQuery)

src/guess/guess.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package guess
22

33
import (
4+
"github.com/jinzhu/inflection"
45
"github.com/syucream/hakagi/src/constraint"
56
"github.com/syucream/hakagi/src/database"
67
)
78

89
const (
10+
idColumn = "id"
911
targetPrimaryKeySuffix = "_id"
1012
)
1113

@@ -15,6 +17,11 @@ func isTargetPrimaryKey(column string) bool {
1517
return cLen >= tLen && column[cLen-tLen:] == targetPrimaryKeySuffix
1618
}
1719

20+
func isSimilarColumn(left, right database.Schema) bool {
21+
maybeRightTable := inflection.Plural(left.Column)
22+
return maybeRightTable == right.Table && right.Column == idColumn
23+
}
24+
1825
// Recongnize a column thats same name of other table's primary key is a foreign key
1926
// This base idea refers to SchemaSpy DbAnalyzer:
2027
// https://github.com/schemaspy/schemaspy/blob/master/src/main/java/org/schemaspy/DbAnalyzer.java
@@ -31,3 +38,17 @@ func GuessByPrimaryKeyName(schemas []database.Schema, primaryKeys []database.Pri
3138

3239
return constraints
3340
}
41+
42+
func GuessBySimilarColumn(schemas []database.Schema) []constraint.Constraint {
43+
var constraints []constraint.Constraint
44+
45+
for _, left := range schemas {
46+
for _, right := range schemas {
47+
if left.Table != right.Table && isSimilarColumn(left, right) {
48+
constraints = append(constraints, constraint.Constraint{left.Table, left.Column, right.Table, right.Column})
49+
}
50+
}
51+
}
52+
53+
return constraints
54+
}

0 commit comments

Comments
 (0)