Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the migration of individual types within a collection #26 #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Mongo.Migration.Test.Core/Mongo.Migration.Test.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="NSubstitute" Version="4.1.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="Mongo2Go" Version="2.2.11" />
Expand Down
12 changes: 9 additions & 3 deletions Mongo.Migration.Test/IntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using LightInject;
using Mongo.Migration.Migrations.Adapters;
using Mongo.Migration.Startup;
using Mongo.Migration.Startup.Static;
using Mongo2Go;
Expand All @@ -9,8 +11,10 @@ namespace Mongo.Migration.Test
{
public class IntegrationTest : IDisposable
{
protected const string DatabaseName = "PerformanceTest";
protected const string CollectionName = "Test";
protected IMongoClient _client;

protected IContainerAdapter _containerAdapter;
protected IComponentRegistry _components;

protected MongoDbRunner _mongoToGoRunner;
Expand All @@ -20,9 +24,11 @@ protected void OnSetUp()
_mongoToGoRunner = MongoDbRunner.Start();
_client = new MongoClient(_mongoToGoRunner.ConnectionString);

_client.GetDatabase("PerformanceTest").CreateCollection("Test");
_client.GetDatabase(DatabaseName).CreateCollection(CollectionName);

_containerAdapter = new LightInjectAdapter(new ServiceContainer());

_components = new ComponentRegistry( new MongoMigrationSettings() {ConnectionString = _mongoToGoRunner.ConnectionString, Database = "PerformanceTest"});
_components = new ComponentRegistry( new MongoMigrationSettings() {ConnectionString = _mongoToGoRunner.ConnectionString, Database = DatabaseName }, _containerAdapter);
_components.RegisterComponents(_client);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Mongo.Migration.Documents.Attributes;
using Mongo.Migration.Documents.Locators;
using Mongo.Migration.Migrations;
using MongoDB.Bson;
using MongoDB.Driver;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Mongo.Migration.Documents;
using Mongo.Migration.Test.TestDoubles;
using Mongo.Migration.Migrations.Locators;

namespace Mongo.Migration.Test.Migrations
{
public class CollectionMigrationRunner_when_migrating_a_typed_document : IntegrationTest
{
private ICollectionMigrationRunner _runner;
private IMongoCollection<BsonDocument> _collection;

[SetUp]
public async Task SetUp()
{

base.OnSetUp();

//mock ICollectionLocator for migration Type to execute
var collectionLocatorMock = new Mock<ICollectionLocator>();
collectionLocatorMock.Setup(x => x.GetLocatesOrEmpty())
.Returns(new Dictionary<Type, CollectionLocationInformation>()
{
{typeof(Car), new CollectionLocationInformation(DatabaseName,CollectionName)}
});

var migrationLocatorMock = new Mock<IMigrationLocator>();
migrationLocatorMock.Setup(x => x.GetMigrationsFromTo(It.Is<Type>(t => t==typeof(Car)),
It.IsAny<DocumentVersion>(),It.IsAny<DocumentVersion>()))
.Returns(new[] {new CarMigration_0_0_1() });

migrationLocatorMock.Setup(x => x.GetLatestVersion(It.Is<Type>(t => t == typeof(Car))))
.Returns(new DocumentVersion(0, 0, 1));

_containerAdapter.RegisterInstance<ICollectionLocator>(collectionLocatorMock.Object);
_containerAdapter.RegisterInstance<IMigrationLocator>(migrationLocatorMock.Object);

_collection = _client.GetDatabase(DatabaseName).GetCollection<BsonDocument>(CollectionName);
await SeedDatabaseAsync();

_runner = _components.Get<ICollectionMigrationRunner>();

}

private async Task SeedDatabaseAsync()
{
var vehicleData = new[]
{
GetCar(),
GetBoat()
};

await _collection.InsertManyAsync(vehicleData);
}


[TearDown]
public void TearDown()
{
this.Dispose();
}

[Test] public async Task when_migrating_a_typed_document_then_only_that_type_is_returned_in_the_batch()
{
_runner.RunAll();
var results = (await _collection.FindAsync(FilterDefinition<BsonDocument>.Empty)).ToList();
var migratedResult = results.Where(x => x.Contains("Version")).SingleOrDefault();

results.Count().Should().Be(2);
migratedResult.Should().NotBeNull();
migratedResult.Should().BeEquivalentTo(GetCarWithVersion());
}

#region testData
private static BsonDocument GetCar()
{
return new BsonDocument { { "_id", new ObjectId("5e9ff3856c9cab88f0a1cf1f") }, { "_t", new BsonArray() { "Vehicle", "Car" } }, { "Doors", 3 }, { "Type", "Cabrio" }, };
}

private static BsonDocument GetCarWithVersion()
{
var car = GetCar();
car.Add(new BsonElement("Version", "0.0.1"));
return car;
}

private static BsonDocument GetBoat()
{
return new BsonDocument { { "_id", new ObjectId("5e9ff3856c9cab88f0a1cf2f") }, { "_t", new BsonArray() { "Vehicle", "Boat" } }, { "Propellers", 3 }, { "Type", "Bertram 35" }, };
}


#endregion

}
}
16 changes: 16 additions & 0 deletions Mongo.Migration.Test/Mongo.Migration.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<HintPath>..\packages\FluentAssertions.5.6.0\lib\net47\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="LightInject, Version=6.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\LightInject.6.4.0\lib\net46\LightInject.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Mongo2Go, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\Mongo2Go.2.2.11\lib\netstandard1.6\Mongo2Go.dll</HintPath>
Expand All @@ -61,6 +64,9 @@
<HintPath>..\packages\MongoDB.Driver.Core.2.8.0\lib\net452\MongoDB.Driver.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.13.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.13.1\lib\net45\Moq.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="NSubstitute, Version=4.1.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca">
<HintPath>..\packages\NSubstitute.4.1.0\lib\net46\NSubstitute.dll</HintPath>
Expand All @@ -76,6 +82,9 @@
<Private>True</Private>
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.IO, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Linq.Expressions, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Private>True</Private>
Expand All @@ -84,6 +93,7 @@
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.Tasks, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
<Private>True</Private>
Expand All @@ -98,6 +108,10 @@
<ItemGroup>
<Compile Include="Documents\Locators\AttributeMigrationLocator_when_locate.cs" />
<Compile Include="IntegrationTest.cs" />
<Compile Include="TestDoubles\WithInheritance\Boat.cs" />
<Compile Include="TestDoubles\WithInheritance\Car.cs" />
<Compile Include="TestDoubles\WithInheritance\CarMigration_0_0_1.cs" />
<Compile Include="Migrations\CollectionRunner_when_migrating_a_typed_document.cs" />
<Compile Include="Migrations\Locators\TypeMigrationLocator_when_locate.cs" />
<Compile Include="Migrations\MigrationRunner_when_migrating_down.cs" />
<Compile Include="Migrations\Migration_when_migrating.cs" />
Expand Down Expand Up @@ -127,8 +141,10 @@
<Compile Include="TestDoubles\TestDocumentWithTwoMigration_0_0_1.cs" />
<Compile Include="TestDoubles\TestDocumentWithOneMigration_0_0_1.cs" />
<Compile Include="TestDoubles\TestDocumentWithTwoMigration.cs" />
<Compile Include="TestDoubles\WithInheritance\Vehicle.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void TearDown()
public void When_entity_is_document_Then_provide_serializer()
{
// Arrange
var provider = _components.Get<MigrationInterceptorProvider>();
var provider = _components.Get<IMigrationInterceptorProvider>();

// Act
var serializer = provider.GetSerializer(typeof(TestDocumentWithOneMigration));
Expand All @@ -38,7 +38,7 @@ public void When_entity_is_document_Then_provide_serializer()
public void When_entity_is_not_document_Then_provide_null()
{
// Arrange
var provider = _components.Get<MigrationInterceptorProvider>();
var provider = _components.Get<IMigrationInterceptorProvider>();

// Act
var serializer = provider.GetSerializer(typeof(TestClass));
Expand Down
8 changes: 8 additions & 0 deletions Mongo.Migration.Test/TestDoubles/WithInheritance/Boat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Mongo.Migration.Test.TestDoubles
{
internal class Boat : Vehicle
{
public int Propellers { get; set; }

}
}
11 changes: 11 additions & 0 deletions Mongo.Migration.Test/TestDoubles/WithInheritance/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mongo.Migration.Documents.Attributes;

namespace Mongo.Migration.Test.TestDoubles
{

[StartUpVersion("0.0.1")]
internal class Car : Vehicle
{
public int Doors { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Mongo.Migration.Migrations;
using MongoDB.Bson;

namespace Mongo.Migration.Test.TestDoubles
{
internal class CarMigration_0_0_1 : Migration<Car>
{
public CarMigration_0_0_1() : base("0.0.1")
{
}

public override void Up(BsonDocument document)
{
}

public override void Down(BsonDocument document)
{
}
}
}
10 changes: 10 additions & 0 deletions Mongo.Migration.Test/TestDoubles/WithInheritance/Vehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mongo.Migration.Documents.Attributes;
using Mongo.Migration.Documents;
using MongoDB.Bson;

namespace Mongo.Migration.Test.TestDoubles
{
internal class Vehicle : Document
{
}
}
19 changes: 19 additions & 0 deletions Mongo.Migration.Test/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
2 changes: 2 additions & 0 deletions Mongo.Migration.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<package id="Castle.Core" version="4.4.0" targetFramework="net47" />
<package id="DnsClient" version="1.2.0" targetFramework="net47" />
<package id="FluentAssertions" version="5.6.0" targetFramework="net47" />
<package id="LightInject" version="6.4.0" targetFramework="net47" />
<package id="Mongo2Go" version="2.2.11" targetFramework="net47" />
<package id="MongoDB.Bson" version="2.8.0" targetFramework="net47" />
<package id="MongoDB.Driver" version="2.8.0" targetFramework="net47" />
<package id="MongoDB.Driver.Core" version="2.8.0" targetFramework="net47" />
<package id="Moq" version="4.13.1" targetFramework="net47" />
<package id="NSubstitute" version="4.1.0" targetFramework="net47" />
<package id="NUnit" version="3.11.0" targetFramework="net47" />
<package id="System.Buffers" version="4.5.0" targetFramework="net47" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Mongo.Migration.Documents.Locators
{
internal class RuntimeVersionLocator : AbstractLocator<DocumentVersion, Type>, IRuntimeVersionLocator
public class RuntimeVersionLocator : AbstractLocator<DocumentVersion, Type>, IRuntimeVersionLocator
{
public override DocumentVersion? GetLocateOrNull(Type identifier)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Mongo.Migration.Documents.Locators
{
internal class StartUpVersionLocator : AbstractLocator<DocumentVersion, Type>, IStartUpVersionLocator
public class StartUpVersionLocator : AbstractLocator<DocumentVersion, Type>, IStartUpVersionLocator
{
public override DocumentVersion? GetLocateOrNull(Type identifier)
{
Expand Down
17 changes: 13 additions & 4 deletions Mongo.Migration/Migrations/CollectionMigrationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Mongo.Migration.Migrations
{
internal class CollectionMigrationRunner : ICollectionMigrationRunner
public class CollectionMigrationRunner : ICollectionMigrationRunner
{
private readonly IMongoClient _client;

Expand Down Expand Up @@ -126,12 +126,21 @@ private FilterDefinition<BsonDocument> CreateQueryForRelevantDocuments(
{
var currentVersion = _versionService.GetCurrentOrLatestMigrationVersion(type);

var existFilter = Builders<BsonDocument>.Filter.Exists(_versionService.GetVersionFieldName(), false);
var notEqualFilter = Builders<BsonDocument>.Filter.Ne(
var versionExistFilter = Builders<BsonDocument>.Filter.Exists(_versionService.GetVersionFieldName(), false);

var versionNotEqualFilter = Builders<BsonDocument>.Filter.Ne(
_versionService.GetVersionFieldName(),
currentVersion);

return Builders<BsonDocument>.Filter.Or(existFilter, notEqualFilter);
var versionExistsOrDoesntMatchFilter = Builders<BsonDocument>.Filter.Or(versionExistFilter, versionNotEqualFilter);

var typeExistFilter = Builders<BsonDocument>.Filter.Exists("_t", false);
var typeEqualsFilter = Builders<BsonDocument>.Filter.AnyEq(
"_t", type.Name);

var typeExistsOrMatchsFilter = Builders<BsonDocument>.Filter.Or(typeExistFilter, typeEqualsFilter);

return Builders<BsonDocument>.Filter.And(versionExistsOrDoesntMatchFilter, typeExistsOrMatchsFilter);
}
}
}
2 changes: 1 addition & 1 deletion Mongo.Migration/Migrations/ICollectionMigrationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mongo.Migration.Migrations
{
internal interface ICollectionMigrationRunner
public interface ICollectionMigrationRunner
{
void RunAll();
}
Expand Down
4 changes: 4 additions & 0 deletions Mongo.Migration/Migrations/IMigration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Mongo.Migration.Documents;
using MongoDB.Bson;

Expand All @@ -13,5 +14,8 @@ public interface IMigration
void Up(BsonDocument document);

void Down(BsonDocument document);

void Initialise();

}
}
Loading