Skip to content

Commit de5bf7c

Browse files
committed
Simplify IsDescending values
If IsDescending is specified, it must explicitly specify all values for all properties.
1 parent f9cfaaf commit de5bf7c

File tree

28 files changed

+69
-134
lines changed

28 files changed

+69
-134
lines changed

src/EFCore.Abstractions/IndexAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public bool IsUnique
5555
}
5656

5757
/// <summary>
58-
/// Gets a set of values indicating whether each corresponding index column has descending sort order.
59-
/// If less sort order values are provided than there are columns, the remaining columns will have ascending order.
58+
/// A set of values indicating whether each corresponding index column has descending sort order.
6059
/// </summary>
6160
public bool[]? IsDescending { get; set; }
6261

src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ protected virtual void Generate(CreateIndexOperation operation, IndentedStringBu
911911
.Append("unique: true");
912912
}
913913

914-
if (operation.IsDescending.Length > 0)
914+
if (operation.IsDescending is not null)
915915
{
916916
builder
917917
.AppendLine(",")

src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ protected virtual void GenerateIndex(
634634
.Append(".IsUnique()");
635635
}
636636

637-
if (index.IsDescending.Count > 0)
637+
if (index.IsDescending is not null)
638638
{
639639
stringBuilder
640640
.AppendLine()

src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -567,46 +567,9 @@ private void GenerateIndex(IIndex index)
567567
lines.Add($".{nameof(IndexBuilder.IsUnique)}()");
568568
}
569569

570-
// Ascending/descending:
571-
// If all ascending (IsDescending.Count == 0), no need to scaffold IsDescending() call (default)
572-
// If all descending (IsDescending is all true), scaffold parameterless IsDescending()
573-
// Otherwise, scaffold IsDescending() with values up until the last true (unspecified values default to false)
574-
if (index.IsDescending.Count > 0)
570+
if (index.IsDescending is not null)
575571
{
576-
var descendingBuilder = new StringBuilder()
577-
.Append('.')
578-
.Append(nameof(IndexBuilder.IsDescending))
579-
.Append('(');
580-
581-
var isAnyAscending = false;
582-
var lastDescending = -1;
583-
for (var i = 0; i < index.IsDescending.Count; i++)
584-
{
585-
if (index.IsDescending[i])
586-
{
587-
lastDescending = i;
588-
}
589-
else
590-
{
591-
isAnyAscending = true;
592-
}
593-
}
594-
595-
if (isAnyAscending)
596-
{
597-
for (var i = 0; i <= lastDescending; i++)
598-
{
599-
if (i > 0)
600-
{
601-
descendingBuilder.Append(", ");
602-
}
603-
604-
descendingBuilder.Append(_code.Literal(index.IsDescending[i]));
605-
}
606-
}
607-
608-
descendingBuilder.Append(')');
609-
lines.Add(descendingBuilder.ToString());
572+
lines.Add($".{nameof(IndexBuilder.IsDescending)}({string.Join(", ", index.IsDescending.Select(d => _code.Literal(d)))})");
610573
}
611574

612575
GenerateAnnotations(index, annotations, lines);

src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private void GenerateIndexAttributes(IEntityType entityType)
212212
indexAttribute.AddParameter($"{nameof(IndexAttribute.IsUnique)} = {_code.Literal(index.IsUnique)}");
213213
}
214214

215-
if (index.IsDescending.Count > 0)
215+
if (index.IsDescending is not null)
216216
{
217217
indexAttribute.AddParameter($"{nameof(IndexAttribute.IsDescending)} = {_code.UnknownLiteral(index.IsDescending)}");
218218
}

src/EFCore.Relational/Metadata/ITableIndex.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ public interface ITableIndex : IAnnotatable
4141

4242
/// <summary>
4343
/// A set of values indicating whether each corresponding index column has descending sort order.
44-
/// If less sort order values are provided than there are columns, the remaining columns will have ascending order.
4544
/// </summary>
46-
IReadOnlyList<bool> IsDescending { get; }
45+
IReadOnlyList<bool>? IsDescending { get; }
4746

4847
/// <summary>
4948
/// Gets the expression used as the index filter.
@@ -80,7 +79,9 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt
8079
.AppendJoin(
8180
", ",
8281
Enumerable.Range(0, Columns.Count)
83-
.Select(i => $"'{Columns[i].Name}'{(i < IsDescending.Count && IsDescending[i] ? " Desc" : "")}"))
82+
.Select(
83+
i =>
84+
$"'{Columns[i].Name}'{(IsDescending is not null && i < IsDescending.Count && IsDescending[i] ? " Desc" : "")}"))
8485
.Append('}');
8586

8687
if (IsUnique)

src/EFCore.Relational/Metadata/Internal/TableIndex.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public TableIndex(
2222
Table table,
2323
IReadOnlyList<Column> columns,
2424
bool unique,
25-
IReadOnlyList<bool> isDescending)
25+
IReadOnlyList<bool>? isDescending)
2626
{
2727
Name = name;
2828
Table = table;
@@ -71,7 +71,7 @@ public override bool IsReadOnly
7171
public virtual bool IsUnique { get; }
7272

7373
/// <inheritdoc />
74-
public virtual IReadOnlyList<bool> IsDescending { get; }
74+
public virtual IReadOnlyList<bool>? IsDescending { get; }
7575

7676
/// <inheritdoc />
7777
public virtual string? Filter

src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,11 @@ protected virtual IEnumerable<MigrationOperation> Diff(
13931393

13941394
private bool IndexStructureEquals(ITableIndex source, ITableIndex target, DiffContext diffContext)
13951395
=> source.IsUnique == target.IsUnique
1396-
&& source.IsDescending.SequenceEqual(target.IsDescending)
1396+
&& (
1397+
source.IsDescending is null == target.IsDescending is null
1398+
|| source.IsDescending is not null
1399+
&& target.IsDescending is not null
1400+
&& source.IsDescending.SequenceEqual(target.IsDescending))
13971401
&& source.Filter == target.Filter
13981402
&& !HasDifferences(source.GetAnnotations(), target.GetAnnotations())
13991403
&& source.Columns.Select(p => p.Name).SequenceEqual(

src/EFCore.Relational/Migrations/MigrationBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,6 @@ public virtual AlterOperationBuilder<AlterTableOperation> AlterTable(
603603
/// <param name="filter">The filter to apply to the index, or <see langword="null" /> for no filter.</param>
604604
/// <param name="descending">
605605
/// A set of values indicating whether each corresponding index column has descending sort order.
606-
/// If less sort order values are provided than there are columns, the remaining columns will have ascending order.
607606
/// If <see langword="null" />, all columns will have ascending order.
608607
/// </param>
609608
/// <returns>A builder to allow annotations to be added to the operation.</returns>
@@ -638,7 +637,6 @@ public virtual OperationBuilder<CreateIndexOperation> CreateIndex(
638637
/// <param name="filter">The filter to apply to the index, or <see langword="null" /> for no filter.</param>
639638
/// <param name="descending">
640639
/// A set of values indicating whether each corresponding index column has descending sort order.
641-
/// If less sort order values are provided than there are columns, the remaining columns will have ascending order.
642640
/// If <see langword="null" />, all columns will have ascending order.
643641
/// </param>
644642
/// <returns>A builder to allow annotations to be added to the operation.</returns>

src/EFCore.Relational/Migrations/Operations/CreateIndexOperation.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public class CreateIndexOperation : MigrationOperation, ITableMigrationOperation
3939

4040
/// <summary>
4141
/// A set of values indicating whether each corresponding index column has descending sort order.
42-
/// If less sort order values are provided than there are columns, the remaining columns will have ascending order.
4342
/// </summary>
44-
public virtual bool[] IsDescending { get; set; } = Array.Empty<bool>();
43+
public virtual bool[]? IsDescending { get; set; }
4544

4645
/// <summary>
4746
/// An expression to use as the index filter.
@@ -64,7 +63,7 @@ public static CreateIndexOperation CreateFrom(ITableIndex index)
6463
Table = index.Table.Name,
6564
Columns = index.Columns.Select(p => p.Name).ToArray(),
6665
IsUnique = index.IsUnique,
67-
IsDescending = index.IsDescending.ToArray(),
66+
IsDescending = index.IsDescending?.ToArray(),
6867
Filter = index.Filter
6968
};
7069
operation.AddAnnotations(index.GetAnnotations());

0 commit comments

Comments
 (0)