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

Proposed fix for Optional RestartSequenceOperation.StartValue #26560 #29346

Merged
merged 6 commits into from
Mar 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1805,11 +1805,15 @@ protected virtual void Generate(RestartSequenceOperation operation, IndentedStri
.Append(Code.Literal(operation.Schema));
}

builder
.AppendLine(",")
.Append("startValue: ")
.Append(Code.Literal(operation.StartValue))
.Append(")");
if (operation.StartValue.HasValue)
{
builder
.AppendLine(",")
.Append("startValue: ")
.Append(Code.Literal(operation.StartValue.Value));
}

builder.Append(")");

Annotations(operation.GetAnnotations(), builder);
}
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Migrations/MigrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,12 +1233,12 @@ public MigrationBuilder(string? activeProvider)
/// See <see href="https://aka.ms/efcore-docs-migrations">Database migrations</see> for more information and examples.
/// </remarks>
/// <param name="name">The name of the sequence.</param>
/// <param name="startValue">The value at which the sequence will start, defaulting to 1.</param>
/// <param name="startValue">The value at which the sequence will start. If <see langword="null" /> (the default), the sequence restarts based on the configuration used during creation.</param>
/// <param name="schema">The schema that contains the sequence, or <see langword="null" /> to use the default schema.</param>
/// <returns>A builder to allow annotations to be added to the operation.</returns>
public virtual OperationBuilder<RestartSequenceOperation> RestartSequence(
string name,
long startValue = 1L,
long? startValue = null,
string? schema = null)
{
Check.NotEmpty(name, nameof(name));
Expand Down
12 changes: 10 additions & 2 deletions src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,16 @@ protected virtual IUpdateSqlGenerator SqlGenerator
builder
.Append("ALTER SEQUENCE ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema))
.Append(" RESTART WITH ")
.Append(longTypeMapping.GenerateSqlLiteral(operation.StartValue))
.Append(" RESTART");

if (operation.StartValue.HasValue)
{
builder
.Append(" WITH ")
.Append(longTypeMapping.GenerateSqlLiteral(operation.StartValue.Value));
}

builder
.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);

EndStatement(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RestartSequenceOperation : MigrationOperation
public virtual string? Schema { get; set; }

/// <summary>
/// The value at which the sequence should re-start, defaulting to 1.
/// The value at which the sequence should restart. If <see langword="null" /> (the default), the sequence restarts based on the configuration used during creation.
/// </summary>
public virtual long StartValue { get; set; } = 1L;
public virtual long? StartValue { get; set; }
}
12 changes: 10 additions & 2 deletions src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,16 @@ protected override void Generate(RenameSequenceOperation operation, IModel? mode
builder
.Append("ALTER SEQUENCE ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema))
.Append(" RESTART WITH ")
.Append(IntegerConstant(operation.StartValue))
.Append(" RESTART");

if (operation.StartValue.HasValue)
{
builder
.Append(" WITH ")
.Append(IntegerConstant(operation.StartValue.Value));
}

builder
.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);

EndStatement(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ public void UpdateDataOperation_throws_for_types_count_mismatch()
Values = new object[,] { { "Targaryen" } }
})).Message);


[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
Expand Down Expand Up @@ -723,6 +724,20 @@ public virtual void DefaultValue_with_line_breaks_2(bool isUnicode)
});
}

[ConditionalTheory]
[InlineData(3L)]
[InlineData(null)]
public virtual void Sequence_restart_operation(long? startsAt)
{
Generate(
new RestartSequenceOperation
{
Name = "TestRestartSequenceOperation",
Schema = "dbo",
StartValue = startsAt
});
}

private static void CreateGotModel(ModelBuilder b)
=> b.HasDefaultSchema("dbo").Entity(
"Person", pb =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,18 @@ public virtual Task Alter_sequence_increment_by()
Assert.Equal(2, sequence.IncrementBy);
});

[ConditionalFact]
public virtual Task Alter_sequence_restart_with()
=> Test(
builder => builder.HasSequence<int>("foo"),
builder => { },
builder => builder.HasSequence<int>("foo").StartsAt(3),
model =>
{
var sequence = Assert.Single(model.Sequences);
Assert.Equal(3, sequence.StartValue);
});

[ConditionalFact]
public virtual Task Drop_sequence()
=> Test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,14 @@ public override async Task Alter_sequence_increment_by()
""");
}

public override async Task Alter_sequence_restart_with()
{
await base.Alter_sequence_restart_with();

AssertSql(
@"ALTER SEQUENCE [foo] RESTART WITH 3;");
}

public override async Task Drop_sequence()
{
await base.Drop_sequence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,17 @@ public override void DefaultValue_with_line_breaks_2(bool isUnicode)
AssertSql(expectedSql);
}

public override void Sequence_restart_operation(long? startsAt)
{
base.Sequence_restart_operation(startsAt);

var expectedSql = startsAt.HasValue ?
@$"ALTER SEQUENCE [dbo].[TestRestartSequenceOperation] RESTART WITH {startsAt};" :
@$"ALTER SEQUENCE [dbo].[TestRestartSequenceOperation] RESTART;";
AssertSql(expectedSql);
}


[ConditionalFact]
public virtual void CreateIndex_generates_exec_when_legacy_filter_and_idempotent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,9 @@ public override Task Alter_sequence_all_settings()
public override Task Alter_sequence_increment_by()
=> AssertNotSupportedAsync(base.Alter_sequence_increment_by, SqliteStrings.SequencesNotSupported);

public override Task Alter_sequence_restart_with()
=> AssertNotSupportedAsync(base.Alter_sequence_restart_with, SqliteStrings.SequencesNotSupported);

public override Task Drop_sequence()
=> AssertNotSupportedAsync(base.Drop_sequence, SqliteStrings.SequencesNotSupported);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,12 @@ public override void UpdateDataOperation_required_args_multiple_rows()
""");
}

public override void Sequence_restart_operation(long? startsAt)
{
var ex = Assert.Throws<NotSupportedException>(() => base.Sequence_restart_operation(startsAt));
Assert.Equal(SqliteStrings.SequencesNotSupported, ex.Message);
}

[ConditionalFact]
public virtual void AddPrimaryKey_throws_when_no_model()
{
Expand Down