fix(sqlserver): omit display width for integer types

This commit is contained in:
t8y2 committed 2026-07-08 11:05:45 +08:00
1 parent 35710e7766
commit 15d4dc6e2e
4 files changed
+83

No files matched your search

@@ -143,6 +143,11 @@ pub(super) fn normalize_column_data_type(dialect: StructureDialect, data_type: &
return base_type.to_string();
}
if dialect == StructureDialect::SqlServer && is_sqlserver_lengthless_type(base_type) {
// SQL Server exact integer and legacy LOB types do not accept MySQL-style display widths.
return base_type.to_string();
}
if is_temporal_precision_type(dialect, base_type) {
return if is_valid_temporal_precision(params, dialect) {
format!("{base_type}({params})")
@@ -180,6 +185,32 @@ fn is_oracle_lengthless_type(base_type: &str) -> bool {
)
}
fn is_sqlserver_lengthless_type(base_type: &str) -> bool {
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
matches!(
normalized.as_str(),
"bigint"
| "bit"
| "date"
| "datetime"
| "image"
| "int"
| "integer"
| "money"
| "ntext"
| "real"
| "smalldatetime"
| "smallint"
| "smallmoney"
| "sql_variant"
| "text"
| "timestamp"
| "tinyint"
| "uniqueidentifier"
| "xml"
)
}
fn normalize_mysql_numeric_attribute_type(base_type: &str, params: &str) -> Option<String> {
let mut parts: Vec<&str> = base_type.split_whitespace().collect();
let type_name = parts.first().copied()?.to_ascii_lowercase();
@@ -1269,6 +1269,28 @@ fn builds_sql_server_quoted_column_and_index_statements() {
);
}
#[test]
fn sqlserver_strips_mysql_display_width_from_fixed_integer_types() {
let mut id = column("id");
id.data_type = "int(11)".to_string();
id.is_nullable = false;
let result = build_table_structure_change_sql(TableStructureSqlOptions {
database_type: Some(DatabaseType::SqlServer),
schema: Some("dbo".to_string()),
table_name: "users".to_string(),
columns: vec![id],
indexes: Vec::new(),
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 [dbo].[users] ADD [id] int NOT NULL;"]);
}
#[test]
fn sqlserver_default_changes_drop_old_constraints_with_isolated_batches() {
let mut sku = column("sku");