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.2 kB

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