Home

Awesome

LiteDB.Migration

NuGet NuGet GitHub Actions Workflow Status License

LiteDB.Migration is a library for migrating schema changes in LiteDB databases.

Features

Getting Started

Installation

LiteDB.Migration is available as a NuGet package. You can install it via the NuGet Package Manager or the CLI:

dotnet add package LiteDB.Migration

Basic Usage

Get started by following the steps below. For a full example, see the Tests

  1. Define Your Models: Start by defining your document models. For each version change that requires a migration, prepare a new version of the model.
// Initial model
public class EntryModel
{
    public int Id { get; set; }
    public string OldProperty { get; set; }
}

// The latest version of the model, which is actively used in your app
public class CurrentModel
{
    public int Id { get; set; }
    public string NewestProperty { get; set; }
}
  1. Seed Data: Seed your database with the original model.
 // seed db
using (var db = new LiteDatabase("YourDatabase.db"))
{
    var col = db.GetCollection<EntryModel>("ModelX");
    col.Insert(new EntryModel { Id = 1, OldProperty = "OldValue" });
}
  1. Define and apply Migrations: Define your migrations using the MigrationContainer class and apply them using the MigrationContainer class.<br/> Whenever you want to change the model, you can define a new migration using the WithMigration method.
 // apply migration
using (var db = new LiteDatabase("YourDatabase.db"))
{
    var container = new MigrationContainer(migConfig =>
    {
        migConfig.Collection<CurrentModel>("ModelX", config => config
            .StartWithModel<EntryModel>()
            .WithMigration(x => new
            {
                x.Id,
                NewProperty = "New-" + x.OldProperty
            })
            .WithMigration(x => new
            {
                x.Id,
                NewestProperty = "New-" + x.NewProperty
            })
        );
    });

    container.Apply(db);
}
  1. Query Data: Query your database using the new model.
// verify migration
using (var db = new LiteDatabase("YourDatabase.db"))
{
    var col = db.GetCollection<CurrentModel>("ModelX");
    var model = col.FindById(1);

    Console.WriteLine($"Id: {model.Id}, NewestProperty: {model.NewestProperty}");
    if (model.Id != 1 || model.NewestProperty != "New-New-OldValue")
    {
        throw new Exception("Migration failed");
    }
    else
    {
        Console.WriteLine("Migration succeeded");
    }
}

Advanced Usage

For larger (enterprise) applications, you may want to use a more structured approach to migrations. In this case, you can use the please see the Full Example (Wiki page coming soon).

Contributing

Contributions are welcome! Feel free to submit

If you are motivated to create something like dotnet-ef cli tool, feel free to do so and submit a pull request :)

License

LiteDB.Migration is released under the MIT License. See the LICENSE file for more details.