Skip to content

Commands: A way to see current Migrations status #577

Description

@bricelam

We should enable the following scenarios:

  • What migration have been applied to the database?
  • Which ones are pending?
  • Do I have unscaffolded model changes?

Activity

  1. self-assigned this
    on Aug 21, 2014
  2. added this to the milestone on Aug 22, 2014
  3. modified the milestone: on Nov 25, 2014
  4. added this to the 7.0.0 milestone on Nov 26, 2014
  5. modified the milestones: , 7.0.0, Backlog on Dec 5, 2014
  6. changed the title [-]Migrations PowerShell: A way to see current status[/-] [+]Commands: A way to see current Migraions status[/+] on Aug 20, 2015
  7. changed the title [-]Commands: A way to see current Migraions status[/-] [+]Commands: A way to see current Migrations status[/+] on Oct 8, 2015
  8. self-assigned this
    on Jan 13, 2017
  9. 32 remaining items

  10. br3nt commented on Jul 23, 2019

    @br3nt

    These will be very useful commands!

    I hope you don't mind if I suggest two additional commands for the following scenarios:

    • Check that all applied migrations exist in the assembly
    • Check that pending migrations will be applied chronologically after applied migrations

    An applied migration may be missing from an assembly if a migration from one git branch is applied to the database, and then the user switches to another branch without rolling-back the migration from the other branch. This can happen for example when deploying feature branches to testing environments.
    It would be nice to be able have CI tools test for this scenario and fail before performing a migration.

    Likewise, a pending migration with a timestamp earlier than the timestamp of one or more applied migrations will occur when one git branch contains migrations that have been applied is then merged into another git branch containing pending migrations with earlier timestamps. This also seems to happen when deploying feature branches to testing environments.

    Both of these scenarios can be frustrating to fix, so a command to prevent it from occurring in the first place would be very welcome.

    Both of these scenarios can be checked programatically:

    // Check that all applied migrations exist in the assembly
    var assemblyMigrations = context.Database.GetMigrations();
    var appliedMigrations = context.Database.GetAppliedMigrations();
    var pendingMigrations = context.Database.GetPendingMigrations();
    if (appliedMigrations.Any(a => !assemblyMigrations.Contains(a)))
        throw new Exception("There are applied migrations that do not exist in this assembly. All applied migrations must exist in the assembly. Aborting the migration.");
    // Check that pending migrations will be applied chronologically after applied migrations
    var appliedTimestamps = appliedMigrations.Select(m => Convert.ToUInt64(m.Substring(0, 14)));
    var pendingTimestamps = pendingMigrations.Select(m => Convert.ToUInt64(m.Substring(0, 14)));
    if (appliedTimestamps.Any(a => pendingTimestamps.Any(p => a > p)))
        throw new Exception("There are pending migrations with a timestamp less than the timestamps of one or more applied migrations. Migrations must be applied chronologically. Aborting the migration.");
  11. added
    good first issue
    and removed
    help wanted
    on Aug 5, 2019
  12. modified the milestones: Backlog, 5.0.0 on Aug 11, 2020
  13. bricelam commented on Aug 17, 2020

    @bricelam
    ContributorAuthor

    Splitting out the pending model changes feature into #22105

  14. modified the milestones: 5.0.0, on Aug 18, 2020
  15. modified the milestones: , 5.0.0 on Nov 7, 2020
  16. voroninp commented on May 6, 2024

    @voroninp

    Hm... still no way to list applied migrations?

  17. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

good first issue

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions