Skip to content

Squash migrations #2174

Open

Description

@rowanmiller

It would be good to have the ability to squash several migrations into a single file to help reduce the number of files in a project.

We probably want to keep track of the original list of migration names so that we can reason about this when targeting an existing database that the original migrations were applied to in their un-squashed form.

Activity

  1. added this to the Backlog milestone on May 12, 2015
  2. popcatalin81 commented on May 14, 2015

    @popcatalin81

    @rowanmiller how will squash work? Merge Migrations into one or simply place them in a single file.

    I think merging will be problematic when a target database has partial migrations but not all from a squash.

  3. bricelam commented on May 14, 2015

    @bricelam
    Contributor

    @popcatalin81 I suspect at first, it will simpy concatenate all the operations together into one migration. In the future, it may try and simplify the operations (e.g. renaming A -> B -> C will become just A -> C)

    Correct, "rewriting history" is always a bad idea. Before squashing, you'll have to revert all the migrations you want to squash, squash them, then re-apply the new one. You shouldn't do it if the migrations have been applied on any database other than your local one.

    This operation would be useful while developing a new feature. You could add all the migrations locally you want, but before merging your feature, you could squash them all down into a single migration.

  4. TheMadKow commented on Dec 8, 2015

    @TheMadKow

    👍 For this idea

  5. markusvt commented on Feb 3, 2016

    @markusvt

    I just wanted to suggest that idea also.. the migrations folder gets quite large quite fast if the projects develops over time

  6. dario-hd commented on May 3, 2017

    @dario-hd

    I was wondering if removing them all and creating an "initial migration" would be a better approach. In the end this is what I did recently. Of course the "initial migration" should be executed only on database creation. This will not only reduce the number of files in the project but it will also speed up the initial database creation if you recreate it multiple times e.g. for development and testing purposes. What do you guys think? @rowanmiller @bricelam

  7. pgrm commented on Nov 15, 2017

    @pgrm

    I'm puzzled that this issue is so inactive. How are others solving the issue of the ever growing Migrations folder?

    Could there be at least some best practice described in the documentation @AndriySvyryd ?

  8. bricelam commented on Nov 15, 2017

    @bricelam
    Contributor

    @pgrm I briefly mentioned a strategy in the Migrations docs I'm adding...

  9. replaysMike commented on Mar 29, 2018

    @replaysMike

    This is a common problem we run into every once in a while. It's quite simple to accomplish. If your database is already up to date just delete all the migration files and truncate the dbo.__EFMigrationsHistory table. Generate a new initial create migration and you have now squashed all your migrations. You lose any comments but that's minor if you're needing to do it.

  10. PMExtra commented on Apr 12, 2019

    @PMExtra

    @replaysMike It will lost my custom migration operations. (For example, I set a custom default value for a new field.)

  11. 127 remaining items

  12. aaref-sh commented on Jun 2, 2025

    @aaref-sh

    I'd just created a new tool for that, called "EfMigrationsSquash"
    explore it in this repo

    How to use it:

    • Download it using dotnet tool install --global EfMigrationsSquash if you don't have it already
    • cd to your migration project (or a project where do your migrations live)
    • run in terminal (EfMigrationsSquash), chose the start and end migration files to squash between
    • check the result
  13. added theissue type on Jun 15, 2025
  14. jasekiw commented on Aug 28, 2025

    @jasekiw

    @aaref-sh Isn't this the same thing as the other project proposal mentioned a few comments up? #2174 (comment)

  15. aaref-sh commented on Aug 29, 2025

    @aaref-sh

    @aaref-sh Isn't this the same thing as the other project proposal mentioned a few comments up? #2174 (comment)

    It looks yes, its similar I guess, however, I wasn't lucky enough to find this repo before you mention it, although I searched too long,
    but guess what! Hurray!!, now we have 2 tools to squash our migrations, isn't that awesome?

  16. yepeekai commented on May 13, 2026

    @yepeekai

    With Ai, I asked to put in place a solution to dynamically compile those designer.cs files on demand (roslyn). In my case, I keep the last 5 ones intact and old ones are copied to an embedded text file per migration in the project. It works fine, it adds a delay of about 5s per migration for 20000 lines designer files that needs to be compiled on demand. It works for updating the database at the startup of the app or update-database or script-migration. I am not going to share the code here, but it is easily reproducible with chat gpt or others.

  17. TsengSR commented on May 13, 2026

    @TsengSR

    @yepeekai 100% certain you didn't test it, because migrations are more complicated than just tossing everything into the same file.

    Up/Downs must work as before, also if you have your app at different versions at different customers, they all need be upgraded first to a specific version, otherwise you'd have errors.

    i.e. if you have Migrations A, B and C at customer and you squash A, B, C, D into one file as D', then deploy it, it will run on error because when you roll your squadshed migration to the customer he doesn't have D, but you have D squashed, so this migration will be missing from the customers system.

    What if you need to roll back a deployment? You can't roll to a specific version after you have squashed because your "down" method would roll back everything. Just some AI slop won't help you here.

  18. yepeekai commented on May 13, 2026

    @yepeekai

    I did not squash anything, all migrations are still there and each ones can execute on demand. I did not try the down method since I don't use that, but mainly it hooks on the BuildTargetModel method in the designer.cs Up definitely works. I don't see why Down wouldn't. I am talking about the designer.cs of each migration, which in our case is a problem since it increases CI compile time by a lot. It is mainly to solve that without losing any functionality.

  19. mendozagit commented on Sep 11, 2026

    @mendozagit

    Roslyn is killing my machine 64GB RAM core i9 I have about 200 migrations anyone have been squashing migrations before?

  20. mendozagit commented on Sep 19, 2026

    @mendozagit

    The assembly/project/place doesn't matter, it needs to be compiled anyways, that's the problem.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions