migrating-ravens / RavenMigrations

A small migrations framework to help you manage your RavenDB Instance.
MIT License
53 stars 24 forks source link

Migration exits with success (exit code 0) even when migration fails #67

Closed MichaelDepner closed 6 months ago

MichaelDepner commented 6 months ago

I have noticed that my CI job that runs migrations always reports back that the job succeeded, even if a migration fails.

I tried running a faulty migration locally, and can see that it returns exit code 0 on failure, meaning the terminal sees the command as executed successfully. I assume there must be a try/catch out there that prints the exception info and then stops throwing it up the chain.

Is this something you can help me figure out?

Example - a faulty migration throws an exception: image

And the end of the stack trace, showing that the program ended with exit code 0 - success: image

Thanks in advance for the help / debugging

JudahGabriel commented 6 months ago

I just created a test that creates a migration that fails, then called runner.Run():

[Fact]
public void Failed_migrations_throw()
{
    var options = GetMigrationOptions();
    options.Profiles.Add("migration that throws exception");

    using var store = GetDocumentStore();
    var runner = new MigrationRunner(store, options, new ConsoleLogger());
    Assert.Throws<System.Exception>(() => runner.Run());
}

...
[Migration(6, "migration that throws exception")]    
    public class MigrationThrowsException : Migration
    {
        public override void Up()
        {
            throw new System.Exception("This is failing migration");
        }
    }

It throws the exception as expected. Is your call to runner.Run() inside a try/catch block?

I've committed the code for this test here.

MichaelDepner commented 6 months ago

Thanks for checking it out. I have done some digging, and it turns out I was misunderstanding how dotnet run exit codes work. It returns 0 if it successfully builds and executes your application, and doesn't care what happens inside. Unless you manually pass the exit code along.

So in my case, this was the fix, and it had nothing to do with you:

await runner.InvokeAsync(args);

to

return await runner.InvokeAsync(args);

Thanks for looking into it anyway, I appreciate the help 🙏