From 1b9d5074a7ebb1b470f468cc9195d54915291ee3 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 6 Sep 2020 22:52:01 +0100 Subject: Add command to recreate tables (#12407) Provides new command: `gitea doctor recreate-table` which will recreate db tables and copy the old data in to the new table. This function can be used to remove the old warning of struct defaults being out of date. Fix #8868 Fix #3265 Fix #8894 Signed-off-by: Andrew Thornton --- models/models.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'models/models.go') diff --git a/models/models.go b/models/models.go index e0dd3ed2a4..27a0c660be 100644 --- a/models/models.go +++ b/models/models.go @@ -10,6 +10,8 @@ import ( "database/sql" "errors" "fmt" + "reflect" + "strings" "code.gitea.io/gitea/modules/setting" @@ -214,6 +216,36 @@ func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err e return nil } +// NamesToBean return a list of beans or an error +func NamesToBean(names ...string) ([]interface{}, error) { + beans := []interface{}{} + if len(names) == 0 { + beans = append(beans, tables...) + return beans, nil + } + // Need to map provided names to beans... + beanMap := make(map[string]interface{}) + for _, bean := range tables { + + beanMap[strings.ToLower(reflect.Indirect(reflect.ValueOf(bean)).Type().Name())] = bean + beanMap[strings.ToLower(x.TableName(bean))] = bean + beanMap[strings.ToLower(x.TableName(bean, true))] = bean + } + + gotBean := make(map[interface{}]bool) + for _, name := range names { + bean, ok := beanMap[strings.ToLower(strings.TrimSpace(name))] + if !ok { + return nil, fmt.Errorf("No table found that matches: %s", name) + } + if !gotBean[bean] { + beans = append(beans, bean) + gotBean[bean] = true + } + } + return beans, nil +} + // Statistic contains the database statistics type Statistic struct { Counter struct { -- cgit v1.2.3