Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Postgres Full Text Index together from Marten #110

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Weasel.Postgresql.Tests/Tables/IndexDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class IndexDefinitionTests

private Table parent = new Table("people");


[InlineData(IndexMethod.btree, true)]
[InlineData(IndexMethod.gin, false)]
[InlineData(IndexMethod.brin, false)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Npgsql;
using Xunit;

namespace Weasel.Postgresql.Tests.Tables.Indexes;

[Collection("fts_deltas")]
public class FullTextIndexDeltasTests(): IndexDeltasDetectionContext("fts_deltas", "users")
{
[PgVersionTargetedFact(MinimumVersion = "10.0")]
public Task wholedoc_fts_index_comparison_works()
{
theTable.ModifyColumn("data").AddFullTextIndex();

return AssertNoDeltasAfterPatching();
}

[PgVersionTargetedFact(MinimumVersion = "10.0")]
public Task fts_index_comparison_must_take_into_account_automatic_cast()
{
theTable.ModifyColumn("data").AddFullTextIndex(documentConfig: "(data ->> 'Name')");

return AssertNoDeltasAfterPatching();
}

[PgVersionTargetedFact(MinimumVersion = "10.0")]
public Task multifield_fts_index_comparison_must_take_into_account_automatic_cast()
{
theTable.ModifyColumn("data")
.AddFullTextIndex(documentConfig: "((data ->> 'FirstName') || ' ' || (data ->> 'LastName'))");

return AssertNoDeltasAfterPatching();
}

[PgVersionTargetedFact(MinimumVersion = "10.0")]
public async Task modified_fts_index_comparison_must_generate_index_update()
{
theTable.ModifyColumn("data").AddFullTextIndex(documentConfig: "(data ->> 'Name')");

await CreateSchemaObjectInDatabase(theTable);

theTable.Indexes.Clear();

theTable.ModifyColumn("data")
.AddFullTextIndex(documentConfig: "((data ->> 'FirstName') || ' ' || (data ->> 'LastName'))");

await AssertIndexUpdate($"{theTable.Identifier.Name}_idx_fts");
}

[PgVersionTargetedFact(MinimumVersion = "10.0")]
public async Task modified_fts_index_by_regConfig_comparison_must_generate_index_update()
{
const string documentConfig = "(data ->> 'Name')";
theTable.ModifyColumn("data").AddFullTextIndex(documentConfig: documentConfig);

await CreateSchemaObjectInDatabase(theTable);

theTable.Indexes.Clear();

const string newRegConfig = "italian";
theTable.ModifyColumn("data")
.AddFullTextIndex(newRegConfig, documentConfig);

await AssertIndexRecreation(
$"{theTable.Identifier.Name}_{newRegConfig}_idx_fts",
$"{theTable.Identifier.Name}_idx_fts"
);
}


[PgVersionTargetedFact(MinimumVersion = "10.0")]
public async Task modified_fts_index_with_customIndex_by_regConfig_comparison_must_generate_index_update()
{
const string documentConfig = "(data ->> 'Name')";
const string indexName = "custom_index_name";

theTable.ModifyColumn("data").AddFullTextIndex(documentConfig: documentConfig, indexName: indexName);

await CreateSchemaObjectInDatabase(theTable);

theTable.Indexes.Clear();

const string newRegConfig = "italian";
theTable.ModifyColumn("data")
.AddFullTextIndex(newRegConfig, documentConfig, indexName: indexName);

await AssertIndexUpdate(indexName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using Shouldly;
using Weasel.Postgresql.Tables;
using Weasel.Postgresql.Tables.Indexes;
using Xunit;

namespace Weasel.Postgresql.Tests.Tables.Indexes;

public class FullTextIndexTests
{
private const string TablePrefix = "mt_";
private const string DataDocumentConfig = "data";
private static readonly PostgresqlObjectName TableName = new("public", "mt_doc_target");
private readonly Table parent = new(TableName);

[Fact]
public void creating_a_full_text_index_should_create_the_index_on_the_table()
{
var index = new FullTextIndexDefinition(TableName, DataDocumentConfig, indexPrefix: TablePrefix);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX {TableName.Name}_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('english',data));"
);
}

[Fact]
public void
creating_a_full_text_index_with_custom_indexName_without_tableprefix_should_create_the_index_on_the_table()
{
var indexName = "custom_index_name";
var index = new FullTextIndexDefinition(
TableName,
DataDocumentConfig,
indexName: indexName,
indexPrefix: TablePrefix
);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX {TablePrefix}{indexName} ON {TableName.QualifiedName} USING gin (to_tsvector('english',data));"
);
}

[Fact]
public void
creating_a_full_text_index_with_custom_indexName_without_prefix_should_create_the_index_on_the_table()
{
var indexName = "custom_index_name";
var index = new FullTextIndexDefinition(
TableName,
DataDocumentConfig,
indexName: indexName
);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX {indexName} ON {TableName.QualifiedName} USING gin (to_tsvector('english',data));"
);
}

[Fact]
public void
creating_a_full_text_index_with_custom_document_configuration_should_create_the_index_without_regConfig_in_indexname_custom_document_configuration()
{
const string documentConfig = "(data ->> 'AnotherString' || ' ' || 'test')";

var index = new FullTextIndexDefinition(TableName, documentConfig);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX {TableName.Name}_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('english',{documentConfig}));"
);
}

[Fact]
public void
creating_a_full_text_index_with_custom_document_configuration_and_custom_regConfig_should_create_the_index_with_custom_regConfig_in_indexname_custom_document_configuration()
{
const string documentConfig = "(data ->> 'AnotherString' || ' ' || 'test')";
const string regConfig = "french";

var index = new FullTextIndexDefinition(
TableName,
documentConfig,
regConfig: regConfig, indexPrefix: TablePrefix);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX mt_doc_target_{regConfig}_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('{regConfig}',{documentConfig}));"
);
}

[Fact]
public void
creating_a_full_text_index_with_single_member_should_create_the_index_without_regConfig_in_indexname_and_member_selectors()
{
const string documentConfig = "(data ->> 'SomeProperty')";

var index = new FullTextIndexDefinition(
TableName,
documentConfig,
indexPrefix: TablePrefix);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX mt_doc_target_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('english',{documentConfig}));"
);
}

[Fact]
public void
creating_a_full_text_index_with_multiple_members_should_create_the_index_without_regConfig_in_indexname_and_members_selectors()
{
const string documentConfig = "((data ->> 'SomeProperty') || ' ' || (data ->> 'AnotherProperty'))";

var index = new FullTextIndexDefinition(
TableName,
documentConfig,
indexPrefix: TablePrefix);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX mt_doc_target_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('english',{documentConfig}));"
);
}

[Fact]
public void
creating_a_full_text_index_with_multiple_members_and_custom_configuration_should_create_the_index_with_custom_configuration_and_members_selectors()
{
const string indexName = "custom_index_name";
const string regConfig = "french";

const string documentConfig = "((data ->> 'SomeProperty') || ' ' || (data ->> 'AnotherProperty'))";

var index = new FullTextIndexDefinition(
TableName,
documentConfig,
regConfig,
indexName,
TablePrefix
);

index.ToDDL(parent).ShouldBe(
$"CREATE INDEX {TablePrefix}{indexName} ON {TableName.QualifiedName} USING gin (to_tsvector('{regConfig}',{documentConfig}));"
);
}

[Fact]
public void
creating_multiple_full_text_index_with_different_regConfigs_and_custom_data_config_should_create_the_indexes_with_different_recConfigs()
{
// Given
const string frenchRegConfig = "french";
const string frenchdocumentConfig = "(data ->> 'SomeProperty')";

const string italianRegConfig = "italian";
const string italiandocumentConfig = "(data ->> 'AnotherProperty')";

var column = parent.AddColumn(new TableColumn("data", "jsonb"));

// When
column.AddFullTextIndex(frenchRegConfig, frenchdocumentConfig);
column.AddFullTextIndex(italianRegConfig, italiandocumentConfig);

// Then
parent.Indexes.Count.ShouldBe(2);

var frenchIndex = parent.Indexes.First();
var italianIndex = parent.Indexes[1];

frenchIndex.ToDDL(parent).ShouldBe(
$"CREATE INDEX mt_doc_target_{frenchRegConfig}_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('{frenchRegConfig}',{frenchdocumentConfig}));"
);
italianIndex.ToDDL(parent).ShouldBe(
$"CREATE INDEX mt_doc_target_{italianRegConfig}_idx_fts ON {TableName.QualifiedName} USING gin (to_tsvector('{italianRegConfig}',{italiandocumentConfig}));"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Shouldly;
using Weasel.Core;
using Weasel.Postgresql.Tables;

namespace Weasel.Postgresql.Tests.Tables.Indexes;

public abstract class IndexDeltasDetectionContext: IntegrationContext
{
protected Table theTable;

public override Task InitializeAsync() =>
ResetSchema();

protected IndexDeltasDetectionContext(string schemaName, string tableName = "people"): base(schemaName)
{
theTable = new Table($"{schemaName}.{tableName}");
theTable.AddColumn<int>("id").AsPrimaryKey();
theTable.AddColumn<string>("first_name");
theTable.AddColumn<string>("last_name");
theTable.AddColumn<string>("user_name");
theTable.AddColumn<DateTime>("created_datetime");
theTable.AddColumn<DateTimeOffset>("created_datetime_offset");
theTable.AddColumn("data", "jsonb");
}

protected async Task AssertNoDeltasAfterPatching(Table? table = null)
{
table ??= theTable;
await table.ApplyChangesAsync(theConnection);

var delta = await table.FindDeltaAsync(theConnection);

delta.HasChanges().ShouldBeFalse();
}

protected async Task<TableDelta> AssertIndexUpdate(
string indexName,
SchemaPatchDifference difference = SchemaPatchDifference.Update,
Table? table = null
)
{
var delta = await AssertIndexChange(difference, table);

delta.Indexes.Different.ShouldContain(
i => i.Expected.Name == indexName && i.Actual.Name == indexName
);

return delta;
}

protected async Task<TableDelta> AssertIndexRecreation(
string oldName,
string? indexName = null,
SchemaPatchDifference difference = SchemaPatchDifference.Update,
Table? table = null
)
{
indexName ??= oldName;

var delta = await AssertIndexChange(difference, table);

delta.Indexes.Extras.ShouldContain(i => i.Name == indexName);
delta.Indexes.Missing.ShouldContain(i => i.Name == oldName);

return delta;
}

private async Task<TableDelta> AssertIndexChange(SchemaPatchDifference difference, Table? table)
{
table ??= theTable;

var delta = await table.FindDeltaAsync(theConnection);

delta.HasChanges().ShouldBeTrue();

delta.Indexes.Difference().ShouldBe(difference);
return delta;
}
}
Loading
Loading