dj-nitehawk / MongoDB.Entities

A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management
https://mongodb-entities.com
MIT License
543 stars 69 forks source link

migrations run repeatly #205

Closed li-zheng-hao closed 1 year ago

li-zheng-hao commented 1 year ago

after upgrade to 22.0.0 version , migraions will run repeatly

image

Sample Code:


Console.WriteLine("Hello, World!");

await DB.InitAsync("testdb",
    MongoClientSettings.FromConnectionString(
        "mongodb://root:******@172.10.2.200:30000"));

await DB.MigrateAsync<_0001_Migration_20221103>();

public class _0001_Migration_20221103:IMigration
{
    public async Task UpgradeAsync()
    {
        try
        {
            var m=new TestModel();
            await m.SaveAsync();
            Console.WriteLine("migration success");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

image

lastMigNum is always 0

and in my another project, when program startup , it will throw a exception

maybe it's a bug in Mongo driver? when i downgrade mongodb driver to 2.18.0, this problem disappeared https://github.com/dotnetcore/CAP/issues/1318#issuecomment-1600296880

MongoDB.Driver.MongoConnectionException: An exception occurred while sending a message to the server.
 ---> System.IO.IOException: Unable to write data to the transport connection: 远程主机强迫关闭了一个现有的连接。.
 ---> System.Net.Sockets.SocketException (10054): 远程主机强迫关闭了一个现有的连接。
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
--- End of stack trace from previous location ---
   at MongoDB.Driver.Core.Misc.StreamExtensionMethods.WriteAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Misc.StreamExtensionMethods.WriteBytesAsync(Stream stream, IByteBuffer buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.SendBufferAsync(IByteBuffer buffer, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.SendBufferAsync(IByteBuffer buffer, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.SendMessagesAsync(IEnumerable`1 messages, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.PooledConnection.SendMessagesAsync(IEnumerable`1 messages, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, ICoreSession session, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadOperationExecutor.ExecuteAsync[TResult](IRetryableReadOperation`1 operation, RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.ReadCommandOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
   at MongoDB.Entities.Find`2.ExecuteAsync(CancellationToken cancellation)
   at FB.Webapi.Migration._0005_Migration_20230112.UpgradeAsync() in F:\Code\bjmyBackend\backend_core\FB.AppSrv\Migration\_0005_Migration_20230112.cs:line 23
dj-nitehawk commented 1 year ago

tried to replicate the issue with the following code:

using MongoDB.Entities;

await DB.InitAsync("test", "localhost");

await DB.MigrateAsync<_001_first_migration>();

Console.ReadLine();

public class _001_first_migration : IMigration
{
    public Task UpgradeAsync()
    {
        Console.Write("migration one complete!" + Environment.NewLine);
        return Task.CompletedTask;
    }
}

public class _002_second_migration : IMigration
{
    public Task UpgradeAsync()
    {
        Console.Write("migration two complete!" + Environment.NewLine);
        return Task.CompletedTask;
    }
}

but it's working correctly. only see the console messages during the first app run. subsequent runs do not print the messages and the migrations are not executed. and the migration history collection in the db has these:

[
  {
      "_id" : ObjectId("6492d9b9102a0f2720ea2fe8"),
      "Number" : 2,
      "Name" : "second migration",
      "TimeTakenSeconds" : 0.0001599
  },
  {
      "_id" : ObjectId("6492d9b9102a0f2720ea2fe7"),
      "Number" : 1,
      "Name" : "first migration",
      "TimeTakenSeconds" : 0.0015929
  }
]

send me a self contained repro project that i can debug with a local mongo instance, and i'll look in to it further.

li-zheng-hao commented 1 year ago

i found this problem is related with mongodb server version (4.2.6). if i use 4.4.19 version of mongodb server, everything is ok .

reproduce steps:

  1. using docker: docker run --name mongodb -p 27017:27017 -d mongo:4.2.6 or install mongodb server with same version
  2. run below code more than one time
using MongoDB.Entities;

await DB.InitAsync("testdb");

var lastMigNum = await
    DB.Find<Migration, int>()
        .Sort(m => m.Number, Order.Descending)
        .Project(m => m.Number)
        .ExecuteFirstAsync();

// always 0
Console.WriteLine(lastMigNum);

await DB.MigrateAsync<_0001_Migration_20221103>();

public class _0001_Migration_20221103:IMigration
{
    public async Task UpgradeAsync()
    {
        try
        {
            Console.WriteLine("migration success");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
dj-nitehawk commented 1 year ago

yeah mongo minimum version should be 4.4 with the current driver afaik.