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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 zuzhi user history data ", deleteNotDisplayUser},
  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 alterUserStaticTable(x *xorm.Engine, static *xorm.Engine) error {
  44. alterSql := "alter table public.user_business_analysis alter column open_i_index type double precision"
  45. _, err := static.Exec(alterSql)
  46. return err
  47. }
  48. func deleteNotDisplayUser(x *xorm.Engine, static *xorm.Engine) error {
  49. querySQL := "select id,name from public.user where type=1"
  50. rows, err := x.Query(querySQL)
  51. if err != nil {
  52. log.Info("select db failed,err:", err)
  53. return err
  54. }
  55. for i, userRow := range rows {
  56. log.Info("delete zuzi user, i=" + fmt.Sprint(i) + " userName=" + string(userRow["name"]))
  57. deleteSql := "delete from user_business_analysis where id=" + string(userRow["id"]) + " and name='" + string(userRow["name"]) + "'"
  58. static.Exec(deleteSql)
  59. }
  60. return nil
  61. }