Skip to content

Commit

Permalink
The INFORMATION_SCHEMA.COLLATION_CHARACTER_SET_APPLICABILITY tabl…
Browse files Browse the repository at this point in the history
…e layout has changed in MariaDB 10.10.1 (breaking change).
  • Loading branch information
lauxjpn committed Dec 7, 2024
1 parent f669673 commit c1b607d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/EFCore.MySql/Infrastructure/MariaDbServerVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal MariaDbServerVersionSupport([NotNull] ServerVersion serverVersion)
public override bool ValuesWithRows => false;
public override bool WhereSubqueryReferencesOuterQuery => false;
public override bool FieldReferenceInTableValueConstructor => false;
public override bool CollationCharacterSetApplicabilityWithFullCollationNameColumn => ServerVersion.Version >= new Version(10, 10, 1);

public override bool JsonTableImplementationStable => false;
public override bool JsonTableImplementationWithoutMariaDbBugs => false;
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.MySql/Infrastructure/MySqlServerVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ internal MySqlServerVersionSupport([NotNull] ServerVersion serverVersion)
public override bool ValuesWithRows => ServerVersion.Version >= new Version(8, 0, 19);
public override bool WhereSubqueryReferencesOuterQuery => false;
public override bool FieldReferenceInTableValueConstructor => true;
public override bool CollationCharacterSetApplicabilityWithFullCollationNameColumn => false;

public override bool JsonTableImplementationStable => false;
public override bool JsonTableImplementationWithoutMySqlBugs => false; // Other non-fatal bugs regarding JSON_TABLE.
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.MySql/Infrastructure/ServerVersionSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public virtual bool PropertyOrVersion(string propertyNameOrServerVersion)
public virtual bool ValuesWithRows => false;
public virtual bool WhereSubqueryReferencesOuterQuery => false;
public virtual bool FieldReferenceInTableValueConstructor => false;
public virtual bool CollationCharacterSetApplicabilityWithFullCollationNameColumn => false;

public virtual bool JsonTableImplementationStable => JsonTable;
public virtual bool JsonTableImplementationWithoutMySqlBugs => JsonTable;
Expand Down
18 changes: 12 additions & 6 deletions src/EFCore.MySql/Migrations/MySqlMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,23 @@ protected override void Generate(AlterTableOperation operation, IModel model, Mi
}
else
{
var collationColumnName = _options.ServerVersion.Supports.CollationCharacterSetApplicabilityWithFullCollationNameColumn
? "FULL_COLLATION_NAME"
: "COLLATION_NAME";

// The charset (and any collation) has been reset to the default.
var resetCharSetSql = $@"set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`COLLATION_NAME` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = {_stringTypeMapping.GenerateSqlLiteral(operation.Name)} AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
var resetCharSetSql = $"""
set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`{collationColumnName}` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = {_stringTypeMapping.GenerateSqlLiteral(operation.Name)} AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
SET @__pomelo_SqlExpr = CONCAT('ALTER TABLE {Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)} CHARACTER SET = ', @__pomelo_TableCharset, ';');
PREPARE __pomelo_SqlExprExecute FROM @__pomelo_SqlExpr;
EXECUTE __pomelo_SqlExprExecute;
DEALLOCATE PREPARE __pomelo_SqlExprExecute;";
DEALLOCATE PREPARE __pomelo_SqlExprExecute;
""";

builder.AppendLine(resetCharSetSql);
EndStatement(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static Func<string, string> GenerateSchemaFilter(IReadOnlyList<string> s
FROM
`INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN
`INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`COLLATION_NAME` = `t`.`TABLE_COLLATION`
`INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`{0}` = `t`.`TABLE_COLLATION`
WHERE
`TABLE_SCHEMA` = SCHEMA()
AND
Expand All @@ -222,8 +222,12 @@ protected virtual IEnumerable<DatabaseTable> GetTables(
{
using (var command = connection.CreateCommand())
{
var collationColumnName = _options.ServerVersion.Supports.CollationCharacterSetApplicabilityWithFullCollationNameColumn
? "FULL_COLLATION_NAME"
: "COLLATION_NAME";

var tables = new List<DatabaseTable>();
command.CommandText = GetTablesQuery;
command.CommandText = string.Format(GetTablesQuery, collationColumnName);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
Expand Down
26 changes: 16 additions & 10 deletions test/EFCore.MySql.FunctionalTests/MigrationsMySqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,21 +1094,27 @@ await Test(
}),
result => { });

var collationColumnName = AppConfig.ServerVersion.Supports.CollationCharacterSetApplicabilityWithFullCollationNameColumn
? "FULL_COLLATION_NAME"
: "COLLATION_NAME";

AssertSql(
@"set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`COLLATION_NAME` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = 'IceCream' AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
$"""
set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`{collationColumnName}` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = 'IceCream' AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
SET @__pomelo_SqlExpr = CONCAT('ALTER TABLE `IceCream` CHARACTER SET = ', @__pomelo_TableCharset, ';');
PREPARE __pomelo_SqlExprExecute FROM @__pomelo_SqlExpr;
EXECUTE __pomelo_SqlExprExecute;
DEALLOCATE PREPARE __pomelo_SqlExprExecute;",
//
$@"ALTER TABLE `IceCream` MODIFY COLUMN `Name` longtext COLLATE {NonDefaultCollation} NULL;",
//
$@"ALTER TABLE `IceCream` MODIFY COLUMN `Brand` longtext COLLATE {NonDefaultCollation2} NULL;");
DEALLOCATE PREPARE __pomelo_SqlExprExecute;
""",
//
$@"ALTER TABLE `IceCream` MODIFY COLUMN `Name` longtext COLLATE {NonDefaultCollation} NULL;",
//
$@"ALTER TABLE `IceCream` MODIFY COLUMN `Brand` longtext COLLATE {NonDefaultCollation2} NULL;");
}

[ConditionalFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1380,17 +1380,23 @@ public virtual void AlterTableOperation_with_collation_reset()
.OldAnnotation(RelationalAnnotationNames.Collation, "latin1_general_ci");
});

var collationColumnName = AppConfig.ServerVersion.Supports.CollationCharacterSetApplicabilityWithFullCollationNameColumn
? "FULL_COLLATION_NAME"
: "COLLATION_NAME";

Assert.Equal(
@"set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`COLLATION_NAME` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = 'IceCreams' AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
$"""
set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`{collationColumnName}` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = 'IceCreams' AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
SET @__pomelo_SqlExpr = CONCAT('ALTER TABLE `IceCreams` CHARACTER SET = ', @__pomelo_TableCharset, ';');
PREPARE __pomelo_SqlExprExecute FROM @__pomelo_SqlExpr;
EXECUTE __pomelo_SqlExprExecute;
DEALLOCATE PREPARE __pomelo_SqlExprExecute;" + EOL,
DEALLOCATE PREPARE __pomelo_SqlExprExecute;
""" + EOL,
Sql,
ignoreLineEndingDifferences: true);
}
Expand Down Expand Up @@ -1499,17 +1505,23 @@ public virtual void AlterTableOperation_with_charset_reset()
.OldAnnotation(MySqlAnnotationNames.CharSet, "latin1");
});

var collationColumnName = AppConfig.ServerVersion.Supports.CollationCharacterSetApplicabilityWithFullCollationNameColumn
? "FULL_COLLATION_NAME"
: "COLLATION_NAME";

Assert.Equal(
@"set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`COLLATION_NAME` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = 'IceCreams' AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
$"""
set @__pomelo_TableCharset = (
SELECT `ccsa`.`CHARACTER_SET_NAME` as `TABLE_CHARACTER_SET`
FROM `INFORMATION_SCHEMA`.`TABLES` as `t`
LEFT JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` as `ccsa` ON `ccsa`.`{collationColumnName}` = `t`.`TABLE_COLLATION`
WHERE `TABLE_SCHEMA` = SCHEMA() AND `TABLE_NAME` = 'IceCreams' AND `TABLE_TYPE` IN ('BASE TABLE', 'VIEW'));
SET @__pomelo_SqlExpr = CONCAT('ALTER TABLE `IceCreams` CHARACTER SET = ', @__pomelo_TableCharset, ';');
PREPARE __pomelo_SqlExprExecute FROM @__pomelo_SqlExpr;
EXECUTE __pomelo_SqlExprExecute;
DEALLOCATE PREPARE __pomelo_SqlExprExecute;" + EOL,
DEALLOCATE PREPARE __pomelo_SqlExprExecute;
""" + EOL,
Sql,
ignoreLineEndingDifferences: true);
}
Expand Down

0 comments on commit c1b607d

Please sign in to comment.