|
1 | 1 | using CmdScale.EntityFrameworkCore.TimescaleDB.Configuration.Hypertable; |
2 | 2 | using CmdScale.EntityFrameworkCore.TimescaleDB.Configuration.ReorderPolicy; |
| 3 | +using CmdScale.EntityFrameworkCore.TimescaleDB.Configuration.RetentionPolicy; |
3 | 4 | using CmdScale.EntityFrameworkCore.TimescaleDB.Operations; |
4 | 5 | using Microsoft.EntityFrameworkCore; |
5 | 6 | using Microsoft.EntityFrameworkCore.Migrations.Operations; |
@@ -726,6 +727,133 @@ public void Should_Order_Hypertable_And_Indexes_Correctly() |
726 | 727 |
|
727 | 728 | #endregion |
728 | 729 |
|
| 730 | + #region Should_Order_CreateHypertable_Before_AddRetentionPolicy |
| 731 | + |
| 732 | + private class OrderingMetric9 |
| 733 | + { |
| 734 | + public DateTime Timestamp { get; set; } |
| 735 | + public double Value { get; set; } |
| 736 | + } |
| 737 | + |
| 738 | + private class OrderingContext9 : DbContext |
| 739 | + { |
| 740 | + public DbSet<OrderingMetric9> Metrics => Set<OrderingMetric9>(); |
| 741 | + |
| 742 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 743 | + => optionsBuilder.UseNpgsql("Host=localhost;Database=test;Username=test;Password=test") |
| 744 | + .UseTimescaleDb(); |
| 745 | + |
| 746 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 747 | + { |
| 748 | + modelBuilder.Entity<OrderingMetric9>(entity => |
| 749 | + { |
| 750 | + entity.ToTable("OrderingMetrics9"); |
| 751 | + entity.HasNoKey(); |
| 752 | + entity.IsHypertable(x => x.Timestamp) |
| 753 | + .WithChunkTimeInterval("1 day"); |
| 754 | + entity.WithRetentionPolicy(dropAfter: "30 days"); |
| 755 | + }); |
| 756 | + } |
| 757 | + } |
| 758 | + |
| 759 | + /// <summary> |
| 760 | + /// Verifies that CreateHypertableOperation appears before AddRetentionPolicyOperation. |
| 761 | + /// Retention policies require the hypertable to exist first. |
| 762 | + /// </summary> |
| 763 | + [Fact] |
| 764 | + public void Should_Order_CreateHypertable_Before_AddRetentionPolicy() |
| 765 | + { |
| 766 | + // Arrange |
| 767 | + using OrderingContext9 context = new(); |
| 768 | + |
| 769 | + // Act |
| 770 | + IReadOnlyList<MigrationOperation> operations = GenerateMigrationOperations(null, context); |
| 771 | + |
| 772 | + // Assert |
| 773 | + int createTableIndex = operations.ToList().FindIndex(op => |
| 774 | + op is CreateTableOperation createTable && createTable.Name == "OrderingMetrics9"); |
| 775 | + int hypertableIndex = operations.ToList().FindIndex(op => |
| 776 | + op is CreateHypertableOperation hypertable && hypertable.TableName == "OrderingMetrics9"); |
| 777 | + int retentionPolicyIndex = operations.ToList().FindIndex(op => |
| 778 | + op is AddRetentionPolicyOperation policy && policy.TableName == "OrderingMetrics9"); |
| 779 | + |
| 780 | + Assert.NotEqual(-1, createTableIndex); |
| 781 | + Assert.NotEqual(-1, hypertableIndex); |
| 782 | + Assert.NotEqual(-1, retentionPolicyIndex); |
| 783 | + |
| 784 | + Assert.True(createTableIndex < hypertableIndex, |
| 785 | + $"CreateTable (index {createTableIndex}) should appear before CreateHypertable (index {hypertableIndex})"); |
| 786 | + Assert.True(hypertableIndex < retentionPolicyIndex, |
| 787 | + $"CreateHypertable (index {hypertableIndex}) should appear before AddRetentionPolicy (index {retentionPolicyIndex})"); |
| 788 | + } |
| 789 | + |
| 790 | + #endregion |
| 791 | + |
| 792 | + #region Should_Order_DropRetentionPolicy_Before_DropTable |
| 793 | + |
| 794 | + private class OrderingMetric10 |
| 795 | + { |
| 796 | + public DateTime Timestamp { get; set; } |
| 797 | + public double Value { get; set; } |
| 798 | + } |
| 799 | + |
| 800 | + private class OrderingSourceContext10 : DbContext |
| 801 | + { |
| 802 | + public DbSet<OrderingMetric10> Metrics => Set<OrderingMetric10>(); |
| 803 | + |
| 804 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 805 | + => optionsBuilder.UseNpgsql("Host=localhost;Database=test;Username=test;Password=test") |
| 806 | + .UseTimescaleDb(); |
| 807 | + |
| 808 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 809 | + { |
| 810 | + modelBuilder.Entity<OrderingMetric10>(entity => |
| 811 | + { |
| 812 | + entity.ToTable("OrderingMetrics10"); |
| 813 | + entity.HasNoKey(); |
| 814 | + entity.IsHypertable(x => x.Timestamp) |
| 815 | + .WithChunkTimeInterval("1 day"); |
| 816 | + entity.WithRetentionPolicy(dropAfter: "30 days"); |
| 817 | + }); |
| 818 | + } |
| 819 | + } |
| 820 | + |
| 821 | + private class OrderingTargetContext10 : DbContext |
| 822 | + { |
| 823 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 824 | + => optionsBuilder.UseNpgsql("Host=localhost;Database=test;Username=test;Password=test") |
| 825 | + .UseTimescaleDb(); |
| 826 | + } |
| 827 | + |
| 828 | + /// <summary> |
| 829 | + /// Verifies that DropRetentionPolicyOperation appears before DropTableOperation. |
| 830 | + /// The retention policy must be removed before the hypertable is dropped. |
| 831 | + /// </summary> |
| 832 | + [Fact] |
| 833 | + public void Should_Order_DropRetentionPolicy_Before_DropTable() |
| 834 | + { |
| 835 | + // Arrange |
| 836 | + using OrderingSourceContext10 sourceContext = new(); |
| 837 | + using OrderingTargetContext10 targetContext = new(); |
| 838 | + |
| 839 | + // Act — diff from source (has table + retention policy) to target (empty) |
| 840 | + IReadOnlyList<MigrationOperation> operations = GenerateMigrationOperations(sourceContext, targetContext); |
| 841 | + |
| 842 | + // Assert |
| 843 | + List<MigrationOperation> opsList = [.. operations]; |
| 844 | + |
| 845 | + int dropRetentionPolicyIndex = opsList.FindIndex(op => op is DropRetentionPolicyOperation); |
| 846 | + int dropTableIndex = opsList.FindIndex(op => |
| 847 | + op is DropTableOperation dropTable && dropTable.Name == "OrderingMetrics10"); |
| 848 | + |
| 849 | + Assert.NotEqual(-1, dropRetentionPolicyIndex); |
| 850 | + Assert.NotEqual(-1, dropTableIndex); |
| 851 | + Assert.True(dropRetentionPolicyIndex < dropTableIndex, |
| 852 | + $"DropRetentionPolicy (index {dropRetentionPolicyIndex}) should appear before DropTable (index {dropTableIndex})"); |
| 853 | + } |
| 854 | + |
| 855 | + #endregion |
| 856 | + |
729 | 857 | #region Should_Fail_With_Unstable_Sort_Many_Tables |
730 | 858 |
|
731 | 859 | // Many entity classes to generate enough operations to trigger unstable sort behavior |
|
0 commit comments