fix(mysql): generate index COMMENT clause and enable index_comment capability

This commit is contained in:
t8y2 committed 2026-06-12 16:56:20 +08:00
1 parent edf9dd88e3
commit 250da6e7cb
5 files changed
+105 -13

No files matched your search

@@ -78,6 +78,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
drop_index: true,
rebuild_index: true,
index_type: true,
index_comment: true,
alter_primary_key: true,
..base
},
@@ -169,6 +169,12 @@ pub(super) fn build_create_index_statements(
} else {
String::new()
};
let comment = clean(&index.comment);
let comment_clause = if !comment.is_empty() && capabilities.index_comment && dialect == StructureDialect::Mysql {
format!(" COMMENT {}", quote_string(&comment))
} else {
String::new()
};
let filter = clean(&index.filter);
let supports_where = capabilities.index_filter
&& matches!(dialect, StructureDialect::Postgres | StructureDialect::SqlServer | StructureDialect::Sqlite);
@@ -180,17 +186,18 @@ pub(super) fn build_create_index_statements(
)
} else {
format!(
"CREATE {unique}{type_prefix}INDEX {}{using_clause} ON {table} ({cols}){include_clause}{where_clause};",
"CREATE {unique}{type_prefix}INDEX {}{using_clause} ON {table} ({cols}){include_clause}{where_clause}{comment_clause};",
quote_ident(dialect, &name)
)
};
let mut statements = vec![create_sql];
let comment = clean(&index.comment);
if !comment.is_empty() && capabilities.index_comment && dialect == StructureDialect::Postgres {
statements.push(format!("COMMENT ON INDEX {} IS {};", quote_ident(dialect, &name), quote_string(&comment)));
} else if !comment.is_empty() && capabilities.index_comment && dialect == StructureDialect::SqlServer {
statements.extend(build_sqlserver_index_comment_sql(table, schema, table_name, &name, &comment));
} else if !comment.is_empty() && capabilities.index_comment && dialect == StructureDialect::Mysql {
// MySQL comment is embedded inline in the CREATE INDEX statement above
} else if !comment.is_empty() && capabilities.index_comment {
warnings.push(format!("Index comments are not supported for {} from this editor.", dialect_label(dialect)));
}
@@ -117,6 +117,61 @@ fn builds_mysql_column_and_index_changes() {
);
}
#[test]
fn mysql_create_index_with_comment() {
let mut col = column("name");
col.data_type = "varchar(120)".to_string();
let mut idx = index("idx_users_name", &["name"]);
idx.comment = "Search index".to_string();
let result = build_table_structure_change_sql(TableStructureSqlOptions {
database_type: Some(DatabaseType::Mysql),
schema: None,
table_name: "users".to_string(),
columns: vec![col],
indexes: vec![idx],
foreign_keys: Vec::new(),
triggers: Vec::new(),
table_comment: None,
original_table_comment: None,
});
assert_eq!(result.warnings, Vec::<String>::new());
assert_eq!(
result.statements,
vec![
"ALTER TABLE `users` ADD COLUMN `name` varchar(120);",
"CREATE INDEX `idx_users_name` ON `users` (`name`) COMMENT 'Search index';",
]
);
}
#[test]
fn mysql_create_unique_index_with_comment_and_btree() {
let mut idx = index("uniq_users_email", &["email"]);
idx.is_unique = true;
idx.index_type = "BTREE".to_string();
idx.comment = "Unique email index".to_string();
let result = build_table_structure_change_sql(TableStructureSqlOptions {
database_type: Some(DatabaseType::Mysql),
schema: None,
table_name: "users".to_string(),
columns: Vec::new(),
indexes: vec![idx],
foreign_keys: Vec::new(),
triggers: Vec::new(),
table_comment: None,
original_table_comment: None,
});
assert_eq!(result.warnings, Vec::<String>::new());
assert_eq!(
result.statements,
vec!["CREATE UNIQUE INDEX `uniq_users_email` USING BTREE ON `users` (`email`) COMMENT 'Unique email index';",]
);
}
#[test]
fn mysql_add_timestamp_column_drops_invalid_precision() {
let mut created_at = column("created_at");