Skip to content
This repository was archived by the owner on Mar 1, 2018. It is now read-only.
This repository was archived by the owner on Mar 1, 2018. It is now read-only.

How to best run Database Migrations #89

Closed

Description

@pwfraley

Maybe I overlooked something, but when building a aspnetcore app with EF core, you would need to be able to run:
dotnet ef database update
Now running this during build time, is not possible, especially if you have multiple stages that you would want to deploy this image to.
I guess best solution would be to run the db migration during startup of the container, but how would you go about doing this, since the final image does not contain the sdk only the runtime?

Activity

  1. bricelam commented on Oct 24, 2017

    @bricelam

    We should look into automating the same workflow used by the Publish Web dialog:

    1. Generate a script on the dev machine during package using dotnet ef migrations script --idempotent
    2. Run the script in the container during deploy using something like sqlcmd
  2. richlander commented on Nov 13, 2017

    @richlander
    Member

    @bricelam Is there an issue filed for that idea somewhere?

  3. bricelam commented on Nov 13, 2017

    @bricelam

    Not that I know of. I'm not sure where it belongs...

  4. richlander commented on Nov 13, 2017

    @richlander
    Member

    Likely an obvious question, but why does the migration need to run at first run? Is the issue that the database may have changed since the container was built?

  5. pwfraley commented on Nov 13, 2017

    @pwfraley
    Author

    @richlander No just an idea, if you have a better suggestion I am open for it :) I just can not do it during build because my build servers do not have access to my production network. Also this seems to be the most common way of doing it. And since the migrations only modify the database if needen it should not hurt anything doing it on container startup (accept for a small delay on app startup, which I could live with).

  6. richlander commented on Nov 13, 2017

    @richlander
    Member

    Have you looked at docker compose?

  7. pwfraley commented on Nov 27, 2017

    @pwfraley
    Author

    @richlander Yes I have, but how would that solve my problem? The docker-compose would also be run on my build / dev Server which have no access to the production database, or did I miss something here?

  8. pwfraley commented on Nov 27, 2017

    @pwfraley
    Author

    Ok now I can put the pieces together, sorry reading all of the comments directly helps.

    So basically during build process I will also run a:

    dotnet ef migrations script -o migrate.sql --idempotent
    

    Then I also copy the generated sql script to the runtime image and also create a shell script to run the generated sql script against the production db server. I guess that would work :)

    The only problem is that I will also need a commandline client for the used database on the runtime image.

    This would be cool to be documented in the README :)

    Thank you all for your help.

  9. FlavioRodr commented on Jan 30, 2018

    @FlavioRodr

    Actually, you are also able to run migrations programmatically and not only through Package Manager Console (PMC). See https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/migrations

    You can call a method in your DbContext to do it:
    DbContext.Database.Migrate()
    using Microsoft.EntityFrameworkCore

    Besides you need to make sure your DB service is up and running before the service tah does this call is bootstrapped. Docker has extensive documentation about this.

  10. bricelam commented on Jan 30, 2018

    @bricelam

    I am not sure about the idempotent file solution since you'll usually do some manual data migration sql scripts (migrationBuilder.Sql) which I don't think will be inserted in the generated file.

    migrationBuilder.Sql() is treated the same as any other operation--it will be included in the script.

  11. FlavioRodr commented on Jan 31, 2018

    @FlavioRodr

    @bricelam you're right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions