LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG] Deleting a non existing collection causes insert operation to fail

Open alierdogan70 opened this issue 6 years ago • 9 comments

Version LiteDb 5.0.8.0 .net RunTime v4.0.30319 VS 2019 Windows Forms Application

Describe the bug

Deleting a non existing collection causes insert operation to fail. InsertBulk returns successful result but data do not get inserted to the database.

public class AddressOfflineReporsitory : IAddressOfflineReporsitory
{      
    public async Task<int> InsertAddresses(IList<Address> addresses)
    {
        var Result = 0;
        await Task<int>.Run(() =>
        {
            using (var db = LiteDBClient.CreateCon(LiteDBClient.DBType.CustomerAccount))
            {
                db.BeginTrans();
                var col = db.GetCollection<Address>();
                col.EnsureIndex(a => a.Name);
                col.EnsureIndex(a => a.No);
                col.EnsureIndex(a => a.Contact);
                Result = col.InsertBulk(addresses);
                db.Commit();
            }

        });
        return Result;
    }

    public async Task DeleteAllAddresses()
    {
        await Task.Run(() =>
        {
            using (var db = LiteDBClient.CreateCon(LiteDBClient.DBType.CustomerAccount))
            {
                
                var col = db.GetCollection<Address>();
                col.DeleteAll();
            }
        });
    }
}

Usage

await addressOfflineReporsitory.DeleteAllAddresses();
var savedcount = await addressOfflineReporsitory.InsertAddresses(ServiceResult.Data.ToList());
logger.Information("{AddressBookRecordCount} Address entry downloaded and saved.", savedcount);

Expected behavior Data to be inserted. Insertbulk method returns correct number although data is not inserted.

Additional context It works as expected when you change delete method as :

if (!db.CollectionExists(nameof(Address))) return;
var col = db.GetCollection<Address>();
col.DeleteAll();

alierdogan70 avatar Aug 19 '20 08:08 alierdogan70

@alierdogan70 I could not reproduce the issue running LiteDB v5.0.8 on .NET Core v3.1.4.

Also, if I understood you correctly, you claim to be using .NET Framework 4.0, which I believe is not possible for two reasons:

  1. LiteDB v5.0.x dropped support for .NET Framework 4.0;
  2. You use async/await, which was only introduced in C# 5, and .NET Framework 4.0 only supported C# 4.

lbnascimento avatar Aug 19 '20 15:08 lbnascimento

Hi @lbnascimento

It is the runtime (CLR) version: LiteDb

Target Framework is : 4.7.2

Sorry for the confusion.


It is not very easy to reproduce the issue. Insert operation needs to be runned immediately after the delete operation as in the example on a brand new database. When I stop the flow during debugging I see database-log file being generated but it disappears after the insert operation.

alierdogan70 avatar Aug 20 '20 11:08 alierdogan70

@alierdogan70 I still haven't been able to reproduce the issue. I'm running on .NET Framework 4.7.2 and I tried running both in debug and in release mode.

The log file is meant to disappear when the LiteDatabase is disposed.

Try calling db.Checkpoint() right after db.Commit().

lbnascimento avatar Aug 20 '20 15:08 lbnascimento

Hi @lbnascimento ,

I created a small application containing the bug. Please delete the database file before every try. If you uncomment AddressRepository.cs line 95 it works properly.

2020-08-21 11_07_03-Form1

LiteDBInsertBulk.zip

I hope this helps

alierdogan70 avatar Aug 21 '20 10:08 alierdogan70

@alierdogan70 This seems to be related to #1762, and your app finally allowed me to reproduce it and to fix it, so thanks for that.

Just to make sure, could you test with the latest master?

lbnascimento avatar Aug 21 '20 13:08 lbnascimento

@lbnascimento I am having the same problem although I tried with latest master. .net 5-rc1, vs 2019 preview.

using var db = new LiteDatabase(new ConnectionString
{
      Filename = "C:\\testing\\taskdb.ld",
      Password = "xxx",
      Connection = ConnectionType.Shared,
      ReadOnly = false
});

var collection = db.GetCollection<BasicGuidValue>("task_guid");

Guid taskMainGuid = Guid.Parse("ac2b28f7-bce7-441c-adbb-7f3e62c89c5c");
var guidBasic = new BasicGuidValue(taskMainGuid);

collection.DeleteAll();
collection.Insert(guidBasic);

// BasicGuidValue obj ->

public class BasicGuidValue
{
     [JsonConstructor]
     [BsonCtor]
     public BasicGuidValue(Guid value)
     {
         Value = value;
     }

     public Guid Value { get; set; }
}

After the code runs, the collection is not created in the database (If not exists). When I remove DeleteAll code line, perfectly created the collection in the database.

--

Same problem, different delete strategy;

using var db = new LiteDatabase(new ConnectionString
{
      Filename = "C:\\testing\\taskdb.ld",
      Password = "xxx",
      Connection = ConnectionType.Shared,
      ReadOnly = false
});
db.DropCollection("task_guid");
var collection = db.GetCollection<BasicGuidValue>("task_guid");

Guid taskMainGuid = Guid.Parse("ac2b28f7-bce7-441c-adbb-7f3e62c89c5c");
var guidBasic = new BasicGuidValue(taskMainGuid);

collection.Insert(guidBasic);

Still not creating the collection.

sinanbuyukbayrak avatar Sep 19 '20 18:09 sinanbuyukbayrak

@alierdogan70 I could not reproduce the issue on .NET Core 3.1. I'm not currently able to test on .NET 5 (I may be in a few days). Is there any way you could test it with .NET Core 3.1?

lbnascimento avatar Sep 21 '20 14:09 lbnascimento

@alierdogan70 I could not reproduce the issue on .NET Core 3.1. I'm not currently able to test on .NET 5 (I may be in a few days). Is there any way you could test it with .NET Core 3.1?

Hi. I am out of town for 3 weeks. I am going to try when I return back.

alierdogan70 avatar Sep 22 '20 05:09 alierdogan70

This is fixed on the dev branch. dev ships as the LiteDB 6.x prereleases.

Please try the latest prerelease (currently 6.0.0-prerelease.81):

dotnet add package LiteDB --prerelease

If the problem still happens on the prerelease, comment here with the version you used and a repro, and we'll reopen the issue.

JKamsker avatar Sep 12 '26 16:09 JKamsker