Skip to content

Commit

Permalink
Merge pull request #1399 from lillo42/fixes-algorithm-error
Browse files Browse the repository at this point in the history
Fixes error to create a index with index method
  • Loading branch information
jzabroski committed Jan 8, 2021
2 parents 0bc21e6 + cc74b41 commit 1b8c65c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ protected virtual string GetIncludeString(CreateIndexExpression column)
throw new NotSupportedException("The current version doesn't support include index. Please use Postgres 11.");
}

protected virtual string GetUsingAlgorithm(CreateIndexExpression expression)
protected virtual Algorithm GetIndexMethod(CreateIndexExpression expression)
{
var algorithm = expression.GetAdditionalFeature<PostgresIndexAlgorithmDefinition>(PostgresExtensions.IndexAlgorithm);
if (algorithm == null)
{
return string.Empty;
return Algorithm.BTree;
}

return $" USING {algorithm.Algorithm.ToString().ToUpper()}";
return algorithm.Algorithm;
}

protected virtual string GetFilter(CreateIndexExpression expression)
Expand Down Expand Up @@ -266,12 +266,15 @@ public override string Generate(CreateIndexExpression expression)
result.Append(" UNIQUE");
}

var indexMethod = GetIndexMethod(expression);

result.AppendFormat(" INDEX{0} {1} ON{2} {3}{4} (",
GetAsConcurrently(expression),
Quoter.QuoteIndexName(expression.Index.Name),
GetAsOnly(expression),
Quoter.QuoteTableName(expression.Index.TableName, expression.Index.SchemaName),
GetUsingAlgorithm(expression));
// B-Tree is default index method
indexMethod == Algorithm.BTree ? string.Empty : $" USING {indexMethod.ToString().ToUpper()}");

var first = true;
foreach (var column in expression.Index.Columns)
Expand All @@ -285,9 +288,21 @@ public override string Generate(CreateIndexExpression expression)
result.Append(",");
}

result.Append(Quoter.QuoteColumnName(column.Name))
.Append(column.Direction == Direction.Ascending ? " ASC" : " DESC")
.Append(GetNullsSort(column));
result.Append(Quoter.QuoteColumnName(column.Name));

switch (indexMethod)
{
// Doesn't support ASC/DESC neither nulls sorts
case Algorithm.Spgist:
case Algorithm.Gist:
case Algorithm.Gin:
case Algorithm.Brin:
case Algorithm.Hash:
continue;
}

result.Append(column.Direction == Direction.Ascending ? " ASC" : " DESC")
.Append(GetNullsSort(column));
}

result.Append(")")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public override void CanDropIndexWithDefaultSchema()
result.ShouldBe("DROP INDEX \"public\".\"TestIndex\";");
}

// This index method doesn't support ASC/DES neither NULLS sort
[TestCase(Algorithm.Brin)]
[TestCase(Algorithm.BTree)]
[TestCase(Algorithm.Gin)]
[TestCase(Algorithm.Gist)]
[TestCase(Algorithm.Hash)]
Expand All @@ -142,7 +142,7 @@ public void CanCreateIndexUsingIndexAlgorithm(Algorithm algorithm)


var result = Generator.Generate(expression);
result.ShouldBe($"CREATE INDEX \"TestIndex\" ON \"public\".\"TestTable1\" USING {algorithm.ToString().ToUpper()} (\"TestColumn1\" ASC);");
result.ShouldBe($"CREATE INDEX \"TestIndex\" ON \"public\".\"TestTable1\" USING {algorithm.ToString().ToUpper()} (\"TestColumn1\");");
}

[Test]
Expand Down

0 comments on commit 1b8c65c

Please sign in to comment.