Skip to content

Commit 7b08f61

Browse files
authored
Support DESC-flag in migrations and scaffolding (#126)
1 parent 87ef29e commit 7b08f61

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/FirebirdSql.EntityFrameworkCore.Firebird/Migrations/FbMigrationsSqlGenerator.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ protected override void Generate(CreateIndexOperation operation, IModel model, M
201201
{
202202
builder.Append("UNIQUE ");
203203
}
204+
205+
if (operation.IsDescending is not null && operation.IsDescending.Length > 0)
206+
{
207+
var isDescending = operation.IsDescending[0];
208+
if (operation.IsDescending.Any(x => x != isDescending))
209+
throw new NotSupportedException("Mixed order indices are not supported by Firebird.");
210+
211+
if (isDescending)
212+
{
213+
builder.Append("DESC ");
214+
}
215+
}
216+
204217
IndexTraits(operation, model, builder);
205218
builder.Append("INDEX ");
206219
builder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name));

src/FirebirdSql.EntityFrameworkCore.Firebird/Scaffolding/Internal/FbDatabaseModelFactory.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ private void GetPrimaryKeys(DbConnection connection, IReadOnlyList<DatabaseTable
311311
@"SELECT
312312
trim(I.rdb$index_name) as INDEX_NAME,
313313
COALESCE(I.rdb$unique_flag, 0) as IS_UNIQUE,
314+
Coalesce(I.rdb$index_type, 0) = 1 as IS_DESC,
314315
list(trim(sg.RDB$FIELD_NAME)) as COLUMNS
315316
FROM
316317
RDB$INDICES i
@@ -319,7 +320,7 @@ private void GetPrimaryKeys(DbConnection connection, IReadOnlyList<DatabaseTable
319320
WHERE
320321
trim(i.rdb$relation_name) = '{0}'
321322
GROUP BY
322-
INDEX_NAME, IS_UNIQUE ;";
323+
INDEX_NAME, IS_UNIQUE, IS_DESC ;";
323324

324325
/// <remarks>
325326
/// Primary keys are handled as in <see cref="GetConstraints"/>, not here
@@ -343,11 +344,18 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> ta
343344
IsUnique = reader.GetBoolean(1),
344345
};
345346

346-
foreach (var column in reader.GetString(2).Split(','))
347+
foreach (var column in reader.GetString(3).Split(','))
347348
{
348349
index.Columns.Add(table.Columns.Single(y => y.Name == column.Trim()));
349350
}
350351

352+
if (reader.GetBoolean(2))
353+
{
354+
var isDescending = new bool[index.Columns.Count];
355+
isDescending.AsSpan().Fill(true);
356+
index.IsDescending = isDescending;
357+
}
358+
351359
table.Indexes.Add(index);
352360
}
353361
}

0 commit comments

Comments
 (0)