Skip to content

Commit

Permalink
Added test for computed index on DateTime column migration
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jul 22, 2023
1 parent 0a7a351 commit 4f4c04c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
6 changes: 3 additions & 3 deletions docs/documents/indexing/computed-indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using (var session = store.QuerySession())
.FirstOrDefault(x => x.UserName == "somebody");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L21-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-a-simple-calculated-index' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L22-L41' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-a-simple-calculated-index' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

In the configuration shown above, Marten generates a database index in Postgresql:
Expand All @@ -55,7 +55,7 @@ var store = DocumentStore.For(_ =>
_.Schema.For<Target>().Index(x => x.Inner.Color);
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L67-L74' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deep-calculated-index' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L68-L75' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deep-calculated-index' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The configuration above creates an index like this:
Expand Down Expand Up @@ -139,5 +139,5 @@ var store = DocumentStore.For(_ =>
});
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L80-L123' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_customizing-calculated-index' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L81-L124' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_customizing-calculated-index' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/documents/indexing/unique.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ var store = DocumentStore.For(_ =>
});
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L80-L123' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_customizing-calculated-index' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Indexes/computed_indexes.cs#L81-L124' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_customizing-calculated-index' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Same can be configured for Duplicated Field:
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/querying/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ a document body, but in that case you will need to supply the full SQL statement
var sumResults = await session
.QueryAsync<int>("select count(*) from mt_doc_target");
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L374-L379' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_by_full_sql' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L372-L377' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_by_full_sql' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

When querying single JSONB properties into a primitive/value type, you'll need to cast the value to the respective postgres type:
Expand Down
38 changes: 29 additions & 9 deletions src/DocumentDbTests/Indexes/computed_indexes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;
using Weasel.Core;
using Weasel.Postgresql.Tables;
using Xunit;

Expand Down Expand Up @@ -167,23 +168,36 @@ public async Task create_multi_property_string_index_with_casing()
var index = table.IndexFor("mt_doc_target_idx_stringstring_field");

index.ToDDL(table).ShouldBe("CREATE INDEX mt_doc_target_idx_stringstring_field ON computed_indexes.mt_doc_target USING btree (upper((data ->> 'String'), upper((data ->> 'StringField'))));");


}

[Fact]
public void creating_index_using_date_should_work()
public async Task creating_index_using_date_should_work()
{
StoreOptions(_ =>
{
_.Schema.For<Target>().Index(x => x.Date);
_.Schema.For<ApiResponseRecord>().Index(x => x.RequestTimeUtc);
_.AutoCreateSchemaObjects = AutoCreate.All;
});
var diff = await theStore.Storage.Database.ApplyAllConfiguredChangesToDatabaseAsync(AutoCreate.CreateOrUpdate);

var data = Target.GenerateRandomData(100).ToArray();
theStore.BulkInsert(data.ToArray());
diff.ShouldBe(SchemaPatchDifference.Create);

}
var table = await theStore.Tenancy.Default.Database.ExistingTableFor(typeof(ApiResponseRecord));
var index = table.IndexFor("mt_doc_apiresponserecord_idx_request_time_utc");

index.ShouldNotBeNull();

index.ToDDL(table).ShouldBe("CREATE INDEX mt_doc_apiresponserecord_idx_request_time_utc ON computed_indexes.mt_doc_apiresponserecord USING btree (computed_indexes.mt_immutable_timestamp((data ->> 'RequestTimeUtc')));");

using var otherStore = SeparateStore(_ =>
{
_.Schema.For<ApiResponseRecord>().Index(x => x.RequestTimeUtc);
_.AutoCreateSchemaObjects = AutoCreate.All;
});

var diff2 = await otherStore.Storage.Database.ApplyAllConfiguredChangesToDatabaseAsync(AutoCreate.CreateOrUpdate);
diff2.ShouldBe(SchemaPatchDifference.None);
}

[Fact]
public async Task create_index_with_custom_name()
Expand Down Expand Up @@ -277,7 +291,13 @@ public async Task create_multi_index_including_collection()
var index = table.IndexFor("mt_doc_target_idx_user_idstring_list");

index.ToDDL(table).ShouldBe("CREATE INDEX mt_doc_target_idx_user_idstring_list ON computed_indexes.mt_doc_target USING btree (CAST(data ->> 'UserId' as uuid), CAST(data ->> 'StringList' as jsonb));");

}
}

}
public class ApiResponseRecord
{
public Guid Id { get; set; }
public string Request { get; set; }
public string Response { get; set; }
public DateTime RequestTimeUtc { get; set; }
}

0 comments on commit 4f4c04c

Please sign in to comment.