Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Add support for custom index names
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Aug 17, 2018
1 parent 0415860 commit 9beeba7
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public override List<string> ToCreateIndexStatements(Type tableType)
{
if (!fieldDef.IsIndexed) continue;

var indexName = GetIndexName(
var indexName = fieldDef.IndexName ?? GetIndexName(
fieldDef.IsUniqueIndex, modelDef.ModelName, fieldDef.FieldName);

sqlIndexes.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public override List<string> ToCreateIndexStatements(Type tableType)
{
if (!fieldDef.IsIndexed) continue;

var indexName = GetIndexName(
var indexName = fieldDef.IndexName ?? GetIndexName(
fieldDef.IsUniqueIndex,
(modelDef.IsInSchema
? modelDef.Schema + "_" + modelDef.ModelName
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceStack.OrmLite/FieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class FieldDefinition
public bool IsClustered { get; set; }

public bool IsNonClustered { get; set; }

public string IndexName { get; set; }

public bool IsRowVersion { get; set; }

Expand Down
5 changes: 3 additions & 2 deletions src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
AutoId = isAutoId,
IsIndexed = !isPrimaryKey && isIndex,
IsUniqueIndex = isUnique,
IsClustered = indexAttr != null && indexAttr.Clustered,
IsNonClustered = indexAttr != null && indexAttr.NonClustered,
IsClustered = indexAttr?.Clustered == true,
IsNonClustered = indexAttr?.NonClustered == true,
IndexName = indexAttr?.Name,
IsRowVersion = isRowVersion,
IgnoreOnInsert = propertyInfo.HasAttribute<IgnoreOnInsertAttribute>(),
IgnoreOnUpdate = propertyInfo.HasAttribute<IgnoreOnUpdateAttribute>(),
Expand Down
3 changes: 2 additions & 1 deletion src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,8 @@ public virtual List<string> ToCreateIndexStatements(Type tableType)
{
if (!fieldDef.IsIndexed) continue;

var indexName = GetIndexName(fieldDef.IsUniqueIndex, modelDef.ModelName.SafeVarName(), fieldDef.FieldName);
var indexName = fieldDef.IndexName
?? GetIndexName(fieldDef.IsUniqueIndex, modelDef.ModelName.SafeVarName(), fieldDef.FieldName);

sqlIndexes.Add(
ToCreateIndexStatement(fieldDef.IsUniqueIndex, indexName, modelDef, fieldDef.FieldName, isCombined: false, fieldDef: fieldDef));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void Can_create_ModelWithIndexFields_table()

Assert.That(sql, Does.Contain(indexName));
Assert.That(sql, Does.Contain(uniqueName));
Assert.That(sql, Does.Contain("altname"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public class ModelWithIndexFields

[Index(true)]
public string UniqueName { get; set; }

[Index(Name = "altname")]
public string Custom { get; set; }
}
}

0 comments on commit 9beeba7

Please sign in to comment.