20250913211637_AddProperParentChildRelationBaseItemWithCascade.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.EntityFrameworkCore.Migrations;
  2. #nullable disable
  3. namespace Jellyfin.Server.Implementations.Migrations
  4. {
  5. /// <inheritdoc />
  6. public partial class AddProperParentChildRelationBaseItemWithCascade : Migration
  7. {
  8. /// <inheritdoc />
  9. protected override void Up(MigrationBuilder migrationBuilder)
  10. {
  11. migrationBuilder.Sql("""
  12. DELETE FROM BaseItems
  13. WHERE
  14. ParentId IS NOT NULL
  15. AND
  16. NOT EXISTS(SELECT 1 FROM BaseItems parent WHERE parent.Id = BaseItems.ParentId);
  17. DELETE FROM BaseItems
  18. WHERE
  19. ParentId IS NOT NULL
  20. AND
  21. NOT EXISTS(SELECT 1 FROM BaseItems parent WHERE parent.Id = BaseItems.ParentId);
  22. DELETE FROM BaseItems
  23. WHERE
  24. ParentId IS NOT NULL
  25. AND
  26. NOT EXISTS(SELECT 1 FROM BaseItems parent WHERE parent.Id = BaseItems.ParentId);
  27. DELETE FROM BaseItems
  28. WHERE
  29. ParentId IS NOT NULL
  30. AND
  31. NOT EXISTS(SELECT 1 FROM BaseItems parent WHERE parent.Id = BaseItems.ParentId);
  32. """);
  33. migrationBuilder.AddForeignKey(
  34. name: "FK_BaseItems_BaseItems_ParentId",
  35. table: "BaseItems",
  36. column: "ParentId",
  37. principalTable: "BaseItems",
  38. principalColumn: "Id",
  39. onDelete: ReferentialAction.Cascade);
  40. }
  41. /// <inheritdoc />
  42. protected override void Down(MigrationBuilder migrationBuilder)
  43. {
  44. migrationBuilder.DropForeignKey(
  45. name: "FK_BaseItems_BaseItems_ParentId",
  46. table: "BaseItems");
  47. }
  48. }
  49. }