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.

v78.go 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 if models.DbCfg.Type == core.MYSQL {
  30. indexes, err := sess.QueryString(`SHOW INDEX FROM repository WHERE KEY_NAME = 'IDX_repository_is_bare'`)
  31. if err != nil {
  32. return err
  33. }
  34. if len(indexes) >= 1 {
  35. _, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
  36. }
  37. } else {
  38. _, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
  39. }
  40. if err != nil {
  41. return fmt.Errorf("Drop index failed: %v", err)
  42. }
  43. if err = sess.Commit(); err != nil {
  44. return err
  45. }
  46. if err := sess.Begin(); err != nil {
  47. return err
  48. }
  49. if err := sess.Sync2(new(Repository)); err != nil {
  50. return err
  51. }
  52. if _, err := sess.Exec("UPDATE repository SET is_empty = is_bare;"); err != nil {
  53. return err
  54. }
  55. if models.DbCfg.Type != core.SQLITE {
  56. _, err = sess.Exec("ALTER TABLE repository DROP COLUMN is_bare")
  57. if err != nil {
  58. return fmt.Errorf("Drop column failed: %v", err)
  59. }
  60. }
  61. if err = sess.Commit(); err != nil {
  62. return err
  63. }
  64. if models.DbCfg.Type == core.SQLITE {
  65. log.Warn("TABLE repository's COLUMN is_bare should be DROP but sqlite is not supported, you could manually do that.")
  66. }
  67. return nil
  68. }