fix(grid): keep manual primary keys in exclude-PK insert copies
Closes #8293
This commit is contained in:
5 files changed
+261
-7
No files matched your search
@@ -469,10 +469,16 @@ pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatemen
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
// "Exclude primary keys" keeps manually-assigned key columns: dropping a
|
||||
// non-auto-generated PK member loses data and yields an INSERT that cannot
|
||||
// satisfy NOT NULL. Only auto-generated (auto_increment/identity) keys are
|
||||
// excluded, matching DBeaver's SQLGeneratorInsert excludeAutoGeneratedColumn.
|
||||
let insert_columns: Vec<(&str, usize, Option<DataGridColumnInfo>)> = insertable_columns
|
||||
.iter()
|
||||
.filter(|(column, _, _)| {
|
||||
!options.exclude_primary_keys || !primary_key_set.contains(&normalize_column_name(column))
|
||||
.filter(|(column, _, info)| {
|
||||
!options.exclude_primary_keys
|
||||
|| !primary_key_set.contains(&normalize_column_name(column))
|
||||
|| !info.as_ref().is_some_and(is_auto_generated_column)
|
||||
})
|
||||
.cloned()
|
||||
.collect();
|
||||
@@ -3792,7 +3798,32 @@ mod tests {
|
||||
schema: None,
|
||||
table_name: "users".to_string(),
|
||||
primary_keys: vec!["id".to_string()],
|
||||
columns: None,
|
||||
columns: Some(vec![
|
||||
DataGridColumnInfo {
|
||||
name: "id".to_string(),
|
||||
data_type: "bigint".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: true,
|
||||
column_default: None,
|
||||
extra: Some("auto_increment".to_string()),
|
||||
},
|
||||
DataGridColumnInfo {
|
||||
name: "login_name".to_string(),
|
||||
data_type: "varchar(64)".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
DataGridColumnInfo {
|
||||
name: "display_name".to_string(),
|
||||
data_type: "varchar(64)".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
]),
|
||||
}),
|
||||
columns: vec!["id".to_string(), "login_name".to_string(), "display_name".to_string()],
|
||||
column_types: None,
|
||||
@@ -3808,6 +3839,128 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_insert_primary_key_exclusion_keeps_manual_composite_key_members() {
|
||||
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
identifier_quote: None,
|
||||
table_meta: Some(DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: None,
|
||||
table_name: "move_statistic_product_daily".to_string(),
|
||||
primary_keys: vec!["id".to_string(), "stat_date".to_string()],
|
||||
columns: Some(vec![
|
||||
DataGridColumnInfo {
|
||||
name: "id".to_string(),
|
||||
data_type: "bigint unsigned".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: true,
|
||||
column_default: None,
|
||||
extra: Some("auto_increment".to_string()),
|
||||
},
|
||||
DataGridColumnInfo {
|
||||
name: "stat_date".to_string(),
|
||||
data_type: "date".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: true,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
DataGridColumnInfo {
|
||||
name: "product_name".to_string(),
|
||||
data_type: "varchar(255)".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
column_default: Some("".to_string()),
|
||||
extra: None,
|
||||
},
|
||||
]),
|
||||
}),
|
||||
columns: vec!["id".to_string(), "stat_date".to_string(), "product_name".to_string()],
|
||||
column_types: None,
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!(1), json!("2026-08-18"), json!("sweater")]],
|
||||
exclude_primary_keys: true,
|
||||
include_computed_columns: false,
|
||||
insert_mode: DataGridCopyInsertMode::Merged,
|
||||
});
|
||||
assert_eq!(
|
||||
statement.as_deref(),
|
||||
Some("INSERT INTO `move_statistic_product_daily` (`stat_date`, `product_name`) VALUES ('2026-08-18', 'sweater');")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_insert_primary_key_exclusion_keeps_manual_primary_keys() {
|
||||
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
identifier_quote: None,
|
||||
table_meta: Some(DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "countries".to_string(),
|
||||
primary_keys: vec!["code".to_string()],
|
||||
columns: Some(vec![
|
||||
DataGridColumnInfo {
|
||||
name: "code".to_string(),
|
||||
data_type: "varchar(2)".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: true,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
DataGridColumnInfo {
|
||||
name: "label".to_string(),
|
||||
data_type: "text".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
]),
|
||||
}),
|
||||
columns: vec!["code".to_string(), "label".to_string()],
|
||||
column_types: None,
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!("AD"), json!("Andorra")]],
|
||||
exclude_primary_keys: true,
|
||||
include_computed_columns: false,
|
||||
insert_mode: DataGridCopyInsertMode::Merged,
|
||||
});
|
||||
assert_eq!(
|
||||
statement.as_deref(),
|
||||
Some("INSERT INTO \"public\".\"countries\" (\"code\", \"label\") VALUES ('AD', 'Andorra');")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_insert_primary_key_exclusion_keeps_unknown_metadata_primary_keys() {
|
||||
// Without column metadata we cannot prove the key is auto-generated;
|
||||
// keep it rather than silently dropping NOT NULL data.
|
||||
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
identifier_quote: None,
|
||||
table_meta: Some(DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: None,
|
||||
table_name: "users".to_string(),
|
||||
primary_keys: vec!["id".to_string()],
|
||||
columns: None,
|
||||
}),
|
||||
columns: vec!["id".to_string(), "login_name".to_string()],
|
||||
column_types: None,
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!(1), json!("ada")]],
|
||||
exclude_primary_keys: true,
|
||||
include_computed_columns: false,
|
||||
insert_mode: DataGridCopyInsertMode::Merged,
|
||||
});
|
||||
assert_eq!(statement.as_deref(), Some("INSERT INTO `users` (`id`, `login_name`) VALUES (1, 'ada');"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_insert_keeps_json_cells_as_single_json_literals() {
|
||||
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
|
||||
|
||||
Reference in new issue
Block a user