Skip to content

Commit

Permalink
Fix dropping of sequence and dependent column (#28361)
Browse files Browse the repository at this point in the history
Fixes #28349
  • Loading branch information
roji authored Jul 5, 2022
1 parent 43ef882 commit ebe508e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
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 @@ protected virtual IReadOnlyList<MigrationOperation> Sort(
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 @@ protected virtual IReadOnlyList<MigrationOperation> Sort(
{
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 @@ protected virtual IReadOnlyList<MigrationOperation> Sort(
.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 @@ await Test(
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

0 comments on commit ebe508e

Please sign in to comment.