|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package models
-
- import (
- "fmt"
-
- "code.gitea.io/gitea/modules/log"
- "xorm.io/xorm"
- )
-
- type CustomMigration struct {
- Description string
- Migrate func(*xorm.Engine) error
- }
-
- type CustomMigrationStatic struct {
- Description string
- Migrate func(*xorm.Engine, *xorm.Engine) error
- }
-
- var customMigrations = []CustomMigration{
- {"Custom v1 Topic struct change to support chinese", syncTopicStruct},
- {"Dataset upgrade", upgradeDataset},
- }
-
- var customMigrationsStatic = []CustomMigrationStatic{
- {"Delete organization user history data ", deleteNotDisplayUser},
- {"update issue_fixed_rate to 1 if num_issues is 0 ", updateIssueFixedRate},
- }
-
- func MigrateCustom(x *xorm.Engine) {
-
- for _, m := range customMigrations {
- log.Info("Migration: %s", m.Description)
- if err := m.Migrate(x); err != nil {
-
- log.Error("Migration: %v", err)
-
- }
- }
-
- }
-
- func MigrateCustomStatic(x *xorm.Engine, static *xorm.Engine) {
- for _, m := range customMigrationsStatic {
- log.Info("Migration: %s", m.Description)
- if err := m.Migrate(x, static); err != nil {
-
- log.Error("Migration: %v", err)
-
- }
- }
- }
-
- func upgradeDataset(x *xorm.Engine) error {
-
- query := "update dataset set status=2 where status!=2 and id not in (select distinct a.id from dataset a,attachment b where a.id=b.dataset_id )"
-
- _, err := x.Exec(query)
- if err != nil {
- return err
- }
- datasetRepo := new(DatasetRepoInfo)
- total, err := x.Count(datasetRepo)
- if err == nil && total == 0 {
- query := "INSERT INTO dataset_repo_info ( data_set_id, repo_id, user_id, created_unix, updated_unix) SELECT id, repo_id, user_id, created_unix,updated_unix FROM dataset WHERE status != 2"
- _, err := x.Exec(query)
- return err
- } else {
- return err
- }
-
- }
-
- func syncTopicStruct(x *xorm.Engine) error {
-
- query := "ALTER TABLE topic ALTER COLUMN name TYPE varchar(105);"
-
- _, err := x.Exec(query)
- return err
- }
-
- func deleteNotDisplayUser(x *xorm.Engine, static *xorm.Engine) error {
-
- querySQL := "select id,name from public.user where type=1"
- rows, err := x.Query(querySQL)
- if err != nil {
- log.Info("select db failed,err:", err)
- return err
- }
-
- for i, userRow := range rows {
- log.Info("delete zuzi user, i=" + fmt.Sprint(i) + " userName=" + string(userRow["name"]))
- deleteSql := "delete from user_business_analysis where id=" + string(userRow["id"]) + " and name='" + string(userRow["name"]) + "'"
- static.Exec(deleteSql)
- }
-
- return nil
- }
-
- func updateIssueFixedRate(x *xorm.Engine, static *xorm.Engine) error {
- updateSQL := "update repo_statistic set issue_fixed_rate=1.0 where num_issues=0"
- _, err := static.Exec(updateSQL)
- return err
- }
|