diff --git a/README.md b/README.md index e314595..4fb17e1 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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 @@ -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") diff --git a/db/schema.go b/db/schema.go index 0afeb4f..f2c9835 100644 --- a/db/schema.go +++ b/db/schema.go @@ -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) } diff --git a/db/schema_test.go b/db/schema_test.go index ba79473..2e4772e 100644 --- a/db/schema_test.go +++ b/db/schema_test.go @@ -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 }{ { @@ -58,6 +62,9 @@ func TestSchema_ToErd(t *testing.T) { }, }, }, + args: args{ + showIndex: true, + }, want: `entity articles { * id : integer -- @@ -101,6 +108,9 @@ articles }-- users`, }, }, }, + args: args{ + showIndex: true, + }, want: `entity articles { * id : integer -- @@ -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) }) } diff --git a/db/table.go b/db/table.go index 1feabee..d7e735f 100644 --- a/db/table.go +++ b/db/table.go @@ -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), } @@ -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()) diff --git a/db/table_test.go b/db/table_test.go index 19f5493..6b59a7f 100644 --- a/db/table_test.go +++ b/db/table_test.go @@ -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 }{ { @@ -38,6 +42,9 @@ func TestTable_ToErd(t *testing.T) { }, }, }, + args: args{ + showIndex: true, + }, want: `entity articles { * id : integer * user_id : integer @@ -66,6 +73,9 @@ func TestTable_ToErd(t *testing.T) { }, }, }, + args: args{ + showIndex: true, + }, want: `entity articles { * id : integer -- @@ -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{ @@ -120,6 +130,9 @@ func TestTable_ToErd(t *testing.T) { }, }, }, + args: args{ + showIndex: true, + }, want: `entity followers { * id : integer -- @@ -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 }`, }, } @@ -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) }) } diff --git a/erd_generator.go b/erd_generator.go index bd8d256..deb1766 100644 --- a/erd_generator.go +++ b/erd_generator.go @@ -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 @@ -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 { diff --git a/main.go b/main.go index 3ec25d4..5932a9a 100644 --- a/main.go +++ b/main.go @@ -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 := ""