diff --git a/src/Weasel.Postgresql.Tests/Tables/partitioning/detecting_table_deltas_with_partitions.cs b/src/Weasel.Postgresql.Tests/Tables/partitioning/detecting_table_deltas_with_partitions.cs index 679a4e40..be82cc6f 100644 --- a/src/Weasel.Postgresql.Tests/Tables/partitioning/detecting_table_deltas_with_partitions.cs +++ b/src/Weasel.Postgresql.Tests/Tables/partitioning/detecting_table_deltas_with_partitions.cs @@ -109,6 +109,36 @@ public async Task apply_migration_to_list_partitioned_with_data() (await countIs()).ShouldBe(4); } + [Fact] + public async Task apply_migration_to_list_partitioned_with_data_with_no_default_partitioning() + { + await CreateSchemaObjectInDatabase(theTable); + + theTable.ModifyColumn("last_name").AsPrimaryKey(); + + theTable.PartitionByList("last_name") + .DisableDefaultPartition() + .AddPartition("Miller", "Miller") + .AddPartition("May", "May") + .AddPartition("Smith", "Smith"); + + await addRow("Jeremy", "Miller"); + await addRow("Lindsey", "Miller"); + await addRow("Russell", "May"); + await addRow("Tyler", "May"); + + + var delta = await theTable.FindDeltaAsync(theConnection); + delta.Difference.ShouldBe(SchemaPatchDifference.Update); + delta.HasChanges().ShouldBeTrue(); + + await AssertNoDeltasAfterPatching(); + + (await countIs()).ShouldBe(4); + } + + + [Fact] public async Task no_partition_to_hash_partition() { diff --git a/src/Weasel.Postgresql.Tests/Tables/partitioning/list_partitions.cs b/src/Weasel.Postgresql.Tests/Tables/partitioning/list_partitions.cs index e8f71c90..e7e59f42 100644 --- a/src/Weasel.Postgresql.Tests/Tables/partitioning/list_partitions.cs +++ b/src/Weasel.Postgresql.Tests/Tables/partitioning/list_partitions.cs @@ -1,3 +1,4 @@ +using JasperFx.Core.Reflection; using Shouldly; using Weasel.Core; using Weasel.Postgresql.Tables; @@ -67,6 +68,19 @@ public async Task can_create_table() await tryToCreateTable(); } + [Fact] + public async Task can_create_table_with_default_partition_off() + { + theTable.Partitioning.As().EnableDefaultPartition = false; + var sql = theTable.ToCreateSql(new PostgresqlMigrator()); + sql.ShouldContain("partitions.people_admin"); + sql.ShouldContain("partitions.people_super"); + sql.ShouldContain("partitions.people_special"); + sql.ShouldNotContain("partitions.people_default"); + + await tryToCreateTable(); + } + [Fact] public async Task fetch_the_existing_table() { @@ -84,4 +98,23 @@ public async Task fetch_the_existing_table() partitioning.HasExistingDefault.ShouldBeTrue(); } + + [Fact] + public async Task fetch_the_existing_table_when_default_partition_is_off() + { + theTable.Partitioning.As().EnableDefaultPartition = false; + await tryToCreateTable(); + + var existing = await tryToFetchExisting(); + + var partitioning = existing.Partitioning.ShouldBeOfType(); + partitioning.Columns.Single().ShouldBe("role"); + + partitioning.Partitions.Count.ShouldBe(3); + partitioning.Partitions.ShouldContain(new ListPartition("admin", "'admin'")); + partitioning.Partitions.ShouldContain(new ListPartition("super", "'super'")); + partitioning.Partitions.ShouldContain(new ListPartition("special", "'special'")); + + partitioning.HasExistingDefault.ShouldBeFalse(); + } } diff --git a/src/Weasel.Postgresql/Tables/Partitioning/ListPartitioning.cs b/src/Weasel.Postgresql/Tables/Partitioning/ListPartitioning.cs index b597d5ce..37a7155a 100644 --- a/src/Weasel.Postgresql/Tables/Partitioning/ListPartitioning.cs +++ b/src/Weasel.Postgresql/Tables/Partitioning/ListPartitioning.cs @@ -12,6 +12,10 @@ public class ListPartitioning: IPartitionStrategy public IReadOnlyList Partitions => _partitions; + /// + /// + public bool EnableDefaultPartition { get; set; } = true; + /// /// Add another list partition table based on the supplied table suffix and values /// @@ -35,7 +39,10 @@ void IPartitionStrategy.WriteCreateStatement(TextWriter writer, Table parent) writer.WriteLine(); } - writer.WriteDefaultPartition(parent.Identifier); + if (EnableDefaultPartition) + { + writer.WriteDefaultPartition(parent.Identifier); + } } void IPartitionStrategy.WritePartitionBy(TextWriter writer) @@ -98,4 +105,14 @@ public async Task ReadPartitionsAsync(DbObjectName identifier, DbDataReader read } public bool HasExistingDefault { get; private set; } + + /// + /// Disable the default partition + /// + /// + public ListPartitioning DisableDefaultPartition() + { + EnableDefaultPartition = false; + return this; + } }