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 2.0 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 organization user history data ", deleteNotDisplayUser},
  20. {"update issue_fixed_rate to 1 if num_issues is 0 ", updateIssueFixedRate},
  21. }
  22. func MigrateCustom(x *xorm.Engine) {
  23. for _, m := range customMigrations {
  24. log.Info("Migration: %s", m.Description)
  25. if err := m.Migrate(x); err != nil {
  26. log.Error("Migration: %v", err)
  27. }
  28. }
  29. }
  30. func MigrateCustomStatic(x *xorm.Engine, static *xorm.Engine) {
  31. for _, m := range customMigrationsStatic {
  32. log.Info("Migration: %s", m.Description)
  33. if err := m.Migrate(x, static); err != nil {
  34. log.Error("Migration: %v", err)
  35. }
  36. }
  37. }
  38. func syncTopicStruct(x *xorm.Engine) error {
  39. query := "ALTER TABLE topic ALTER COLUMN name TYPE varchar(105);"
  40. _, err := x.Exec(query)
  41. return err
  42. }
  43. func deleteNotDisplayUser(x *xorm.Engine, static *xorm.Engine) error {
  44. querySQL := "select id,name from public.user where type=1"
  45. rows, err := x.Query(querySQL)
  46. if err != nil {
  47. log.Info("select db failed,err:", err)
  48. return err
  49. }
  50. for i, userRow := range rows {
  51. log.Info("delete zuzi user, i=" + fmt.Sprint(i) + " userName=" + string(userRow["name"]))
  52. deleteSql := "delete from user_business_analysis where id=" + string(userRow["id"]) + " and name='" + string(userRow["name"]) + "'"
  53. static.Exec(deleteSql)
  54. }
  55. return nil
  56. }
  57. func updateIssueFixedRate(x *xorm.Engine, static *xorm.Engine) error {
  58. updateSQL := "update repo_statistic set issue_fixed_rate=1.0 where num_issues=0"
  59. _, err := static.Exec(updateSQL)
  60. return err
  61. }