Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(dameng): skip identity insert for auto increment columns

lxk95505e0539a1e07
diff --git a/crates/dbx-core/src/data/database_export.rs b/crates/dbx-core/src/data/database_export.rs
index 102a1bcdce..5c281b67ba 100644
--- a/crates/dbx-core/src/data/database_export.rs
+++ b/crates/dbx-core/src/data/database_export.rs
@@ -2153,6 +2153,81 @@ fn format_export_table_ddl(ddl: &str, database_type: Option<DatabaseType>, opts:
format!("{ddl};")
}
+pub(crate) fn is_dameng_auto_increment_ddl(ddl: &str) -> bool {
+ let mut in_single_quote = false;
+ let mut in_double_quote = false;
+ let mut in_line_comment = false;
+ let mut in_block_comment = false;
+ let mut chars = ddl.chars().peekable();
+ let mut current_word = String::new();
+
+ while let Some(ch) = chars.next() {
+ if in_single_quote {
+ if ch == '\'' {
+ if chars.peek() == Some(&'\'') {
+ chars.next();
+ } else {
+ in_single_quote = false;
+ }
+ }
+ continue;
+ }
+ if in_double_quote {
+ if ch == '"' {
+ if chars.peek() == Some(&'"') {
+ chars.next();
+ } else {
+ in_double_quote = false;
+ }
+ }
+ continue;
+ }
+ if in_line_comment {
+ if ch == '\n' {
+ in_line_comment = false;
+ }
+ continue;
+ }
+ if in_block_comment {
+ if ch == '*' && chars.peek() == Some(&'/') {
+ chars.next();
+ in_block_comment = false;
+ }
+ continue;
+ }
+
+ if ch == '-' && chars.peek() == Some(&'-') {
+ chars.next();
+ in_line_comment = true;
+ continue;
+ }
+ if ch == '/' && chars.peek() == Some(&'*') {
+ chars.next();
+ in_block_comment = true;
+ continue;
+ }
+ if ch == '\'' {
+ in_single_quote = true;
+ continue;
+ }
+ if ch == '"' {
+ in_double_quote = true;
+ continue;
+ }
+
+ if ch.is_ascii_alphanumeric() || ch == '_' {
+ current_word.push(ch);
+ } else {
+ if current_word.eq_ignore_ascii_case("auto_increment") || current_word.eq_ignore_ascii_case("autoincrement")
+ {
+ return true;
+ }
+ current_word.clear();
+ }
+ }
+ current_word.eq_ignore_ascii_case("auto_increment") || current_word.eq_ignore_ascii_case("autoincrement")
+}
+
/// PostgreSQL database export: split the statements that may depend on
/// schema-wide routines out of a relation's DDL, returning
/// `(table, indexes, triggers)`.
@@ -3034,6 +3109,7 @@ fn write_database_export_rows<W: Write>(
insert_dialect,
SqlInsertMode::default(),
false,
+ None,
)
}
@@ -3050,7 +3126,24 @@ fn write_database_export_rows_with_mode<W: Write>(
insert_dialect: SqlInsertDialect,
insert_mode: SqlInsertMode,
preserve_original_language: bool,
+ table_ddl: Option<&str>,
) -> Result<(), String> {
+ let normalized_column_extras;
+ let column_extras = if *db_type == DatabaseType::Dameng && table_ddl.is_some_and(is_dameng_auto_increment_ddl) {
+ normalized_column_extras = column_extras
+ .iter()
+ .map(|extra| {
+ if extra.as_deref().is_some_and(|e| e.eq_ignore_ascii_case("identity")) {
+ Some("auto_increment".to_string())
+ } else {
+ extra.clone()
+ }
+ })
+ .collect::<Vec<_>>();
+ normalized_column_extras.as_slice()
+ } else {
+ column_extras
+ };
let insert_indices = columns
.iter()
.enumerate()
@@ -4466,6 +4559,8 @@ async fn export_database_sql_core_inner(
error_summary: None,
});
+ let mut table_ddl_text: Option<String> = None;
+
// Export structure
if request.include_structure {
if request.drop_table_if_exists {
@@ -4519,6 +4614,7 @@ async fn export_database_sql_core_inner(
}
match ddl_result {
Ok(ddl) => {
+ table_ddl_text = Some(ddl.clone());
let (ddl, indexes, triggers) = split_postgres_export_table_ddl(&ddl, db_type);
deferred_postgres_indexes.extend(indexes);
deferred_postgres_triggers.extend(triggers);
@@ -4594,6 +4690,29 @@ async fn export_database_sql_core_inner(
let col_types = columns.iter().map(|c| Some(c.data_type.clone())).collect::<Vec<_>>();
let col_extras = columns.iter().map(|c| c.extra.clone()).collect::<Vec<_>>();
+ if table_ddl_text.is_none()
+ && db_type == DatabaseType::Dameng
+ && col_extras
+ .iter()
+ .any(|extra| extra.as_deref().is_some_and(|e| e.to_ascii_lowercase().contains("identity")))
+ {
+ if let Ok(ddl) = await_export_operation(
+ &request.export_id,
+ Box::pin(get_export_table_ddl_isolated(
+ state.clone(),
+ request.connection_id.clone(),
+ request.database.clone(),
+ request.schema.clone(),
+ table_name.clone(),
+ client_session_id.clone(),
+ )),
+ )
+ .await
+ {
+ table_ddl_text = Some(ddl);
+ }
+ }
+
if !col_names.is_empty() {
if let Some(snapshot_session_id) = request.snapshot_session_id.as_deref() {
let sql = database_export_select_sql(&col_names, &col_types, table_name, &request.schema, &db_type);
@@ -4625,6 +4744,7 @@ async fn export_database_sql_core_inner(
request.insert_dialect,
request.insert_mode,
request.preserve_original_language,
+ table_ddl_text.as_deref(),
)?;
total_rows_exported += rows.len() as u64;
on_progress(ExportProgress {
@@ -4744,6 +4864,7 @@ async fn export_database_sql_core_inner(
request.insert_dialect,
request.insert_mode,
request.preserve_original_language,
+ table_ddl_text.as_deref(),
)?;
total_rows_exported += row_count as u64;
if use_keyset {
@@ -5155,15 +5276,16 @@ mod tests {
format_mysql_spatial_export_literal, format_postgres_spatial_export_literal_with_srid,
format_xugu_spatial_export_literal, generate_postgres_enum_ddl, generate_postgres_extension_ddl,
generate_postgres_sequence_create_ddl, generate_postgres_sequence_post_table_ddl,
- generate_postgres_sequence_setval_sql, is_postgres_extension_member_routine, mysql_database_export_preamble,
- mysql_view_dependencies_from_rows, mysql_view_dependencies_sql, normalize_export_table_ddl,
- record_export_destination_identity, record_export_error, replace_database_export_select_list,
- sort_export_views_by_dependencies, split_postgres_export_table_ddl, write_database_export_rows,
- write_database_export_rows_with_mode, BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions,
- BuildExportSqlInsertOptions, DatabaseExportObjectCounts, DatabaseExportRequest, DatabaseExportWriter,
- DdlNormalizeOptions, ExportedTableSql, PostgresExportEnum, PostgresExportExtension, PostgresExportSequence,
- PostgresExtensionMembers, SqlInsertDialect, SqlInsertMode, DATABASE_EXPORT_INSERT_BATCH_SIZE,
- DATABASE_EXPORT_ROW_LIMIT, POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL, POSTGRES_EXPORT_SEQUENCES_SQL,
+ generate_postgres_sequence_setval_sql, is_dameng_auto_increment_ddl, is_postgres_extension_member_routine,
+ mysql_database_export_preamble, mysql_view_dependencies_from_rows, mysql_view_dependencies_sql,
+ normalize_export_table_ddl, record_export_destination_identity, record_export_error,
+ replace_database_export_select_list, sort_export_views_by_dependencies, split_postgres_export_table_ddl,
+ write_database_export_rows, write_database_export_rows_with_mode, BuildDatabaseSqlExportOptions,
+ BuildExportInsertStatementsOptions, BuildExportSqlInsertOptions, DatabaseExportObjectCounts,
+ DatabaseExportRequest, DatabaseExportWriter, DdlNormalizeOptions, ExportedTableSql, PostgresExportEnum,
+ PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers, SqlInsertDialect, SqlInsertMode,
+ DATABASE_EXPORT_INSERT_BATCH_SIZE, DATABASE_EXPORT_ROW_LIMIT, POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL,
+ POSTGRES_EXPORT_SEQUENCES_SQL,
};
use super::{ExportProgress, LenientExportErrors};
use crate::connection::AppState;
@@ -8148,6 +8270,7 @@ mod tests {
SqlInsertDialect::Source,
SqlInsertMode::Single,
false,
+ None,
)
.unwrap();
drop(file);
@@ -8461,6 +8584,70 @@ mod tests {
);
}
+ #[test]
+ fn dameng_auto_increment_export_inserts_omit_identity_insert() {
+ let statements = build_export_insert_statements(BuildExportInsertStatementsOptions {
+ database_type: Some(DatabaseType::Dameng),
+ identifier_quote: None,
+ schema: Some("SYSDBA".to_string()),
+ table_name: Some("USERS".to_string()),
+ qualified_table_name: None,
+ columns: vec!["ID".to_string(), "NAME".to_string()],
+ column_types: vec![Some("INT".to_string()), Some("VARCHAR(20)".to_string())],
+ column_extras: vec![Some("auto_increment".to_string()), None],
+ spatial_columns: Vec::new(),
+ spatial_values: Vec::new(),
+ rows: vec![vec![json!(1), json!("Ada")]],
+ batch_size: Some(10),
+ preserve_original_language: false,
+ })
+ .unwrap();
+
+ assert_eq!(statements, vec!["INSERT INTO \"SYSDBA\".\"USERS\" (\"ID\", \"NAME\") VALUES (1, 'Ada');"]);
+ }
+
+ #[test]
+ fn dameng_database_row_writer_omits_identity_insert_when_table_ddl_has_auto_increment() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("users.sql");
+ let mut file = std::fs::File::create(&path).unwrap();
+
+ let ddl = "CREATE TABLE \"SYSDBA\".\"USERS\" (\"ID\" INT AUTO_INCREMENT PRIMARY KEY, \"NAME\" VARCHAR(20));";
+ write_database_export_rows_with_mode(
+ &mut file,
+ &[vec![json!(1), json!("Ada")]],
+ &["ID".to_string(), "NAME".to_string()],
+ &[Some("INT".to_string()), Some("VARCHAR(20)".to_string())],
+ // Catalog reports "identity" because DM c.INFO2 & 0x01 = 1 for auto_increment
+ &[Some("identity".to_string()), None],
+ "USERS",
+ "SYSDBA",
+ &DatabaseType::Dameng,
+ SqlInsertDialect::Source,
+ SqlInsertMode::Single,
+ false,
+ Some(ddl),
+ )
+ .unwrap();
+ drop(file);
+
+ let content = std::fs::read_to_string(path).unwrap();
+ assert!(!content.contains("IDENTITY_INSERT"));
+ assert_eq!(content, "INSERT INTO \"SYSDBA\".\"USERS\" (\"ID\", \"NAME\") VALUES (1, 'Ada');\n\n");
+ }
+
+ #[test]
+ fn dameng_is_auto_increment_ddl_detection() {
+ assert!(is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"ID\" INT AUTO_INCREMENT, \"NAME\" VARCHAR(20))"));
+ assert!(is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"ID\" INT autoincrement PRIMARY KEY)"));
+ assert!(is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"ID\" INT AUTO_INCREMENT=100)"));
+ assert!(!is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"ID\" INT IDENTITY(1, 1), \"NAME\" VARCHAR(20))"));
+ assert!(!is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"AUTO_INCREMENT\" INT)"));
+ assert!(!is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"VAL\" VARCHAR(50) DEFAULT 'AUTO_INCREMENT')"));
+ assert!(!is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\"ID\" INT /* AUTO_INCREMENT */)"));
+ assert!(!is_dameng_auto_increment_ddl("CREATE TABLE \"T\" (\n-- AUTO_INCREMENT\n\"ID\" INT)"));
+ }
+
#[test]
fn sqlserver_identity_export_inserts_enable_identity_insert() {
// The SQL Server column metadata reports `identity(seed,increment)`;
diff --git a/crates/dbx-core/src/data/table_export.rs b/crates/dbx-core/src/data/table_export.rs
index ccffb4ec06..802a502ada 100644
--- a/crates/dbx-core/src/data/table_export.rs
+++ b/crates/dbx-core/src/data/table_export.rs
@@ -1671,6 +1671,37 @@ async fn export_table_data_core_inner(
let primary_keys: Vec<String> = columns.iter().filter(|c| c.is_primary_key).map(|c| c.name.clone()).collect();
(col_names, column_types, column_extras, primary_keys)
};
+ let mut column_extras = column_extras;
+ if request.format.eq_ignore_ascii_case("sql")
+ && db_type == DatabaseType::Dameng
+ && column_extras
+ .iter()
+ .any(|extra| extra.as_deref().is_some_and(|e| e.to_ascii_lowercase().contains("identity")))
+ {
+ if let Ok(ddl) = crate::schema::get_table_ddl_core(
+ state,
+ &request.connection_id,
+ &request.database,
+ request.schema.as_deref().unwrap_or(""),
+ &request.table_name,
+ None,
+ )
+ .await
+ {
+ if crate::data::database_export::is_dameng_auto_increment_ddl(&ddl) {
+ column_extras = column_extras
+ .into_iter()
+ .map(|extra| {
+ if extra.as_deref().is_some_and(|e| e.eq_ignore_ascii_case("identity")) {
+ Some("auto_increment".to_string())
+ } else {
+ extra
+ }
+ })
+ .collect();
+ }
+ }
+ }
let sql_projection = if request.format.eq_ignore_ascii_case("sql") {
Some(SqlExportProjection::resolve(&col_names, request.selected_columns.as_deref())?)
diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rs
index 07dfe4f207..7f0e659398 100644
--- a/crates/dbx-core/src/data/transfer.rs
+++ b/crates/dbx-core/src/data/transfer.rs
@@ -1849,7 +1849,9 @@ pub(crate) fn is_postgres_generated_always_identity_extra(extra: Option<&str>) -
pub(crate) fn is_identity_column_extra(extra: Option<&str>) -> bool {
extra.is_some_and(|value| {
let normalized = value.trim().to_ascii_lowercase();
- normalized.contains("identity") || normalized.contains("auto_increment") || normalized.contains("autoincrement")
+ normalized.contains("identity")
+ && !normalized.contains("auto_increment")
+ && !normalized.contains("autoincrement")
})
}
@@ -16233,7 +16235,10 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;
&[String::from("id"), String::from("name")],
&[Some(String::from("identity")), None],
));
- assert!(selected_columns_include_identity_extras(
+ // Dameng AUTO_INCREMENT columns are not true identity columns (issue #12124):
+ // they must not trigger IDENTITY_INSERT wrappers, so auto_increment no
+ // longer counts as an identity extra.
+ assert!(!selected_columns_include_identity_extras(
&[String::from("id")],
&[Some(String::from("auto_increment"))],
));
@@ -16247,10 +16252,12 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;
fn detects_selected_identity_columns_from_target_metadata() {
let target_columns = vec![
db::ColumnInfo { extra: Some("identity".to_string()), ..test_column("ID", "INT") },
+ db::ColumnInfo { extra: Some("auto_increment".to_string()), ..test_column("AUTO_ID", "BIGINT") },
test_column("NAME", "VARCHAR(20)"),
];
assert!(selected_columns_include_identity_columns(&[String::from("id")], &target_columns));
+ assert!(!selected_columns_include_identity_columns(&[String::from("auto_id")], &target_columns));
assert!(!selected_columns_include_identity_columns(&[String::from("name")], &target_columns));
}
diff --git a/crates/dbx-sql-data/src/data_grid_sql.rs b/crates/dbx-sql-data/src/data_grid_sql.rs
index 4aee58ae4e..efc4d99034 100644
--- a/crates/dbx-sql-data/src/data_grid_sql.rs
+++ b/crates/dbx-sql-data/src/data_grid_sql.rs
@@ -782,11 +782,28 @@ pub(crate) fn build_data_grid_copy_insert_statements_with_formatters(
// SQL Server and Dameng reject explicit values for identity columns unless the
// statement runs between `SET IDENTITY_INSERT <table> ON` and `OFF` (SQL Server
// error 544), so a copied INSERT that carries the identity column must ship the
- // wrapper. Each statement is wrapped on its own so row-by-row copies stay
- // individually executable, matching the SQL export path.
- let needs_identity_insert_wrapper =
- matches!(options.database_type, Some(DatabaseType::SqlServer | DatabaseType::Dameng))
- && insert_columns.iter().any(|(_, _, info)| info.as_ref().is_some_and(is_auto_generated_column));
+ // wrapper. Dameng AUTO_INCREMENT columns are not IDENTITY columns (DM error -2717)
+ // and must not be wrapped. Each statement is wrapped on its own so row-by-row
+ // copies stay individually executable, matching the SQL export path.
+ let needs_identity_insert_wrapper = match options.database_type {
+ Some(DatabaseType::SqlServer) => insert_columns.iter().any(|(_, _, info)| {
+ info.as_ref().is_some_and(|col| {
+ col.extra.as_deref().is_some_and(|extra| extra.to_ascii_lowercase().contains("identity"))
+ || is_auto_generated_column(col)
+ })
+ }),
+ Some(DatabaseType::Dameng) => insert_columns.iter().any(|(_, _, info)| {
+ info.as_ref().is_some_and(|col| {
+ col.extra.as_deref().is_some_and(|extra| {
+ let normalized = extra.to_ascii_lowercase();
+ normalized.contains("identity")
+ && !normalized.contains("auto_increment")
+ && !normalized.contains("autoincrement")
+ })
+ })
+ }),
+ _ => false,
+ };
if needs_identity_insert_wrapper {
return Some(
statements
@@ -6638,6 +6655,24 @@ mod tests {
);
}
+ #[test]
+ fn dameng_copy_insert_omits_identity_wrapper_for_auto_increment_columns() {
+ let mut options = sqlserver_identity_copy_insert_options(
+ vec![vec![json!(1), json!("t_destype")]],
+ DataGridCopyInsertMode::Merged,
+ );
+ options.database_type = Some(DatabaseType::Dameng);
+ options.table_meta.as_mut().unwrap().columns = Some(vec![
+ column("table_id", "bigint", false, Some("auto_increment")),
+ column("table_name", "varchar", true, None),
+ ]);
+ let statement = build_data_grid_copy_insert_statement(options);
+ assert_eq!(
+ statement.as_deref(),
+ Some("INSERT INTO \"dbo\".\"gen_table\" (\"table_id\", \"table_name\") VALUES (1, 't_destype');")
+ );
+ }
+
#[test]
fn mysql_copy_statements_preserve_blob_hex_literals() {
let table_meta = DataGridTableMeta {