LiteDB.Migration is a library for migrating schema changes in LiteDB databases.
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
Get started by following the steps below. For a full example, see the Tests
// 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; }
}
// seed db
using (var db = new LiteDatabase("YourDatabase.db"))
{
var col = db.GetCollection<EntryModel>("ModelX");
col.Insert(new EntryModel { Id = 1, OldProperty = "OldValue" });
}
MigrationContainer
class and apply them using the MigrationContainer
class.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);
}
// 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");
}
}
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).
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 :)
LiteDB.Migration is released under the MIT License. See the LICENSE file for more details.