Skip to content

Commit

Permalink
chore: improve artisan db:show,db:table command display information
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Dec 31, 2024
1 parent b202f13 commit 28f486f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
10 changes: 6 additions & 4 deletions contracts/database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ type Index struct {
}

type Table struct {
Comment string
Name string
Schema string
Size int
Comment string
Name string
Schema string
Collation string
Engine string
Size int
}

type Type struct {
Expand Down
5 changes: 3 additions & 2 deletions database/console/show_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (r *ShowCommand) getDataBaseInfo() (name, version, openConnections string,
version = versionResult.Value
if strings.Contains(version, "MariaDB") {
name = "MariaDB"
version = strings.Trim(version[:strings.Index(version, "MariaDB")], "-")
}
if len(driver.openConnectionsQuery) > 0 {
var openConnectionsResult queryResult
Expand Down Expand Up @@ -167,11 +168,11 @@ func (r *ShowCommand) display(ctx console.Context, info databaseInfo) {
}
return
}(); size > 0 {
ctx.TwoColumnDetail("Total Size", fmt.Sprintf("%.3fMiB", float64(size)/1024/1024))
ctx.TwoColumnDetail("Total Size", fmt.Sprintf("%.3f MB", float64(size)/1024/1024))
}
ctx.NewLine()
if len(info.Tables) > 0 {
ctx.TwoColumnDetail("<fg=green;op=bold>Tables</>", "<fg=yellow;op=bold>Size (MiB)</>")
ctx.TwoColumnDetail("<fg=green;op=bold>Tables</>", "<fg=yellow;op=bold>Size (MB)</>")
for i := range info.Tables {
ctx.TwoColumnDetail(info.Tables[i].Name, fmt.Sprintf("%.3f", float64(info.Tables[i].Size)/1024/1024))
}
Expand Down
8 changes: 4 additions & 4 deletions database/console/show_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func TestShowCommand(t *testing.T) {
mockQuery = mocksorm.NewQuery(t)
}
successCaseExpected := [][2]string{
{"<fg=green;op=bold>MariaDB</>", "MariaDB"},
{"<fg=green;op=bold>MariaDB</>", "test-version"},
{"Database", "db"},
{"Host", "host"},
{"Port", "port"},
{"Username", "username"},
{"Open Connections", "2"},
{"Tables", "1"},
{"Total Size", "0.000MiB"},
{"<fg=green;op=bold>Tables</>", "<fg=yellow;op=bold>Size (MiB)</>"},
{"Total Size", "0.000 MB"},
{"<fg=green;op=bold>Tables</>", "<fg=yellow;op=bold>Size (MB)</>"},
{"test", "0.000"},
{"<fg=green;op=bold>Views</>", "<fg=yellow;op=bold>Rows</>"},
{"test", "0"},
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestShowCommand(t *testing.T) {
mockQuery.EXPECT().Raw("SHOW status WHERE variable_name = 'threads_connected';").Return(mockQuery).Once()
mockQuery.EXPECT().Scan(&queryResult{}).Run(func(dest interface{}) {
if d, ok := dest.(*queryResult); ok {
d.Value = "MariaDB"
d.Value = "test-version-MariaDB"
}
}).Return(nil).Once()
mockQuery.EXPECT().Scan(&queryResult{}).Run(func(dest interface{}) {
Expand Down
19 changes: 14 additions & 5 deletions database/console/table_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,34 @@ func (r *TableCommand) display(ctx console.Context, table schema.Table) {
ctx.Error(fmt.Sprintf("Failed to get foreign keys: %s", err.Error()))
return
}
ctx.TwoColumnDetail(fmt.Sprintf("<fg=green;op=bold>%s</>", table.Name), "")
ctx.TwoColumnDetail(fmt.Sprintf("<fg=green;op=bold>%s</>", table.Name), fmt.Sprintf("<fg=gray>%s</>", table.Comment))
ctx.TwoColumnDetail("Columns", fmt.Sprintf("%d", len(columns)))
ctx.TwoColumnDetail("Size", fmt.Sprintf("%.3fMiB", float64(table.Size)/1024/1024))
ctx.TwoColumnDetail("Size", fmt.Sprintf("%.3f MB", float64(table.Size)/1024/1024))
if len(table.Engine) > 0 {
ctx.TwoColumnDetail("Engine", table.Engine)
}
if len(table.Collation) > 0 {
ctx.TwoColumnDetail("Collation", table.Collation)
}
if len(columns) > 0 {
ctx.NewLine()
ctx.TwoColumnDetail("<fg=green;op=bold>Column</>", "Type")
for i := range columns {
var (
key = columns[i].Name
value = columns[i].TypeName
value = columns[i].Type
attributes []string
)
if columns[i].Autoincrement {
attributes = append(attributes, "autoincrement")
}
attributes = append(attributes, columns[i].Type)
attributes = append(attributes, columns[i].TypeName)
if columns[i].Nullable {
attributes = append(attributes, "nullable")
}
if len(columns[i].Collation) > 0 {
attributes = append(attributes, columns[i].Collation)
}
key = fmt.Sprintf("%s <fg=gray>%s</>", key, strings.Join(attributes, ", "))
if columns[i].Default != "" {
value = fmt.Sprintf("<fg=gray>%s</> %s", columns[i].Default, value)
Expand All @@ -131,7 +140,7 @@ func (r *TableCommand) display(ctx console.Context, table schema.Table) {
ctx.NewLine()
ctx.TwoColumnDetail("<fg=green;op=bold>Index</>", "")
for i := range indexes {
var attributes []string
attributes := []string{indexes[i].Type}
if len(indexes[i].Columns) > 1 {
attributes = append(attributes, "compound")
}
Expand Down
22 changes: 14 additions & 8 deletions database/console/table_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ func TestTableCommand(t *testing.T) {
mockSchema = mocksschema.NewSchema(t)
}
successCaseExpected := [][2]string{
{"<fg=green;op=bold>test</>", ""},
{"Columns", "1"},
{"Size", "0.000MiB"},
{"<fg=green;op=bold>test</>", "<fg=gray>test_comment</>"},
{"Columns", "2"},
{"Size", "0.000 MB"},
{"Engine", "InnoDB"},
{"Collation", "utf8mb4_general_ci"},
{"<fg=green;op=bold>Column</>", "Type"},
{"foo <fg=gray>autoincrement, int, nullable</>", "<fg=gray>bar</> int"},
{"foo <fg=gray>autoincrement, int, nullable, utf8mb4_general_ci</>", "<fg=gray>bar</> int(11)"},
{"bar <fg=gray>varchar, utf8mb4_general_ci</>", "varchar(32)"},
{"<fg=green;op=bold>Index</>", ""},
{"index_foo <fg=gray>foo, bar</>", "compound, unique, primary"},
{"index_foo <fg=gray>foo, bar</>", "btree, compound, unique, primary"},
{"<fg=green;op=bold>Foreign Key</>", "On Update / On Delete"},
{"fk_foo <fg=gray>foo references baz on bar</>", "restrict / cascade"},
}
Expand Down Expand Up @@ -126,14 +129,17 @@ func TestTableCommand(t *testing.T) {
mockContext.EXPECT().Option("database").Return("test").Once()
mockSchema.EXPECT().Connection("test").Return(mockSchema).Once()
mockContext.EXPECT().Argument(0).Return("").Once()
mockSchema.EXPECT().GetTables().Return([]schema.Table{{Name: "test"}}, nil).Once()
mockSchema.EXPECT().GetTables().Return([]schema.Table{
{Name: "test", Comment: "test_comment", Collation: "utf8mb4_general_ci", Engine: "InnoDB"},
}, nil).Once()
mockContext.EXPECT().Choice("Which table would you like to inspect?",
[]console.Choice{{Key: "test", Value: "test"}}).Return("test", nil).Once()
mockSchema.EXPECT().GetColumns("test").Return([]schema.Column{
{Name: "foo", Type: "int", TypeName: "int", Autoincrement: true, Nullable: true, Default: "bar"},
{Name: "foo", Type: "int(11)", TypeName: "int", Autoincrement: true, Nullable: true, Default: "bar", Collation: "utf8mb4_general_ci"},
{Name: "bar", Type: "varchar(32)", TypeName: "varchar", Collation: "utf8mb4_general_ci"},
}, nil).Once()
mockSchema.EXPECT().GetIndexes("test").Return([]schema.Index{
{Name: "index_foo", Columns: []string{"foo", "bar"}, Unique: true, Primary: true},
{Name: "index_foo", Type: "btree", Columns: []string{"foo", "bar"}, Unique: true, Primary: true},
}, nil).Once()
mockSchema.EXPECT().GetForeignKeys("test").Return([]schema.ForeignKey{
{
Expand Down

0 comments on commit 28f486f

Please sign in to comment.