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.

consistency.go 994B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package db
  4. import (
  5. "context"
  6. "xorm.io/builder"
  7. )
  8. // CountOrphanedObjects count subjects with have no existing refobject anymore
  9. func CountOrphanedObjects(ctx context.Context, subject, refobject, joinCond string) (int64, error) {
  10. return GetEngine(ctx).
  11. Table("`"+subject+"`").
  12. Join("LEFT", "`"+refobject+"`", joinCond).
  13. Where(builder.IsNull{"`" + refobject + "`.id"}).
  14. Select("COUNT(`" + subject + "`.`id`)").
  15. Count()
  16. }
  17. // DeleteOrphanedObjects delete subjects with have no existing refobject anymore
  18. func DeleteOrphanedObjects(ctx context.Context, subject, refobject, joinCond string) error {
  19. subQuery := builder.Select("`"+subject+"`.id").
  20. From("`"+subject+"`").
  21. Join("LEFT", "`"+refobject+"`", joinCond).
  22. Where(builder.IsNull{"`" + refobject + "`.id"})
  23. b := builder.Delete(builder.In("id", subQuery)).From("`" + subject + "`")
  24. _, err := GetEngine(ctx).Exec(b)
  25. return err
  26. }