From e2e27af2fd019447fef744b16d6077ee79bb673a Mon Sep 17 00:00:00 2001 From: Tobias Grimm Date: Thu, 25 Jul 2024 15:29:07 +0200 Subject: [PATCH] Create temp table for bulk loading with "LIKE" clause Using "LIKE" and "INCLUDING DEFAULTS" is required to e.g. set mt_deleted to the default falue FALSE, otherwise it will be set to NULL. --- src/DocumentDbTests/Writing/bulk_loading.cs | 18 ++++++++++++++++++ .../CodeGeneration/BulkLoaderBuilder.cs | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/DocumentDbTests/Writing/bulk_loading.cs b/src/DocumentDbTests/Writing/bulk_loading.cs index 794ea21e63..5c952fb015 100644 --- a/src/DocumentDbTests/Writing/bulk_loading.cs +++ b/src/DocumentDbTests/Writing/bulk_loading.cs @@ -598,6 +598,24 @@ public async Task can_bulk_insert_mixed_list_of_objects_enlist_transaction_async } } + [Fact] + public async Task can_bulk_insert_soft_deletable_documents_when_using_overwrite_mode() + { + StoreOptions(x => x.Schema.For().SoftDeletedWithIndex()); + + var doc1 = new User(); + var doc2 = new User(); + + var documents = new object[] { doc1, doc2 }; + + await theStore.BulkInsertAsync(documents, BulkInsertMode.OverwriteExisting); + + await using (var querying = theStore.QuerySession()) + { + querying.Query().Count().ShouldBe(2); + } + } + public Task InitializeAsync() { return theStore.Advanced.Clean.DeleteAllDocumentsAsync(); diff --git a/src/Marten/Internal/CodeGeneration/BulkLoaderBuilder.cs b/src/Marten/Internal/CodeGeneration/BulkLoaderBuilder.cs index 08ecd70f00..89943e9234 100644 --- a/src/Marten/Internal/CodeGeneration/BulkLoaderBuilder.cs +++ b/src/Marten/Internal/CodeGeneration/BulkLoaderBuilder.cs @@ -122,6 +122,6 @@ public string OverwriteDuplicatesFromTempTable() public string CreateTempTableForCopying() { - return $"create temporary table {_tempTable} as select * from {_mapping.TableName.QualifiedName} limit 0"; + return $"create temporary table {_tempTable} (like {_mapping.TableName.QualifiedName} including defaults)"; } }