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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/log"
  4. "xorm.io/xorm"
  5. )
  6. type CustomMigration struct {
  7. Description string
  8. Migrate func(*xorm.Engine) error
  9. }
  10. type CustomMigrationStatic struct {
  11. Description string
  12. Migrate func(*xorm.Engine, *xorm.Engine) error
  13. }
  14. var customMigrations = []CustomMigration{
  15. //手机号功能可以不启用,不启用时手机号为空串,为了避免启用时唯一性约束报错,定制唯一性约束(对null和空字符串不做唯一性检查)
  16. {"set phone number unique index", setPhoneNumberUniqueIndex},
  17. }
  18. var customMigrationsStatic []CustomMigrationStatic
  19. func MigrateCustom(x *xorm.Engine) {
  20. for _, m := range customMigrations {
  21. log.Info("Migration: %s", m.Description)
  22. if err := m.Migrate(x); err != nil {
  23. log.Error("Migration: %v", err)
  24. }
  25. }
  26. }
  27. func MigrateCustomStatic(x *xorm.Engine, static *xorm.Engine) {
  28. for _, m := range customMigrationsStatic {
  29. log.Info("Migration: %s", m.Description)
  30. if err := m.Migrate(x, static); err != nil {
  31. log.Error("Migration: %v", err)
  32. }
  33. }
  34. }
  35. func setPhoneNumberUniqueIndex(x *xorm.Engine) error {
  36. query := "CREATE UNIQUE INDEX IF NOT EXISTS \"UQE_user_phone_number\" ON \"user\" (phone_number) WHERE phone_number IS NOT NULL and phone_number !='';"
  37. _, err := x.Exec(query)
  38. return err
  39. }
  40. func syncTopicStruct(x *xorm.Engine) error {
  41. query := "ALTER TABLE topic ALTER COLUMN name TYPE varchar(105);"
  42. _, err := x.Exec(query)
  43. return err
  44. }
  45. func updateIssueFixedRate(x *xorm.Engine, static *xorm.Engine) error {
  46. updateSQL := "update repo_statistic set issue_fixed_rate=1.0 where num_issues=0"
  47. _, err := static.Exec(updateSQL)
  48. return err
  49. }