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

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

Open
alierdogan70 opened this issue Aug 19, 2020 · 8 comments
Labels

Comments

@alierdogan70
Copy link

alierdogan70 commented Aug 19, 2020

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();
@lbnascimento
Copy link
Contributor

@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.

@alierdogan70
Copy link
Author

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.

@lbnascimento
Copy link
Contributor

@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().

@alierdogan70
Copy link
Author

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

@lbnascimento
Copy link
Contributor

@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?

@sinanbuyukbayrak
Copy link

sinanbuyukbayrak commented Sep 19, 2020

@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.

@lbnascimento
Copy link
Contributor

@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?

@alierdogan70
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants