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.

v286.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_22 //nolint
  4. import (
  5. "errors"
  6. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/setting"
  9. "xorm.io/xorm"
  10. )
  11. func expandHashReferencesToSha256(x *xorm.Engine) error {
  12. alteredTables := [][2]string{
  13. {"commit_status", "context_hash"},
  14. {"comment", "commit_sha"},
  15. {"pull_request", "merge_base"},
  16. {"pull_request", "merged_commit_id"},
  17. {"review", "commit_id"},
  18. {"review_state", "commit_sha"},
  19. {"repo_archiver", "commit_id"},
  20. {"release", "sha1"},
  21. {"repo_indexer_status", "commit_sha"},
  22. }
  23. db := x.NewSession()
  24. defer db.Close()
  25. if err := db.Begin(); err != nil {
  26. return err
  27. }
  28. if !setting.Database.Type.IsSQLite3() {
  29. if setting.Database.Type.IsMSSQL() {
  30. // drop indexes that need to be re-created afterwards
  31. droppedIndexes := []string{
  32. "DROP INDEX IF EXISTS [IDX_commit_status_context_hash] ON [commit_status]",
  33. "DROP INDEX IF EXISTS [UQE_review_state_pull_commit_user] ON [review_state]",
  34. "DROP INDEX IF EXISTS [UQE_repo_archiver_s] ON [repo_archiver]",
  35. }
  36. for _, s := range droppedIndexes {
  37. _, err := db.Exec(s)
  38. if err != nil {
  39. return errors.New(s + " " + err.Error())
  40. }
  41. }
  42. }
  43. for _, alts := range alteredTables {
  44. var err error
  45. if setting.Database.Type.IsMySQL() {
  46. _, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` MODIFY COLUMN `%s` VARCHAR(64)", alts[0], alts[1]))
  47. } else if setting.Database.Type.IsMSSQL() {
  48. _, err = db.Exec(fmt.Sprintf("ALTER TABLE [%s] ALTER COLUMN [%s] NVARCHAR(64)", alts[0], alts[1]))
  49. } else {
  50. _, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` TYPE VARCHAR(64)", alts[0], alts[1]))
  51. }
  52. if err != nil {
  53. return fmt.Errorf("alter column '%s' of table '%s' failed: %w", alts[1], alts[0], err)
  54. }
  55. }
  56. if setting.Database.Type.IsMSSQL() {
  57. recreateIndexes := []string{
  58. "CREATE INDEX IDX_commit_status_context_hash ON commit_status(context_hash)",
  59. "CREATE UNIQUE INDEX UQE_review_state_pull_commit_user ON review_state(user_id, pull_id, commit_sha)",
  60. "CREATE UNIQUE INDEX UQE_repo_archiver_s ON repo_archiver(repo_id, type, commit_id)",
  61. }
  62. for _, s := range recreateIndexes {
  63. _, err := db.Exec(s)
  64. if err != nil {
  65. return errors.New(s + " " + err.Error())
  66. }
  67. }
  68. }
  69. }
  70. log.Debug("Updated database tables to hold SHA256 git hash references")
  71. return db.Commit()
  72. }
  73. func addObjectFormatNameToRepository(x *xorm.Engine) error {
  74. type Repository struct {
  75. ObjectFormatName string `xorm:"VARCHAR(6) NOT NULL DEFAULT 'sha1'"`
  76. }
  77. if err := x.Sync(new(Repository)); err != nil {
  78. return err
  79. }
  80. // Here to catch weird edge-cases where column constraints above are
  81. // not applied by the DB backend
  82. _, err := x.Exec("UPDATE repository set object_format_name = 'sha1' WHERE object_format_name = '' or object_format_name IS NULL")
  83. return err
  84. }
  85. func AdjustDBForSha256(x *xorm.Engine) error {
  86. if err := expandHashReferencesToSha256(x); err != nil {
  87. return err
  88. }
  89. return addObjectFormatNameToRepository(x)
  90. }