mirror of https://github.com/gogs/gogs.git
schemadoc: print table indexes (#7012)
parent
a328e7ccc4
commit
4cc3000b07
|
@ -9,6 +9,8 @@
|
||||||
Mode | mode | BIGINT NOT NULL | BIGINT NOT NULL | INTEGER NOT NULL
|
Mode | mode | BIGINT NOT NULL | BIGINT NOT NULL | INTEGER NOT NULL
|
||||||
|
|
||||||
Primary keys: id
|
Primary keys: id
|
||||||
|
Indexes:
|
||||||
|
"access_user_repo_unique" UNIQUE (user_id, repo_id)
|
||||||
```
|
```
|
||||||
|
|
||||||
# Table "access_token"
|
# Table "access_token"
|
||||||
|
@ -25,6 +27,8 @@ Primary keys: id
|
||||||
UpdatedUnix | updated_unix | BIGINT | BIGINT | INTEGER
|
UpdatedUnix | updated_unix | BIGINT | BIGINT | INTEGER
|
||||||
|
|
||||||
Primary keys: id
|
Primary keys: id
|
||||||
|
Indexes:
|
||||||
|
"idx_access_token_user_id" (uid)
|
||||||
```
|
```
|
||||||
|
|
||||||
# Table "lfs_object"
|
# Table "lfs_object"
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
|
@ -77,6 +79,28 @@ func main() {
|
||||||
_, _ = w.WriteString(strings.Join(ti.PrimaryKeys, ", "))
|
_, _ = w.WriteString(strings.Join(ti.PrimaryKeys, ", "))
|
||||||
_, _ = w.WriteString("\n")
|
_, _ = w.WriteString("\n")
|
||||||
|
|
||||||
|
if len(ti.Indexes) > 0 {
|
||||||
|
_, _ = w.WriteString("Indexes: \n")
|
||||||
|
for _, index := range ti.Indexes {
|
||||||
|
_, _ = w.WriteString(fmt.Sprintf("\t%q", index.Name))
|
||||||
|
if index.Class != "" {
|
||||||
|
_, _ = w.WriteString(fmt.Sprintf(" %s", index.Class))
|
||||||
|
}
|
||||||
|
if index.Type != "" {
|
||||||
|
_, _ = w.WriteString(fmt.Sprintf(", %s", index.Type))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(index.Fields) > 0 {
|
||||||
|
fields := make([]string, len(index.Fields))
|
||||||
|
for i := range index.Fields {
|
||||||
|
fields[i] = index.Fields[i].DBName
|
||||||
|
}
|
||||||
|
_, _ = w.WriteString(fmt.Sprintf(" (%s)", strings.Join(fields, ", ")))
|
||||||
|
}
|
||||||
|
_, _ = w.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, _ = w.WriteString("```\n\n")
|
_, _ = w.WriteString("```\n\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,17 +115,21 @@ type tableInfo struct {
|
||||||
Name string
|
Name string
|
||||||
Fields []*tableField
|
Fields []*tableField
|
||||||
PrimaryKeys []string
|
PrimaryKeys []string
|
||||||
|
Indexes []schema.Index
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is derived from gorm.io/gorm/migrator/migrator.go:Migrator.CreateTable.
|
// This function is derived from gorm.io/gorm/migrator/migrator.go:Migrator.CreateTable.
|
||||||
func generate(dialector gorm.Dialector) ([]*tableInfo, error) {
|
func generate(dialector gorm.Dialector) ([]*tableInfo, error) {
|
||||||
conn, err := gorm.Open(dialector, &gorm.Config{
|
conn, err := gorm.Open(dialector,
|
||||||
NamingStrategy: schema.NamingStrategy{
|
&gorm.Config{
|
||||||
SingularTable: true,
|
SkipDefaultTransaction: true,
|
||||||
|
NamingStrategy: schema.NamingStrategy{
|
||||||
|
SingularTable: true,
|
||||||
|
},
|
||||||
|
DryRun: true,
|
||||||
|
DisableAutomaticPing: true,
|
||||||
},
|
},
|
||||||
DryRun: true,
|
)
|
||||||
DisableAutomaticPing: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "open database")
|
return nil, errors.Wrap(err, "open database")
|
||||||
}
|
}
|
||||||
|
@ -133,10 +161,19 @@ func generate(dialector gorm.Dialector) ([]*tableInfo, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var indexes []schema.Index
|
||||||
|
for _, index := range stmt.Schema.ParseIndexes() {
|
||||||
|
indexes = append(indexes, index)
|
||||||
|
}
|
||||||
|
sort.Slice(indexes, func(i, j int) bool {
|
||||||
|
return indexes[i].Name < indexes[j].Name
|
||||||
|
})
|
||||||
|
|
||||||
tableInfos = append(tableInfos, &tableInfo{
|
tableInfos = append(tableInfos, &tableInfo{
|
||||||
Name: stmt.Table,
|
Name: stmt.Table,
|
||||||
Fields: fields,
|
Fields: fields,
|
||||||
PrimaryKeys: primaryKeys,
|
PrimaryKeys: primaryKeys,
|
||||||
|
Indexes: indexes,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue