You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/log"
  9. "github.com/go-xorm/core"
  10. "github.com/go-xorm/xorm"
  11. )
  12. func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
  13. type Repository struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. IsBare bool
  16. IsEmpty bool `xorm:"INDEX"`
  17. }
  18. // First remove the index
  19. sess := x.NewSession()
  20. defer sess.Close()
  21. if err := sess.Begin(); err != nil {
  22. return err
  23. }
  24. var err error
  25. if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
  26. _, err = sess.Exec("DROP INDEX IF EXISTS IDX_repository_is_bare")
  27. } else if models.DbCfg.Type == core.MSSQL {
  28. _, err = sess.Exec("DROP INDEX IF EXISTS IDX_repository_is_bare ON repository")
  29. } else {
  30. _, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
  31. }
  32. if err != nil {
  33. return fmt.Errorf("Drop index failed: %v", err)
  34. }
  35. if err = sess.Commit(); err != nil {
  36. return err
  37. }
  38. if err := sess.Begin(); err != nil {
  39. return err
  40. }
  41. if err := sess.Sync2(new(Repository)); err != nil {
  42. return err
  43. }
  44. if _, err := sess.Exec("UPDATE repository SET is_empty = is_bare;"); err != nil {
  45. return err
  46. }
  47. if models.DbCfg.Type != core.SQLITE {
  48. _, err = sess.Exec("ALTER TABLE repository DROP COLUMN is_bare")
  49. if err != nil {
  50. return fmt.Errorf("Drop column failed: %v", err)
  51. }
  52. }
  53. if err = sess.Commit(); err != nil {
  54. return err
  55. }
  56. if models.DbCfg.Type == core.SQLITE {
  57. log.Warn("TABLE repository's COLUMN is_bare should be DROP but sqlite is not supported, you could manually do that.")
  58. }
  59. return nil
  60. }