gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_14
  4. import (
  5. "code.gitea.io/gitea/models/migrations/base"
  6. "xorm.io/xorm"
  7. "xorm.io/xorm/schemas"
  8. )
  9. func ConvertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
  10. dbType := x.Dialect().URI().DBType
  11. if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT
  12. return nil
  13. }
  14. // HookTask: Typ string `xorm:"VARCHAR(16) index"`
  15. if err := base.ModifyColumn(x, "hook_task", &schemas.Column{
  16. Name: "typ",
  17. SQLType: schemas.SQLType{
  18. Name: "VARCHAR",
  19. },
  20. Length: 16,
  21. Nullable: true, // To keep compatible as nullable
  22. DefaultIsEmpty: true,
  23. }); err != nil {
  24. return err
  25. }
  26. var hookTaskTrimSQL string
  27. if dbType == schemas.MSSQL {
  28. hookTaskTrimSQL = "UPDATE hook_task SET typ = RTRIM(LTRIM(typ))"
  29. } else {
  30. hookTaskTrimSQL = "UPDATE hook_task SET typ = TRIM(typ)"
  31. }
  32. if _, err := x.Exec(hookTaskTrimSQL); err != nil {
  33. return err
  34. }
  35. // Webhook: Type string `xorm:"VARCHAR(16) index"`
  36. if err := base.ModifyColumn(x, "webhook", &schemas.Column{
  37. Name: "type",
  38. SQLType: schemas.SQLType{
  39. Name: "VARCHAR",
  40. },
  41. Length: 16,
  42. Nullable: true, // To keep compatible as nullable
  43. DefaultIsEmpty: true,
  44. }); err != nil {
  45. return err
  46. }
  47. var webhookTrimSQL string
  48. if dbType == schemas.MSSQL {
  49. webhookTrimSQL = "UPDATE webhook SET type = RTRIM(LTRIM(type))"
  50. } else {
  51. webhookTrimSQL = "UPDATE webhook SET type = TRIM(type)"
  52. }
  53. _, err := x.Exec(webhookTrimSQL)
  54. return err
  55. }