Skip to content

Commit

Permalink
feat: migrator support table comment (#114)
Browse files Browse the repository at this point in the history
* feat: migrator support table comment

* feat: migrator support TableType. It like ColumnTypes.

* remove CatalogValue & EngineValue.

* upgrade gorm version.
  • Loading branch information
johnmai-dev authored May 5, 2023
1 parent 60607c4 commit 2b3cf21
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/go-sql-driver/mysql v1.7.0
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677
)

require github.com/jinzhu/now v1.1.5 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11 h1:9qNbmu21nNThCNnF5i2R3kw2aL27U8ZwbzccNjOmW0g=
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677 h1:+vFpygNj+UclZztgzy/5ZGvkag6EH6moMm/MNTxkShk=
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
25 changes: 25 additions & 0 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,28 @@ func (m Migrator) CurrentSchema(stmt *gorm.Statement, table string) (string, str
func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
return typeAliasMap[databaseTypeName]
}

// TableType table type return tableType,error
func (m Migrator) TableType(value interface{}) (tableType gorm.TableType, err error) {
var table migrator.TableType

err = m.RunWithValue(value, func(stmt *gorm.Statement) error {
var (
values = []interface{}{
&table.SchemaValue, &table.NameValue, &table.TypeValue, &table.CommentValue,
}
currentDatabase, tableName = m.CurrentSchema(stmt, stmt.Table)
tableTypeSQL = "SELECT table_schema, table_name, table_type, table_comment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?"
)

row := m.DB.Table(tableName).Raw(tableTypeSQL, currentDatabase, tableName).Row()

if scanErr := row.Scan(values...); scanErr != nil {
return scanErr
}

return nil
})

return table, err
}

0 comments on commit 2b3cf21

Please sign in to comment.