You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_migrations.go 1.7 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package models
  2. import (
  3. "fmt"
  4. "code.gitea.io/gitea/modules/log"
  5. "xorm.io/xorm"
  6. )
  7. type CustomMigration struct {
  8. Description string
  9. Migrate func(*xorm.Engine) error
  10. }
  11. type CustomMigrationStatic struct {
  12. Description string
  13. Migrate func(*xorm.Engine, *xorm.Engine) error
  14. }
  15. var customMigrations = []CustomMigration{
  16. {"Custom v1 Topic struct change to support chinese", syncTopicStruct},
  17. }
  18. var customMigrationsStatic = []CustomMigrationStatic{
  19. {"Delete zuzhi user history data ", deleteNotDisplayUser},
  20. }
  21. func MigrateCustom(x *xorm.Engine) {
  22. for _, m := range customMigrations {
  23. log.Info("Migration: %s", m.Description)
  24. if err := m.Migrate(x); err != nil {
  25. log.Error("Migration: %v", err)
  26. }
  27. }
  28. }
  29. func MigrateCustomStatic(x *xorm.Engine, static *xorm.Engine) {
  30. for _, m := range customMigrationsStatic {
  31. log.Info("Migration: %s", m.Description)
  32. if err := m.Migrate(x, static); err != nil {
  33. log.Error("Migration: %v", err)
  34. }
  35. }
  36. }
  37. func syncTopicStruct(x *xorm.Engine) error {
  38. query := "ALTER TABLE topic ALTER COLUMN name TYPE varchar(105);"
  39. _, err := x.Exec(query)
  40. return err
  41. }
  42. func deleteNotDisplayUser(x *xorm.Engine, static *xorm.Engine) error {
  43. sess := x.NewSession()
  44. defer sess.Close()
  45. sess.Select("`user`.*").Table("user").Where("type=1")
  46. userList := make([]*User, 0)
  47. log.Info("delete zuzi user size=" + fmt.Sprint(len(userList)))
  48. for i, userRecord := range userList {
  49. log.Info("delete zuzi user, i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
  50. deleteSql := "delete from user_business_analysis where id=" + fmt.Sprint(userRecord.ID) + " and name='" + userRecord.Name + "'"
  51. static.Exec(deleteSql)
  52. }
  53. return nil
  54. }