efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Migrations called twice

Open depler opened this issue 2 years ago • 3 comments

NpgsqlMigrator.cs contains method MigrateAsync with following code:

await base.MigrateAsync(targetMigration, cancellationToken).ConfigureAwait(false);

...

var reloadTypes = migrations
	.SelectMany(m => m.UpOperations)
	.OfType<AlterDatabaseOperation>()
	.Any(o => o.GetPostgresExtensions().Any() || o.GetPostgresEnums().Any() || o.GetPostgresRanges().Any());

Access to m.UpOperations forces to call BuildOperations:

public virtual IReadOnlyList<MigrationOperation> UpOperations
    => _upOperations ??= BuildOperations(Up);

So all migrations will call Up() - that would second call for each migration, because MigrateAsync is already called in the beginning. This logic can potentially break some migrations, if they relying on "call once" logic

depler avatar Dec 25 '23 14:12 depler

the same issue reported below: https://github.com/dotnet/efcore/issues/33279

mariuszkochanowski avatar Mar 26 '24 06:03 mariuszkochanowski

@depler thanks for reporting... This code was introduced in #2951 to make trigger type reloading after new types are created in the database.

Let's have this looked at on the EF side to see how best to solve it.

roji avatar Mar 26 '24 07:03 roji

Until fix, if you need a work around you can do that for each migration scripts:

   public partial class InitPostGre : Migration
   {
       static bool alreadyExecuted = false;
       /// <inheritdoc />
       protected override void Up(MigrationBuilder migrationBuilder)
       {
           if (alreadyExecuted) return;
           alreadyExecuted = true;

Maleaume avatar Mar 28 '24 20:03 Maleaume