fix(mysql): normalize unsigned column types
This commit is contained in:
1 parent
4ef94eb3a9
commit
9781b0f562
4 files changed
+106
-3
No files matched your search
@@ -132,6 +132,12 @@ pub(super) fn normalize_column_data_type(dialect: StructureDialect, data_type: &
|
||||
return trimmed.to_string();
|
||||
}
|
||||
|
||||
if dialect == StructureDialect::Mysql {
|
||||
if let Some(normalized) = normalize_mysql_numeric_attribute_type(base_type, params) {
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
if is_temporal_precision_type(dialect, base_type) {
|
||||
return if is_valid_temporal_precision(params, dialect) {
|
||||
format!("{base_type}({params})")
|
||||
@@ -143,6 +149,41 @@ pub(super) fn normalize_column_data_type(dialect: StructureDialect, data_type: &
|
||||
trimmed.to_string()
|
||||
}
|
||||
|
||||
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();
|
||||
if !matches!(
|
||||
type_name.as_str(),
|
||||
"tinyint"
|
||||
| "smallint"
|
||||
| "mediumint"
|
||||
| "int"
|
||||
| "integer"
|
||||
| "bigint"
|
||||
| "real"
|
||||
| "double"
|
||||
| "float"
|
||||
| "decimal"
|
||||
| "numeric"
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
let split_index = parts.iter().position(|part| {
|
||||
let normalized = part.to_ascii_lowercase();
|
||||
matches!(normalized.as_str(), "signed" | "unsigned" | "zerofill")
|
||||
})?;
|
||||
if !parts[split_index..].iter().all(|part| {
|
||||
let normalized = part.to_ascii_lowercase();
|
||||
matches!(normalized.as_str(), "signed" | "unsigned" | "zerofill")
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let attrs = parts.split_off(split_index).join(" ");
|
||||
let base = parts.join(" ");
|
||||
Some(format!("{base}({params}) {attrs}"))
|
||||
}
|
||||
|
||||
pub(super) fn is_temporal_precision_type(dialect: StructureDialect, base_type: &str) -> bool {
|
||||
let normalized = base_type.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_lowercase();
|
||||
match dialect {
|
||||
|
||||
@@ -117,6 +117,27 @@ fn builds_mysql_column_and_index_changes() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_mysql_unsigned_integer_column_with_length_before_attribute() {
|
||||
let mut score = column("score");
|
||||
score.data_type = "int unsigned(11)".to_string();
|
||||
|
||||
let result = build_table_structure_change_sql(TableStructureSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
schema: None,
|
||||
table_name: "users".to_string(),
|
||||
columns: vec![score],
|
||||
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 `users` ADD COLUMN `score` int(11) unsigned;"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_highgo_foreign_key_changes_with_postgres_syntax() {
|
||||
let mut old_fk = foreign_key("orders_user_id_fkey", "user_id", "users", "id");
|
||||
|
||||
Reference in new issue
Block a user