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

Fix dropping of sequence and dependent column #28361

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class MigrationsModelDiffer : IMigrationsModelDiffer
{
typeof(DropIndexOperation),
typeof(DropPrimaryKeyOperation),
typeof(DropSequenceOperation),
typeof(DropUniqueConstraintOperation),
typeof(DropCheckConstraintOperation)
};
Expand Down Expand Up @@ -131,6 +130,7 @@ public virtual IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel
var dropColumnOperations = new List<MigrationOperation>();
var dropComputedColumnOperations = new List<MigrationOperation>();
var dropTableOperations = new List<DropTableOperation>();
var dropSequenceOperations = new List<MigrationOperation>();
var ensureSchemaOperations = new List<MigrationOperation>();
var createSequenceOperations = new List<MigrationOperation>();
var createTableOperations = new List<CreateTableOperation>();
Expand Down Expand Up @@ -173,6 +173,10 @@ public virtual IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel
{
dropTableOperations.Add((DropTableOperation)operation);
}
else if (type == typeof(DropSequenceOperation))
{
dropSequenceOperations.Add((DropSequenceOperation)operation);
}
else if (type == typeof(EnsureSchemaOperation))
{
ensureSchemaOperations.Add(operation);
Expand Down Expand Up @@ -312,6 +316,7 @@ public virtual IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel
.Concat(sourceDataOperations)
.Concat(dropComputedColumnOperations)
.Concat(dropColumnOperations)
.Concat(dropSequenceOperations)
.Concat(ensureSchemaOperations)
.Concat(renameTableOperations)
.Concat(renameOperations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,54 @@ public virtual async Task Move_sequence_into_default_schema()
EXEC(N'ALTER SCHEMA [' + @defaultSchema + N'] TRANSFER [TestSequenceSchema].[TestSequence];');");
}

[ConditionalFact]
public async Task Create_sequence_and_dependent_column()
{
await Test(
builder => builder.Entity("People").Property<int>("Id"),
builder => { },
builder =>
{
builder.HasSequence<int>("TestSequence");
builder.Entity("People").Property<int>("SeqProp").HasDefaultValueSql("NEXT VALUE FOR TestSequence");
},
model =>
{
var sequence = Assert.Single(model.Sequences);
Assert.Equal("TestSequence", sequence.Name);
});

AssertSql(
@"CREATE SEQUENCE [TestSequence] AS int START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;",
//
@"ALTER TABLE [People] ADD [SeqProp] int NOT NULL DEFAULT (NEXT VALUE FOR TestSequence);");
}

[ConditionalFact]
public async Task Drop_sequence_and_dependent_column()
{
await Test(
builder => builder.Entity("People").Property<int>("Id"),
builder =>
{
builder.HasSequence<int>("TestSequence");
builder.Entity("People").Property<int>("SeqProp").HasDefaultValueSql("NEXT VALUE FOR TestSequence");
},
builder => { },
model => Assert.Empty(model.Sequences));

AssertSql(
@"DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[People]') AND [c].[name] = N'SeqProp');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [People] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [People] DROP COLUMN [SeqProp];",
//
@"DROP SEQUENCE [TestSequence];");
}

public override async Task InsertDataOperation()
{
await base.InsertDataOperation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,17 @@ public void Add_SequenceHiLo_with_seed_data()
downOps => Assert.Collection(
downOps,
o =>
{
var operation = Assert.IsType<DropSequenceOperation>(o);
Assert.Equal("dbo", operation.Schema);
Assert.Equal("EntityFrameworkHiLoSequence", operation.Name);
},
o =>
{
var m = Assert.IsType<DeleteDataOperation>(o);
AssertMultidimensionalArray(
m.KeyValues,
v => Assert.Equal(43, v));
},
o =>
{
var operation = Assert.IsType<DropSequenceOperation>(o);
Assert.Equal("dbo", operation.Schema);
Assert.Equal("EntityFrameworkHiLoSequence", operation.Name);
}));

[ConditionalFact]
Expand Down