fix(structure): trim table names before creation

This commit is contained in:
zipg authored and GitHub committed 2026-07-20 23:42:41 +08:00
1 parent 4634b0948d
commit e67cdf6134
3 files changed
+29 -3

No files matched your search

@@ -10,9 +10,10 @@ use super::types::{TableStructureSqlOptions, TableStructureSqlResult};
use super::util::{clean, format_default_for_sql, normalize_default, qualified_table, quote_ident, quote_string};
use super::validation::{validate_columns, validate_dameng_identity};
pub fn build_create_table_sql(options: TableStructureSqlOptions) -> TableStructureSqlResult {
pub fn build_create_table_sql(mut options: TableStructureSqlOptions) -> TableStructureSqlResult {
options.table_name = clean(&options.table_name);
let mut warnings = Vec::new();
if clean(&options.table_name).is_empty() {
if options.table_name.is_empty() {
warnings.push("Table name is required.".to_string());
}
let active_columns: Vec<_> = options.columns.iter().filter(|column| !column.marked_for_drop).collect();
@@ -1203,6 +1203,31 @@ fn builds_postgres_create_table_with_comments_and_index() {
);
}
#[test]
fn create_table_trims_table_name_whitespace_for_all_statements() {
let mut id = column("id");
id.data_type = "integer".to_string();
let idx = index("idx_users_id", &["id"]);
let result = build_create_table_sql(TableStructureSqlOptions {
database_type: Some(DatabaseType::Mysql),
schema: None,
table_name: " users ".to_string(),
columns: vec![id],
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 TABLE `users` (\n `id` integer\n);", "CREATE INDEX `idx_users_id` ON `users` (`id`);",]
);
}
#[test]
fn warns_for_sqlite_unsafe_column_changes() {
let mut col = column("name");