Open Closed

Runtime migrations failing after upgrade #7667


User avatar
0
guzman.vila@dualbootpartners.com created
  • ABP Framework version: v8.2.0
  • UI Type: Angular
  • Database System: PostgreSQL
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

I have a project created using the MicroService template and after upgrading from ABP v7.4.2 to v8.2.1 I'm having an issue running the migrations with the CheckAndApplyDatabaseMigrationsAsync. In my solution each MicroService has its own schema instead of a Database, I achieve that just by using the same ConnectionString and changing the SearchPath for each case. That was working without any issues before the upgrade.

Now I was trying to apply the changes related to the IdentityService and whenever it tried to apply it will throw that the Migrations table already exists. I did some troubleshooting and if I try to apply directly with the update-database command I can see that EF is sending a script to validate if the Migrations table exists, but the namespace using is public instead of my schema defined in the ConnectionString. Exception for Update-Database

ConnectionStrings

 "ConnectionStrings": {
    "IdentityService": "Host=localhost;Port=5432;Database=autosled;User ID=postgres;Password=postgres;SearchPath=identity_service",
    "AdministrationService": "Host=localhost;Port=5432;Database=autosled;User ID=postgres;Password=postgres;SearchPath=administration_service",
    "SaasService": "Host=localhost;Port=5432;Database=autosled;User ID=postgres;Password=postgres;SearchPath=saas_service"
  },

After that, I tried to add the schema in the MigrationsHistoryTable and that fixed the issue of applying the migrations with Update-Database.

But whenever I try to run the solution I will get an error that the migration table already exists. In my module, I have the CheckAndApplyDatabaseMigrationsAsync enabled, and its calling to the checker in the Shared.Hosting.Microservices project.

Debugging the LockAndApplyDatabaseMigrationsAsync I can see that pendingMigrations variable has the list of all the migrations even if the Database is up-to-date. Any idea how this issue? Currently the documentation about the migration for v8.2 is down so I'm not sure if there is the answer I'm missing or something different.

Any help is appreciated. Thanks in advanced

Logs

2024-08-06 17:30:56.831 -03:00 [INF] Lock is acquired for db migration and seeding on database named: IdentityService...
2024-08-06 17:30:58.159 -03:00 [ERR] Failed executing DbCommand (7ms) [Parameters=[], CommandType='"Text"', CommandTimeout='30']
CREATE TABLE "__IdentityService_Migrations" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___IdentityService_Migrations" PRIMARY KEY ("MigrationId")
);
2024-08-06 17:30:58.166 -03:00 [WRN] PostgresException has been thrown. The operation will be tried 2 times more. Exception:
42P07: relation "__IdentityService_Migrations" already exists
2024-08-06 17:31:00.326 -03:00 [INF] Completed to save external localizations.
2024-08-06 17:31:12.156 -03:00 [INF] Lock is acquired for db migration and seeding on database named: IdentityService...
2024-08-06 17:31:12.262 -03:00 [ERR] Failed executing DbCommand (2ms) [Parameters=[], CommandType='"Text"', CommandTimeout='30']
CREATE TABLE "__IdentityService_Migrations" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___IdentityService_Migrations" PRIMARY KEY ("MigrationId")
);
2024-08-06 17:31:12.264 -03:00 [WRN] PostgresException has been thrown. The operation will be tried 1 times more. Exception:
42P07: relation "__IdentityService_Migrations" already exists
2024-08-06 17:31:26.709 -03:00 [INF] Lock is acquired for db migration and seeding on database named: IdentityService...
2024-08-06 17:31:26.775 -03:00 [ERR] Failed executing DbCommand (1ms) [Parameters=[], CommandType='"Text"', CommandTimeout='30']
CREATE TABLE "__IdentityService_Migrations" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___IdentityService_Migrations" PRIMARY KEY ("MigrationId")
);
2024-08-06 17:31:26.780 -03:00 [FTL] Autosled.IdentityService.HttpApi.Host terminated unexpectedly!
Volo.Abp.AbpInitializationException: An error occurred during the initialize Volo.Abp.Modularity.OnPostApplicationInitializationModuleLifecycleContributor phase of the module Autosled.IdentityService.IdentityServiceHttpApiHostModule, Autosled.IdentityService.HttpApi.Host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 42P07: relation "__IdentityService_Migrations" already exists. See the inner exception for details.
 ---> Npgsql.PostgresException (0x80004005): 42P07: relation "__IdentityService_Migrations" already exists
   at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
   at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
   at Autosled.Shared.Hosting.Microservices.DbMigrations.EfCore.PendingEfCoreMigrationsChecker`1.LockAndApplyDatabaseMigrationsAsync() in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\EfCore\PendingEfCoreMigrationsChecker.cs:line 83
   at Autosled.Shared.Hosting.Microservices.DbMigrations.EfCore.PendingEfCoreMigrationsChecker`1.LockAndApplyDatabaseMigrationsAsync() in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\EfCore\PendingEfCoreMigrationsChecker.cs:line 91
   at Autosled.Shared.Hosting.Microservices.DbMigrations.PendingMigrationsCheckerBase.TryAsync(Func`1 task, Int32 retryCount) in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\PendingMigrationsCheckerBase.cs:line 22
   at Autosled.Shared.Hosting.Microservices.DbMigrations.PendingMigrationsCheckerBase.TryAsync(Func`1 task, Int32 retryCount) in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\PendingMigrationsCheckerBase.cs:line 30
   at Autosled.Shared.Hosting.Microservices.DbMigrations.PendingMigrationsCheckerBase.TryAsync(Func`1 task, Int32 retryCount) in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\PendingMigrationsCheckerBase.cs:line 37
   at Autosled.Shared.Hosting.Microservices.DbMigrations.PendingMigrationsCheckerBase.TryAsync(Func`1 task, Int32 retryCount) in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\PendingMigrationsCheckerBase.cs:line 37
   at Autosled.Shared.Hosting.Microservices.DbMigrations.EfCore.PendingEfCoreMigrationsChecker`1.CheckAndApplyDatabaseMigrationsAsync() in C:\GIT\autosled-microservices-api\shared\Autosled.Shared.Hosting.Microservices\DbMigrations\EfCore\PendingEfCoreMigrationsChecker.cs:line 53
   at Autosled.IdentityService.DbMigrations.IdentityServiceDatabaseMigrationChecker.CheckAndApplyDatabaseMigrationsAsync() in C:\GIT\autosled-microservices-api\services\identity\src\Autosled.IdentityService.HttpApi.Host\DbMigrations\IdentityServiceDatabaseMigrationChecker.cs:line 39
   at Autosled.IdentityService.IdentityServiceHttpApiHostModule.OnPostApplicationInitializationAsync(ApplicationInitializationContext context) in C:\GIT\autosled-microservices-api\services\identity\src\Autosled.IdentityService.HttpApi.Host\IdentityServiceHttpApiHostModule.cs:line 121
   at Volo.Abp.Modularity.OnPostApplicationInitializationModuleLifecycleContributor.InitializeAsync(ApplicationInitializationContext context, IAbpModule module)
   at Volo.Abp.Modularity.ModuleManager.InitializeModulesAsync(ApplicationInitializationContext context)
  Exception data:
    Severity: ERROR
    SqlState: 42P07
    MessageText: relation "__IdentityService_Migrations" already exists
    File: heap.c
    Line: 1200
    Routine: heap_create_with_catalog
   --- End of inner exception stack trace ---
   at Volo.Abp.Modularity.ModuleManager.InitializeModulesAsync(ApplicationInitializationContext context)
   at Volo.Abp.AbpApplicationBase.InitializeModulesAsync()
   at Volo.Abp.AbpApplicationWithExternalServiceProvider.InitializeAsync(IServiceProvider serviceProvider)
   at Microsoft.AspNetCore.Builder.AbpApplicationBuilderExtensions.InitializeApplicationAsync(IApplicationBuilder app)
   at Autosled.IdentityService.Program.Main(String[] args) in C:\GIT\autosled-microservices-api\services\identity\src\Autosled.IdentityService.HttpApi.Host\Program.cs:line 38

1 Answer(s)
  • User Avatar
    0
    guzman.vila@dualbootpartners.com created

    I was able to find my issue I forgot to add the schema in the EFModule for the MigrationHistroyTable

Made with ❤️ on ABP v9.0.0-preview Updated on September 19, 2024, 10:13