Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions SQLSchemaCompare.Core/Entities/Database/ABaseDbIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class ABaseDbIndex : ABaseDbConstraint
/// <remarks>Used only by the DatabaseProvider to group the indexes and fill the ColumnDescending list</remarks>
public bool IsDescending { get; set; }

/// <summary>
/// Gets or sets the length of the prefix.
/// </summary>
public long? PrefixLength { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the index is included.
/// </summary>
Expand All @@ -24,6 +29,11 @@ public class ABaseDbIndex : ABaseDbConstraint
/// </summary>
public List<bool> ColumnDescending { get; } = [];

/// <summary>
/// Gets the column prefix lengths.
/// </summary>
public List<long?> ColumnPrefixLengths { get; } = [];

/// <summary>
/// Gets the included columns.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ protected TDatabase DiscoverDatabase(TDatabaseContext context, TaskInfo taskInfo
index.Database = db;
index.ColumnNames.AddRange(indexGroup.Where(x => !x.IsIncluded).OrderBy(x => x.OrdinalPosition).Select(x => x.ColumnName));
index.ColumnDescending.AddRange(indexGroup.Where(x => !x.IsIncluded).OrderBy(x => x.OrdinalPosition).Select(x => x.IsDescending));
index.ColumnPrefixLengths.AddRange(indexGroup.Where(x => !x.IsIncluded).OrderBy(x => x.OrdinalPosition).Select(x => x.PrefixLength));
index.IncludedColumns.AddRange(indexGroup.Where(x => x.IsIncluded).OrderBy(x => x.OrdinalPosition).Select(x => x.ColumnName));
db.Indexes.Add(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ protected override IEnumerable<ABaseDbIndex> GetIndexes(MySqlDatabaseContext con
query.AppendLine(" CAST(s.SEQ_IN_INDEX AS SIGNED) AS 'OrdinalPosition',");
query.AppendLine(" CASE WHEN s.COLLATION = 'D' THEN TRUE ELSE FALSE END as 'IsDescending',");
query.AppendLine(" s.INDEX_TYPE AS 'IndexType',");
query.AppendLine(" COALESCE(tc.CONSTRAINT_TYPE, 'INDEX') AS 'ConstraintType'");
query.AppendLine(" COALESCE(tc.CONSTRAINT_TYPE, 'INDEX') AS 'ConstraintType',");
query.AppendLine(" s.SUB_PART AS 'PrefixLength'");
query.AppendLine("FROM INFORMATION_SCHEMA.STATISTICS s");
query.AppendLine("LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc");
query.AppendLine(" ON tc.table_name = s.table_name AND tc.table_schema = s.table_schema AND tc.constraint_name = s.index_name");
Expand Down
11 changes: 9 additions & 2 deletions SQLSchemaCompare.Infrastructure/SqlScripters/MySqlScripter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,19 @@ protected override string ScriptCreateIndex(ABaseDbIndex index)
var scriptOrder = index.ColumnDescending.Any(x => x);
var columnList = index.ColumnNames.Select((x, i) =>
{
var column = $"{this.ScriptHelper.ScriptObjectName(x)}";

if (indexMySql.IndexType == "BTREE" && index.ColumnPrefixLengths[i] != null)
{
column += $"({index.ColumnPrefixLengths[i]})";
}

if (scriptOrder)
{
return $"{this.ScriptHelper.ScriptObjectName(x)} {(index.ColumnDescending[i] ? "DESC" : "ASC")}";
column += $" {(index.ColumnDescending[i] ? "DESC" : "ASC")}";
}

return $"{this.ScriptHelper.ScriptObjectName(x)}";
return column;
});

sb.Append("CREATE ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CREATE TABLE actor (
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted BIT(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (actor_id),
KEY idx_actor_name (first_name(10), last_name(15)),
KEY idx_actor_last_name (last_name)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Expand Down
2 changes: 2 additions & 0 deletions SQLSchemaCompare.Test/Datasources/sakila-schema-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ CREATE TABLE actor (
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted BIT(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (actor_id),
KEY idx_actor_name (first_name(10), last_name(15)),
KEY idx_actor_last_name (last_name)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Expand Down
Loading