Skip to content

Commit

Permalink
made all partition suffixes force the names to be lower case
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Sep 14, 2024
1 parent 484685c commit 993f536
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
33 changes: 33 additions & 0 deletions src/Weasel.Postgresql.Tests/Tables/partitioning/list_partitions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using JasperFx.Core.Reflection;
using Shouldly;
using Weasel.Core;
using Weasel.Postgresql.Tables;
Expand Down Expand Up @@ -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<ListPartitioning>().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()
{
Expand All @@ -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<ListPartitioning>().EnableDefaultPartition = false;
await tryToCreateTable();

var existing = await tryToFetchExisting();

var partitioning = existing.Partitioning.ShouldBeOfType<ListPartitioning>();
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();
}
}
19 changes: 18 additions & 1 deletion src/Weasel.Postgresql/Tables/Partitioning/ListPartitioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class ListPartitioning: IPartitionStrategy

public IReadOnlyList<ListPartition> Partitions => _partitions;

/// <summary>
/// </summary>
public bool EnableDefaultPartition { get; set; } = true;

/// <summary>
/// Add another list partition table based on the supplied table suffix and values
/// </summary>
Expand All @@ -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)
Expand Down Expand Up @@ -98,4 +105,14 @@ public async Task ReadPartitionsAsync(DbObjectName identifier, DbDataReader read
}

public bool HasExistingDefault { get; private set; }

/// <summary>
/// Disable the default partition
/// </summary>
/// <returns></returns>
public ListPartitioning DisableDefaultPartition()
{
EnableDefaultPartition = false;
return this;
}
}

0 comments on commit 993f536

Please sign in to comment.