mbdavid / LiteDB

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

[QUESTION] Apply change scripts for new version of my app #1713

Open ruslanmogilevskiy opened 4 years ago

ruslanmogilevskiy commented 4 years ago

Hi,

Preconditions: full .NET 4.6+, asp.net webapi app. Does anyone know a library or an existing code that will run a bunch of my sql scripts upon the application new version deploy (on-demand run) to migrate or update the existing collections\rows after application new version release?

My situation: an app is living and creating the collections and data inside of them. Then I release a new version of the app which has changed the structure or format of the existing collection(s). How to migrate existing data to the new format or change its structure (remove document`s fields, etc)?

My vision: have a list of sql files inside my app; each sql file will have a unique name as a version; have a _version collection and store there the already run sql files; on each app start check this _version collection and run all sql files that are not mentioned there.

But maybe there is an existing implementation?

lbnascimento commented 4 years ago

@ruslanmogilevskiy There is a UserVersion field in the LiteDB datafile header that can be used for this purpose. It is set to 0 when the datafile is created. You could do something like this at the beginning of your code:

switch (db.UserVersion)
{
    case 0:
        //code to migrate from v0 to v1
        db.UserVersion++;
        goto case 1;
    case 1:
        //code to migrate from v1 to v2
        db.UserVersion++;
        goto case 2;
    case 2:
        //code to migrate from v2 to v3
        db.UserVersion++;
        break;
    default:
        break;
}
ruslanmogilevskiy commented 4 years ago

@lbnascimento ,

Thanks, didn't know about it. But my first goal is to find an existing solution for this that covers my needs.