diff options
author | zeripath <art27@cantab.net> | 2020-09-06 22:52:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-06 22:52:01 +0100 |
commit | 1b9d5074a7ebb1b470f468cc9195d54915291ee3 (patch) | |
tree | 1045623ccc744aedb934017f6bf8ae345131db17 /models/models.go | |
parent | ad2bf376dfd934394cad46c3ff3e022ca232958f (diff) | |
download | gitea-1b9d5074a7ebb1b470f468cc9195d54915291ee3.tar.gz gitea-1b9d5074a7ebb1b470f468cc9195d54915291ee3.zip |
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 <art27@cantab.net>
Diffstat (limited to 'models/models.go')
-rw-r--r-- | models/models.go | 32 |
1 files changed, 32 insertions, 0 deletions
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 { |