Skip to content

Statement order in down-migration is incorrect #14030

Closed as not planned
Closed as not planned

Description

@bobvandevijver

See the migration below:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateSequence(
        name: "seq_bi_id",
        startValue: 15000000000L);

    migrationBuilder.AlterColumn<long>(
        name: "bi_id",
        table: "animals",
        nullable: false,
        defaultValueSql: "NEXT VALUE FOR seq_bi_id",
        oldClrType: typeof(long));
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropSequence(
        name: "seq_bi_id");
        
    migrationBuilder.AlterColumn<long>(
        name: "bi_id",
        table: "animals",
        nullable: false,
        oldClrType: typeof(long),
        oldDefaultValueSql: "NEXT VALUE FOR seq_bi_id");
}

Generated by this code:

const string biIds = "seq_bi_id";

modelBuilder.HasSequence<long>(biIds)
            .StartsAt(15000000000)
            .IncrementsBy(1);

 modelBuilder.Entity<AnimalEntity>()
            .Property(a => a.BiId)
            .HasDefaultValueSql($"NEXT VALUE FOR {biIds}");

In this scenario, the down migration fails as the execution order is the same as with the Up migration. The sequence cannot be removed, as it is still used as default value sql in the animals table.

It would make sense if by default the generated order is flipped. There are probably exceptions with dependencies etc, but in general reversing the order should always be correct (you want to revert statements, which are coincidentally grouped in a migration). It would at least solve the problem here.

What do you think?

Further technical details

EF Core version: 2.1.3
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: macOS
IDE: Rider

Activity

  1. bricelam commented on Nov 28, 2018

    @bricelam
    Contributor

    Similar to #12726. We need to be smarter about columns with HasDefaultValueSql and HasComputedColumnSql. Although without actually parsing the SQL, we'll never be perfect.

    The workaround is to manually re-order the operations after scaffolding.

  2. self-assigned this
    on Nov 28, 2018
  3. added this to the 3.0.0 milestone on Nov 28, 2018
  4. modified the milestones: 3.0.0, Backlog on Jun 21, 2019
  5. modified the milestones: Backlog, on Aug 21, 2019
  6. bricelam commented on Oct 9, 2019

    @bricelam
    Contributor

    Looking closer, I don't think we should fix this. At a high level, the order we use is:

    • Drop things
    • Rename things
    • Create and alter things

    This is to enable replacing database objects with new ones. I think that if we try to fix this specific case, it will cause other scenarios to fail.

    Short of parsing the SQL (a complexity we desperately wish to avoid), I don't think we can adequately fix this.

  7. removed this from the milestone on Oct 9, 2019
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