Skip to content

Commit

Permalink
Merge pull request #42 from sue445/feature/skip_index_option
Browse files Browse the repository at this point in the history
Add --skip-index
  • Loading branch information
sue445 authored Dec 11, 2019
2 parents 61301e4 + f28d7b6 commit a6d0b0a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ OPTIONS:
-d DISTANCE, --distance DISTANCE Output only tables within a certain DISTANCE adjacent to each other with foreign keys from a specific table (default: 0)
--database DATABASE SQLite3 DATABASE file
-f FILE, --file FILE FILE for output (default: stdout)
-i, --skip-index Whether don't print index to ERD
-t TABLE, --table TABLE Output only tables within a certain distance adjacent to each other with foreign keys from a specific TABLE
```

Expand All @@ -70,6 +71,7 @@ OPTIONS:
--database DATABASE MySQL DATABASE name
-f FILE, --file FILE FILE for output (default: stdout)
--host HOST MySQL HOST (default: "localhost")
-i, --skip-index Whether don't print index to ERD
--password PASSWORD MySQL PASSWORD [$MYSQL_PASSWORD]
--port PORT MySQL PORT (default: 3306)
-t TABLE, --table TABLE Output only tables within a certain distance adjacent to each other with foreign keys from a specific TABLE
Expand All @@ -90,6 +92,7 @@ OPTIONS:
--database DATABASE PostgreSQL DATABASE name
-f FILE, --file FILE FILE for output (default: stdout)
--host HOST PostgreSQL HOST (default: "localhost")
-i, --skip-index Whether don't print index to ERD
--password PASSWORD PostgreSQL PASSWORD [$POSTGRES_PASSWORD]
--port PORT PostgreSQL PORT (default: 5432)
--sslmode SSLMODE PostgreSQL SSLMODE. c.f. https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS (default: "disable")
Expand Down
4 changes: 2 additions & 2 deletions db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func NewSchema(tables []*Table) *Schema {
}

// ToErd returns ERD formatted schema
func (s *Schema) ToErd() string {
func (s *Schema) ToErd(showIndex bool) string {
var lines []string
tableNames := mapset.NewSet()

for _, table := range s.Tables {
lines = append(lines, table.ToErd())
lines = append(lines, table.ToErd(showIndex))
tableNames.Add(table.Name)
}

Expand Down
12 changes: 11 additions & 1 deletion db/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ func TestSchema_ToErd(t *testing.T) {
type fields struct {
Tables []*Table
}
type args struct {
showIndex bool
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
Expand Down Expand Up @@ -58,6 +62,9 @@ func TestSchema_ToErd(t *testing.T) {
},
},
},
args: args{
showIndex: true,
},
want: `entity articles {
* id : integer
--
Expand Down Expand Up @@ -101,6 +108,9 @@ articles }-- users`,
},
},
},
args: args{
showIndex: true,
},
want: `entity articles {
* id : integer
--
Expand All @@ -114,7 +124,7 @@ articles }-- users`,
Tables: tt.fields.Tables,
}

got := s.ToErd()
got := s.ToErd(tt.args.showIndex)
assert.Equal(t, tt.want, got)
})
}
Expand Down
4 changes: 2 additions & 2 deletions db/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Table struct {
}

// ToErd returns ERD formatted table
func (t *Table) ToErd() string {
func (t *Table) ToErd(showIndex bool) string {
lines := []string{
fmt.Sprintf("entity %s {", t.Name),
}
Expand All @@ -40,7 +40,7 @@ func (t *Table) ToErd() string {
area = append(area, strings.Join(parts, "\n"))
}

if len(t.Indexes) > 0 {
if showIndex && len(t.Indexes) > 0 {
var parts []string
for _, index := range t.Indexes {
parts = append(parts, " "+index.ToErd())
Expand Down
74 changes: 72 additions & 2 deletions db/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ func TestTable_ToErd(t *testing.T) {
ForeignKeys []*ForeignKey
Indexes []*Index
}
type args struct {
showIndex bool
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
Expand All @@ -38,6 +42,9 @@ func TestTable_ToErd(t *testing.T) {
},
},
},
args: args{
showIndex: true,
},
want: `entity articles {
* id : integer
* user_id : integer
Expand Down Expand Up @@ -66,6 +73,9 @@ func TestTable_ToErd(t *testing.T) {
},
},
},
args: args{
showIndex: true,
},
want: `entity articles {
* id : integer
--
Expand All @@ -74,7 +84,7 @@ func TestTable_ToErd(t *testing.T) {
}`,
},
{
name: "with index",
name: "with index and enabled showIndex",
fields: fields{
Name: "followers",
Columns: []*Column{
Expand Down Expand Up @@ -120,6 +130,9 @@ func TestTable_ToErd(t *testing.T) {
},
},
},
args: args{
showIndex: true,
},
want: `entity followers {
* id : integer
--
Expand All @@ -128,6 +141,63 @@ func TestTable_ToErd(t *testing.T) {
--
- index_target_user_id_and_user_id_on_followers (target_user_id, user_id)
index_user_id_and_target_user_id_on_followers (user_id, target_user_id)
}`,
},
{
name: "with index and disabled showIndex",
fields: fields{
Name: "followers",
Columns: []*Column{
{
Name: "id",
Type: "integer",
NotNull: true,
PrimaryKey: true,
},
{
Name: "user_id",
Type: "integer",
NotNull: true,
},
{
Name: "target_user_id",
Type: "integer",
NotNull: true,
},
},
ForeignKeys: []*ForeignKey{
{
FromColumn: "target_user_id",
ToTable: "users",
ToColumn: "id",
},
{
FromColumn: "user_id",
ToTable: "users",
ToColumn: "id",
},
},
Indexes: []*Index{
{
Name: "index_target_user_id_and_user_id_on_followers",
Columns: []string{"target_user_id", "user_id"},
Unique: true,
},
{
Name: "index_user_id_and_target_user_id_on_followers",
Columns: []string{"user_id", "target_user_id"},
Unique: false,
},
},
},
args: args{
showIndex: false,
},
want: `entity followers {
* id : integer
--
* user_id : integer
* target_user_id : integer
}`,
},
}
Expand All @@ -140,7 +210,7 @@ func TestTable_ToErd(t *testing.T) {
Indexes: tt.fields.Indexes,
}

got := table.ToErd()
got := table.ToErd(tt.args.showIndex)
assert.Equal(t, tt.want, got)
})
}
Expand Down
11 changes: 6 additions & 5 deletions erd_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

// ErdGenerator represents ERD generator
type ErdGenerator struct {
Filepath string
Table string
Distance int
Filepath string
Table string
Distance int
SKipIndex bool
}

// Run performs generator
Expand Down Expand Up @@ -42,11 +43,11 @@ func (g *ErdGenerator) checkParamTable(schema *db.Schema) error {

func (g *ErdGenerator) generate(schema *db.Schema) string {
if g.Table == "" || g.Distance <= 0 {
return schema.ToErd()
return schema.ToErd(!g.SKipIndex)
}

subset := schema.Subset(g.Table, g.Distance)
return subset.ToErd()
return subset.ToErd(!g.SKipIndex)
}

func (g *ErdGenerator) outputErd(content string) error {
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func main() {
Destination: &generator.Distance,
Value: 0,
},
cli.BoolFlag{
Name: "i,skip-index",
Usage: "Whether don't print index to ERD",
Required: false,
Destination: &generator.SKipIndex,
},
}

sqlite3Database := ""
Expand Down

0 comments on commit a6d0b0a

Please sign in to comment.