Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for null column name and add expression to index schema #14

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,38 @@ func CreateTableMarkdown(tableName string, comment string, columns []ColumnDescr
// format the indexes
tableMarkdown.WriteString("#### INDEXES\n")
indexesTable := tablewriter.NewWriter(tableMarkdown)
indexesTable.SetHeader([]string{"KEY NAME", "UNIQUE", "COLUMNS", "COMMENT"})

// EXPRESSION is a new column type introduced in MySQL 8.0.
// Only include this header if one of the indexes has an expression.
hasExpression := false
for _, idx := range indexes {
if idx.Expression != "" {
hasExpression = true
break
}
}
header := []string{"KEY NAME", "UNIQUE", "COLUMNS", "COMMENT"}
if hasExpression {
header = []string{"KEY NAME", "UNIQUE", "COLUMNS", "COMMENT", "EXPRESSION"}
}

indexesTable.SetHeader(header)
indexesTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
indexesTable.SetAutoWrapText(false)
indexesTable.SetCenterSeparator("|")

for _, idx := range indexes {
indexesTable.Append([]string{
indexRow := []string{
wrapBackTicks(idx.KeyName),
wrapBackTicks(fmt.Sprintf("%t", !idx.NonUnique)),
wrapBackTicks(fmt.Sprintf(`(%s)`, strings.Join(idx.IndexedColumnNamesOrdered, ", "))),
wrapBackTicks(idx.Comment),
})
}
// Only include this if one of the indexes in the table has an expression.
if hasExpression {
indexRow = append(indexRow, wrapBackTicks(idx.Expression))
}
indexesTable.Append(indexRow)
}

// write the indexes table to the buf
Expand Down
16 changes: 9 additions & 7 deletions format/index_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type IndexDescription struct {
NonUnique bool `db:"Non_unique"`
KeyName string `db:"Key_name"`
SeqInIndex int `db:"Seq_in_index"`
ColumnName string `db:"Column_name"`
ColumnName sql.NullString `db:"Column_name"`
Comment sql.NullString `db:"Comment"`
Expression sql.NullString `db:"Expression"`

// Not used (yet)
Collation sql.NullString `db:"Collation"`
Expand All @@ -24,7 +25,6 @@ type IndexDescription struct {
IndexType sql.NullString `db:"Index_type"`
IndexComment sql.NullString `db:"Index_comment"`
Visible sql.NullString `db:"Visible"`
Expression sql.NullString `db:"Expression"`
}

// IndexDescriptions is a set of index descriptions
Expand All @@ -40,14 +40,15 @@ func (descs IndexDescriptions) ConvertToLogicalIndexes() ([]LogicalIndex, error)
var ok bool
if li, ok = indices[description.KeyName]; !ok {
li = LogicalIndex{
Table: description.Table,
NonUnique: description.NonUnique,
KeyName: description.KeyName,
Comment: description.Comment.String,
Table: description.Table,
NonUnique: description.NonUnique,
KeyName: description.KeyName,
Comment: description.Comment.String,
Expression: description.Expression.String,
}
}

li.IndexedColumnNamesOrdered = append(li.IndexedColumnNamesOrdered, description.ColumnName)
li.IndexedColumnNamesOrdered = append(li.IndexedColumnNamesOrdered, description.ColumnName.String)

if len(li.IndexedColumnNamesOrdered) != description.SeqInIndex {
return nil, fmt.Errorf("internal logic error: expecting for indexed columns to always be returned in sequence")
Expand Down Expand Up @@ -80,4 +81,5 @@ type LogicalIndex struct {
KeyName string
IndexedColumnNamesOrdered []string
Comment string
Expression string
}