Skip to content

Commit

Permalink
Support DESC-flag in migrations and scaffolding (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
la-we authored Dec 8, 2023
1 parent 87ef29e commit 7b08f61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ protected override void Generate(CreateIndexOperation operation, IModel model, M
{
builder.Append("UNIQUE ");
}

if (operation.IsDescending is not null && operation.IsDescending.Length > 0)
{
var isDescending = operation.IsDescending[0];
if (operation.IsDescending.Any(x => x != isDescending))
throw new NotSupportedException("Mixed order indices are not supported by Firebird.");

if (isDescending)
{
builder.Append("DESC ");
}
}

IndexTraits(operation, model, builder);
builder.Append("INDEX ");
builder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ private void GetPrimaryKeys(DbConnection connection, IReadOnlyList<DatabaseTable
@"SELECT
trim(I.rdb$index_name) as INDEX_NAME,
COALESCE(I.rdb$unique_flag, 0) as IS_UNIQUE,
Coalesce(I.rdb$index_type, 0) = 1 as IS_DESC,
list(trim(sg.RDB$FIELD_NAME)) as COLUMNS
FROM
RDB$INDICES i
Expand All @@ -319,7 +320,7 @@ private void GetPrimaryKeys(DbConnection connection, IReadOnlyList<DatabaseTable
WHERE
trim(i.rdb$relation_name) = '{0}'
GROUP BY
INDEX_NAME, IS_UNIQUE ;";
INDEX_NAME, IS_UNIQUE, IS_DESC ;";

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

foreach (var column in reader.GetString(2).Split(','))
foreach (var column in reader.GetString(3).Split(','))
{
index.Columns.Add(table.Columns.Single(y => y.Name == column.Trim()));
}

if (reader.GetBoolean(2))
{
var isDescending = new bool[index.Columns.Count];
isDescending.AsSpan().Fill(true);
index.IsDescending = isDescending;
}

table.Indexes.Add(index);
}
}
Expand Down

0 comments on commit 7b08f61

Please sign in to comment.