Skip to content

"dotnet ef migrations bundle" => bundle.exe unexpected exit code on failing migrations #27783

Closed as not planned
@mrlux

Description

@mrlux

Question

I'm using the 'new' dotnet ef migrations bundle command to create a bundle migration executable
I package this into a docker file to run as a pre-deployment step of my release process (using argocd).

What I noticed is that when the bundle application runs into an exception like a missing connection string as an example the exit code is 1 (failure). This is expected behavior.

The thing that I did not expect is that when the bundle application starts but hits an exception in one of the migrations itself the exit code is 0 (success). As this exit code passed to the docker container exit code my pre-deployment step thinks all is good and continues the deployment.

I am wondering if I'm doing something wrong as I would expect that the main usage of this bundle application would be in a pre-deployment scenario being:

  • Build/release pipeline
  • Pre-deployment hooks using helm or argocd
  • ....

Proposed solution/change

It would be nice if we could pass an exit code toggle/option to the bundle application on how to respond to failing migrations

efbundle --hard-fail-on-migration-failure

Code

Docker file

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

ENV CONNECTION=''
ENV DATABASE_NAME=''

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Sample.Infrastructure/Sample.Infrastructure.csproj", "Sample.Infrastructure/"]
COPY ["Sample.Core/Sample.Core.csproj", "Sample.Core/"]
RUN dotnet restore "Sample.Infrastructure/Sample.Infrastructure.csproj"
COPY . .
WORKDIR "/src/Sample.Infrastructure"
RUN dotnet build "Sample.Infrastructure.csproj" -c Release -o /app/build

RUN dotnet tool install --global dotnet-ef
ENV PATH="${PATH}:/root/.dotnet/tools"

FROM build AS publish
RUN dotnet ef migrations bundle --project Sample.Infrastructure.csproj -r linux-x64 --verbose --configuration Bundle --self-contained -f -o /app/efbundle

FROM base AS final
WORKDIR /app
COPY --from=publish /app/efbundle .

ENTRYPOINT ./efbundle --prefix-output --verbose --connection "${CONNECTION}" | sed 's/{CATALOG_NAME}/'$DATABASE_NAME'/;'

Stack traces

Log output

info:    Microsoft.Data.SqlClient.SqlException (0x80131904): Reference to database and/or server name in 'samplxxxx.dbo.EVChargers' is not supported in this version of SQL Server.
info:       at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
info:       at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
info:       at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
info:       at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
info:       at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
info:       at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
info:       at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
info:       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
info:       at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
info:       at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
info:       at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
info:       at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
info:       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.ExecuteInternal(String[] args)
info:       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.<>c__DisplayClass6_0.<Configure>b__0(String[] args)
info:       at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
info:       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.Execute(String context, Assembly assembly, Assembly startupAssembly, String[] args)
info:    ClientConnectionId:5e3b07c9-2e34-4232-9787-dce9dd15362a
info:    Error Number:40515,State:1,Class:15
error:   Reference to database and/or server name in 'samplxxxx.dbo.EVChargers' is not supported in this version of SQL Server.

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions