Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop computed columns before non-computed #25238

Merged
1 commit merged into from
Jul 29, 2021
Merged

Conversation

roji
Copy link
Member

@roji roji commented Jul 12, 2021

  • Made DropColumnOperation inherit from ColumnOperation. This was needed to distinguish between dropping computed and non-computed, but it makes sense to expose all column facets in case they matter somehow when dropping the column.
  • SQL Server and PostgreSQL don't allowing referencing a computed column from another computed column, Sqlite and MySQL do. In any case, we won't parse the computed SQL to understand dependencies.

Fixes #25237

@roji roji requested a review from bricelam July 12, 2021 12:05
@bricelam bricelam self-assigned this Jul 28, 2021
Comment on lines 213 to 216
if (string.IsNullOrWhiteSpace(((ColumnOperation)operation).ComputedColumnSql))
{
dropColumnOperations.Add(operation);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of storing this in the migrations operation, let's just use the DiffContext instead. The code here would be something like this:

Suggested change
if (string.IsNullOrWhiteSpace(((ColumnOperation)operation).ComputedColumnSql))
{
dropColumnOperations.Add(operation);
}
if (string.IsNullOrWhiteSpace(diffContext.FindColumn((DropColumnOperation)operation).ComputedColumnSql)
{
dropColumnOperations.Add(operation);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also double-check the behavior of null vs whitespace. I think there's an overload of HasComputedColumnSql() that marks the column as computed, but doesn't actually specify the SQL. Although, it probably throws that it's not defined before it gets here...

Copy link
Member Author

@roji roji Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of storing this in the migrations operation, let's just use the DiffContext instead.

Done, check it out (am still worried we'll need facets in SQL generation at some point for dropping stuff, but we can wait for that to happen).

Also double-check the behavior of null vs whitespace. I think there's an overload of HasComputedColumnSql() that marks the column as computed, but doesn't actually specify the SQL. Although, it probably throws that it's not defined before it gets here...

Seems so - I did a quick console test using this PR code, and got the following exception stack trace (BTW should this be model-validated?):

Unhandled exception. System.InvalidOperationException: The computed column SQL has not been specified for the column 'Name.Blogs'. Specify the SQL before using Entity Framework to create the database schema.
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Initialize(ColumnOperation columnOperation, IColumn column, RelationalTypeMapping typeMapping, Boolean isNullable, IEnumerable`1 migrationsAnnotations, Boolean inline) in /home/roji/projects/efcore/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 1145
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IColumn target, DiffContext diffContext, Boolean inline)+MoveNext() in /home/roji/projects/efcore/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 1090
Test code
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0")
            .EnableSensitiveDataLogging()
            .UseLoggerFactory(ContextLoggerFactory);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Blog>().Property(b => b.Name).HasComputedColumnSql();
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's a valid model--we just can't create the full schema for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that seems... nuanced... But doesn't really matter.

@roji roji force-pushed the roji/DropComputedColumnsFirst branch from 1467c3f to 4c231ff Compare July 29, 2021 11:00
@roji roji requested a review from bricelam July 29, 2021 11:10
@bricelam bricelam removed their assignment Jul 29, 2021
@roji roji force-pushed the roji/DropComputedColumnsFirst branch from 4c231ff to 009c858 Compare July 29, 2021 20:48
@ghost
Copy link

ghost commented Jul 29, 2021

Hello @roji!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost
Copy link

ghost commented Jul 29, 2021

Apologies, while this PR appears ready to be merged, I've been configured to only merge when all checks have explicitly passed. The following integrations have not reported any progress on their checks and are blocking auto-merge:

  1. Azure Pipelines

These integrations are possibly never going to report a check, and unblocking auto-merge likely requires a human being to update my configuration to exempt these integrations from requiring a passing check.

Give feedback on this
From the bot dev team

We've tried to tune the bot such that it posts a comment like this only when auto-merge is blocked for exceptional, non-intuitive reasons. When the bot's auto-merge capability is properly configured, auto-merge should operate as you would intuitively expect and you should not see any spurious comments.

Please reach out to us at [email protected] to provide feedback if you believe you're seeing this comment appear spuriously. Please note that we usually are unable to update your bot configuration on your team's behalf, but we're happy to help you identify your bot admin.

@ghost ghost merged commit fd63d8c into main Jul 29, 2021
@ghost ghost deleted the roji/DropComputedColumnsFirst branch July 29, 2021 22:32
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrations: Drop generated column should come before dropping the columns it's generated from
2 participants