mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.36k stars 1.22k forks source link

[BUG] Opening a V4 database without upgrade parameter corrupt the database #2434

Open viveleltsi opened 4 months ago

viveleltsi commented 4 months ago

Version Creating the database in v4.1.4 Opening the database in v5.0.18

Describe the bug If a database created in liteDB v4.1.4 is opened with liteDB v5.0.18 without the param upgrade=true there is a "File is not a valid LiteDB database format or contains a invalid password." exception. Then the database is corrupt and it's not possible to open it with LiteDB v4.1.4 nor to open it with LiteDB v5.0.18 (even with the upgrading param)

Code to Reproduce

Here the code I use to create the v4 database:

void CreateV4Database()
{
    string dbFolder = @"Data\";
    Directory.CreateDirectory(dbFolder);

    string dbPath = $"{dbFolder}dbTest{liteDBVersion}.db";

    using (var db = new LiteDatabase(dbPath))
    {
        // Create collection
        var col = db.GetCollection<MyObject>("MyCollection");

        var id = ObjectId.NewObjectId();

        var newObject = new MyObject()
        {
            Id = id,
            Name = "test",
        };

        var actualId = col.Insert(newObject);
    }
}

public class MyObject
{
    public object Id { get; set; }
    public string Name { get; set; }
}

And here the code used to try to open the database with LiteDB v5.0.18

void OpenDatabaseNoUpgrade()
{
    string dbFolder = @"Data\";
    Directory.CreateDirectory(dbFolder);

    string dbPath = $"{dbFolder}dbTest4.1.4.0 - Copy.db";
    using (var db = new LiteDatabase($"Filename={dbPath}"))
    {
        Console.WriteLine("Open database");
    }
}

Expected behavior The exception is a good thing but the database should not be edited. The v4 database should stay the same or at least it should be possible to migrate to v5 with the upgrade=true parameter after it failed wihout the parameter

Screenshots/Stacktrace image

Additional context I add the database before and after trying to open with LiteDBV5

databases.zip

petertiedemann commented 3 weeks ago

I am seeing the same with 5.0.20 (just FYI)