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.

52 lines
1.1 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. "net/url"
  7. "xorm.io/xorm"
  8. )
  9. func sanitizeOriginalURL(x *xorm.Engine) error {
  10. type Repository struct {
  11. ID int64
  12. OriginalURL string `xorm:"VARCHAR(2048)"`
  13. }
  14. var last int
  15. const batchSize = 50
  16. for {
  17. var results = make([]Repository, 0, batchSize)
  18. err := x.Where("original_url <> '' AND original_url IS NOT NULL").
  19. And("original_service_type = 0 OR original_service_type IS NULL").
  20. OrderBy("id").
  21. Limit(batchSize, last).
  22. Find(&results)
  23. if err != nil {
  24. return err
  25. }
  26. if len(results) == 0 {
  27. break
  28. }
  29. last += len(results)
  30. for _, res := range results {
  31. u, err := url.Parse(res.OriginalURL)
  32. if err != nil {
  33. // it is ok to continue here, we only care about fixing URLs that we can read
  34. continue
  35. }
  36. u.User = nil
  37. originalURL := u.String()
  38. _, err = x.Exec("UPDATE repository SET original_url = ? WHERE id = ?", originalURL, res.ID)
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. }
  44. return nil
  45. }