generated from SwissLife-OSS/template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce MongoDB.Extensions.Migration (#62)
Add fist version of Mongo.Extensions.Migration for performing migrations with MongoDB
- Loading branch information
1 parent
7bdccc0
commit 9452f47
Showing
43 changed files
with
1,180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MongoDB.Extensions.Migration; | ||
|
||
namespace Migration; | ||
|
||
public record Customer(string Id, string Name) : IVersioned | ||
{ | ||
public int Version { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Extensions.Migration; | ||
|
||
namespace Migration; | ||
|
||
public class ExampleMigration : IMigration | ||
{ | ||
public int Version => 1; | ||
|
||
public void Up(BsonDocument document) | ||
{ | ||
document["Name"] += " Migrated up to 1"; | ||
} | ||
|
||
public void Down(BsonDocument document) | ||
{ | ||
document["Name"] += " Migrated down to 0"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MongoDB.Driver" Version="2.15.1" /> | ||
<PackageReference Include="MongoDB.Extensions.Migration" Version="1.3.0-preview1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Migration; | ||
using MongoDB.Extensions.Migration; | ||
using MongoDB.Driver; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services | ||
.AddSingleton(_ => new MongoClient("mongodb://localhost:27017")) | ||
.AddTransient<Repository>(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseMongoMigration(m => m | ||
.ForEntity<Customer>(e => e | ||
.AtVersion(1) | ||
.WithMigration(new ExampleMigration()))); | ||
|
||
app.MapGet("/customer/{id}", (string id, Repository repo) => repo.GetAsync(id)); | ||
app.MapPost("/customer/", (Customer customer, Repository repo) => repo.AddAsync(customer)); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using MongoDB.Driver; | ||
using MongoDB.Driver.Linq; | ||
|
||
namespace Migration; | ||
|
||
public class Repository | ||
{ | ||
private readonly IMongoCollection<Customer> _collection; | ||
|
||
public Repository(MongoClient client) | ||
{ | ||
var database = client.GetDatabase("Example1"); | ||
_collection = database.GetCollection<Customer>("customer"); | ||
} | ||
|
||
public Task AddAsync(Customer customer) => _collection.InsertOneAsync(customer); | ||
|
||
public Task<Customer> GetAsync(string id) => _collection.AsQueryable() | ||
.SingleOrDefaultAsync(c => c.Id == id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Migration.Tests/Integration/Scenario1/MigrateDownTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using MongoDB.Extensions.Migration; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Migration.Tests; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Driver; | ||
using MongoDB.Driver.Linq; | ||
using Squadron; | ||
using Xunit; | ||
|
||
namespace MongoMigrationTest.Integration.Scenario1; | ||
|
||
[Collection("SharedMongoDbCollection")] | ||
public class MigrateDownTests | ||
{ | ||
readonly IMongoCollection<TestEntityForDown> _typedCollection; | ||
readonly IMongoCollection<BsonDocument> _untypedCollection; | ||
|
||
public MigrateDownTests(MongoResource resource) | ||
{ | ||
RegisterMongoMigrations(); | ||
IMongoDatabase database = resource.Client.GetDatabase("Scenario1-down"); | ||
_typedCollection = database.GetCollection<TestEntityForDown>("TestEntityForDown"); | ||
_untypedCollection = database.GetCollection<BsonDocument>("TestEntityForDown"); | ||
} | ||
|
||
static void RegisterMongoMigrations() | ||
{ | ||
MigrationOption options = new MigrationOptionBuilder() | ||
.ForEntity<TestEntityForDown>(o => o.AtVersion(0) | ||
.WithMigration(new TestMigration1()) | ||
.WithMigration(new TestMigration2()) | ||
.WithMigration(new TestMigration3())) | ||
.Build(); | ||
var context = new MigrationContext(options, NullLoggerFactory.Instance); | ||
|
||
BsonSerializer.RegisterSerializationProvider(new MigrationSerializerProvider(context)); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_AddRetrieve_NoMigration() | ||
{ | ||
// Arrange | ||
const string input = "Bar"; | ||
await _typedCollection.InsertOneAsync(new TestEntityForDown("1", input)); | ||
|
||
// Act | ||
TestEntityForDown result = await _typedCollection.AsQueryable() | ||
.SingleOrDefaultAsync(c => c.Id == "1"); | ||
|
||
// Assert | ||
result.Foo.Should().Be(input); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_RetrieveAtVersion3_MigratedDownTo0() | ||
{ | ||
// Arrange | ||
await _untypedCollection.InsertOneAsync(new BsonDocument(new Dictionary<string, object> | ||
{ ["_id"] = "id0", ["Foo"] = "Bar", ["Version"] = 3 })); | ||
|
||
// Act | ||
TestEntityForDown result = await _typedCollection.AsQueryable() | ||
.SingleOrDefaultAsync(c => c.Id == "id0"); | ||
|
||
// Assert | ||
result.Foo.Should().Be("Bar Migrated Down to 2 Migrated Down to 1 Migrated Down to 0"); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_RetrieveAtVersion2_MigratedToVersion3() | ||
{ | ||
// Arrange | ||
await _untypedCollection.InsertOneAsync(new BsonDocument(new Dictionary<string, object> | ||
{ ["_id"] = "id1", ["Foo"] = "Bar", ["Version"] = 1 })); | ||
|
||
// Act | ||
TestEntityForDown result = await _typedCollection.AsQueryable() | ||
.SingleOrDefaultAsync(c => c.Id == "id1"); | ||
|
||
// Assert | ||
result.Foo.Should().Be("Bar Migrated Down to 0"); | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
src/Migration.Tests/Integration/Scenario1/MigrateUpTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using MongoDB.Extensions.Migration; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Migration.Tests; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Driver; | ||
using Xunit; | ||
using MongoDB.Driver.Linq; | ||
using Squadron; | ||
|
||
namespace MongoMigrationTest.Integration.Scenario1; | ||
|
||
[Collection("SharedMongoDbCollection")] | ||
public class MigrateUpTests | ||
{ | ||
readonly IMongoCollection<TestEntityForUp> _typedCollection; | ||
readonly IMongoCollection<BsonDocument> _untypedCollection; | ||
|
||
public MigrateUpTests(MongoResource resource) | ||
{ | ||
RegisterMongoMigrations(); | ||
IMongoDatabase database = resource.Client.GetDatabase("Scenario1-up"); | ||
_typedCollection = database.GetCollection<TestEntityForUp>("TestEntityForUp"); | ||
_untypedCollection = database.GetCollection<BsonDocument>("TestEntityForUp"); | ||
} | ||
|
||
static void RegisterMongoMigrations() | ||
{ | ||
MigrationOption options = new MigrationOptionBuilder() | ||
.ForEntity<TestEntityForUp>(o => o | ||
.WithMigration(new TestMigration1()) | ||
.WithMigration(new TestMigration2()) | ||
.WithMigration(new TestMigration3())) | ||
.Build(); | ||
var context = new MigrationContext(options, NullLoggerFactory.Instance); | ||
|
||
BsonSerializer.RegisterSerializationProvider(new MigrationSerializerProvider(context)); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_AddRetrieve_NoMigration() | ||
{ | ||
// Arrange | ||
const string input = "Bar"; | ||
await _typedCollection.InsertOneAsync(new TestEntityForUp("1", input)); | ||
|
||
// Act | ||
var result = await _typedCollection.AsQueryable().SingleOrDefaultAsync(c => c.Id == "1"); | ||
|
||
// Assert | ||
result.Foo.Should().Be(input); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_RetrieveWithoutVersion_MigratedToNewestVersion() | ||
{ | ||
// Arrange | ||
await _untypedCollection.InsertOneAsync(new BsonDocument(new Dictionary<string, object> | ||
{ ["_id"] = "2", ["Foo"] = "Bar" })); | ||
|
||
// Act | ||
TestEntityForUp result = await _typedCollection.AsQueryable().SingleOrDefaultAsync(c => c.Id == "2"); | ||
|
||
// Assert | ||
result.Foo.Should().Be("Bar Migrated Up to 1 Migrated Up to 2 Migrated Up to 3"); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_RetrieveAtNewUnknownVersion_NoMigration() | ||
{ | ||
// Arrange | ||
await _untypedCollection.InsertOneAsync(new BsonDocument(new Dictionary<string, object> | ||
{ ["_id"] = "3", ["Foo"] = "Bar", ["Version"] = 4 })); | ||
|
||
// Act | ||
TestEntityForUp result = await _typedCollection.AsQueryable().SingleOrDefaultAsync(c => c.Id == "3"); | ||
|
||
// Assert | ||
result.Foo.Should().Be("Bar"); | ||
} | ||
|
||
[Fact] | ||
public async Task Scenario1_RetrieveAtVersion2_MigratedToVersion3() | ||
{ | ||
// Arrange | ||
await _untypedCollection.InsertOneAsync(new BsonDocument(new Dictionary<string, object> | ||
{ ["_id"] = "4", ["Foo"] = "Bar", ["Version"] = 2 })); | ||
|
||
// Act | ||
TestEntityForUp result = await _typedCollection.AsQueryable().SingleOrDefaultAsync(c => c.Id == "4"); | ||
|
||
// Assert | ||
result.Foo.Should().Be("Bar Migrated Up to 3"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Migration.Tests/Integration/Scenario1/TestEntityForDown.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MongoDB.Extensions.Migration; | ||
|
||
namespace MongoMigrationTest.Integration.Scenario1; | ||
|
||
public record TestEntityForDown(string Id, string Foo) : IVersioned | ||
{ | ||
public int Version { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MongoDB.Extensions.Migration; | ||
|
||
namespace MongoMigrationTest.Integration.Scenario1; | ||
|
||
public record TestEntityForUp(string Id, string Foo) : IVersioned | ||
{ | ||
public int Version { get; set; } | ||
} |
Oops, something went wrong.