fix(mysql): handle export terminators and escaped quotes

This commit is contained in:
zipg authored and GitHub committed 2026-07-21 17:13:50 +08:00
1 parent 8a546be58d
commit a12f8d09b7
2 files changed
+60 -7

No files matched your search

+21 -5
View File
@@ -783,9 +783,8 @@ pub fn build_database_sql_export(options: BuildDatabaseSqlExportOptions) -> Resu
for table in options.tables {
if let Some(ddl) = table.ddl.as_ref().map(|ddl| ddl.trim()).filter(|ddl| !ddl.is_empty()) {
let ddl = normalize_export_table_ddl(ddl, table.database_type);
lines.push(format!("-- Structure for {}", table.display_name));
lines.push(format!("{};", ddl.trim_end_matches(';')));
lines.push(format_export_table_ddl(ddl, table.database_type));
lines.push(String::new());
}
@@ -844,6 +843,12 @@ fn normalize_export_table_ddl(ddl: &str, database_type: Option<DatabaseType>) ->
LEGACY_MYSQL_ROW_FORMAT_RE.replace_all(ddl, "ROW_FORMAT=DYNAMIC").into_owned()
}
fn format_export_table_ddl(ddl: &str, database_type: Option<DatabaseType>) -> String {
let ddl = normalize_export_table_ddl(ddl, database_type);
let ddl = ddl.trim().trim_end_matches(';').trim_end();
format!("{ddl};")
}
fn postgres_sequence_qualified_name(schema: &str, sequence_name: &str) -> String {
let db_type = DatabaseType::Postgres;
if schema.trim().is_empty() {
@@ -1534,8 +1539,8 @@ pub async fn export_database_sql_core(
};
match ddl_result {
Ok(ddl) => {
let ddl = normalize_export_table_ddl(&ddl, Some(db_type));
writeln!(file, "{};\n", ddl).map_err(|e| format!("Failed to write file: {e}"))?;
let ddl = format_export_table_ddl(&ddl, Some(db_type));
writeln!(file, "{ddl}\n").map_err(|e| format!("Failed to write file: {e}"))?;
}
Err(e) => {
record_export_error(
@@ -1952,7 +1957,7 @@ mod tests {
use super::concurrent_metadata_prefetch_allowed;
use super::{
build_database_export_object_source_sql, build_database_sql_export, build_export_insert_statements,
drop_table_if_exists_sql, filter_export_table_infos, format_export_sql_literal,
drop_table_if_exists_sql, filter_export_table_infos, format_export_sql_literal, format_export_table_ddl,
generate_postgres_extension_ddl, generate_postgres_sequence_create_ddl, generate_postgres_sequence_owner_ddl,
generate_postgres_sequence_setval_sql, is_postgres_extension_member_routine, normalize_export_table_ddl,
record_export_error, BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions, ExportedTableSql,
@@ -2629,6 +2634,17 @@ mod tests {
);
}
#[test]
fn table_ddl_export_has_one_statement_terminator() {
let ddl = "CREATE TABLE `users` (`id` int);;\n";
assert_eq!(format_export_table_ddl(ddl, Some(DatabaseType::Mysql)), "CREATE TABLE `users` (`id` int);");
assert_eq!(
format_export_table_ddl("CREATE TABLE users (id int)", Some(DatabaseType::Postgres)),
"CREATE TABLE users (id int);"
);
}
#[test]
fn normalizes_legacy_mysql_row_format_for_export_compatibility() {
let ddl = "CREATE TABLE `wide_table` (\n `payload` varchar(4096) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT";