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
@@ -257,7 +257,13 @@ fn sql_selected_data(context: &ExtractContext<'_>, for_update: bool) -> Result<S
|
||||
&& info.is_some_and(is_auto_generated_column)
|
||||
&& !is_primary_key)
|
||||
|| (for_update && is_primary_key)
|
||||
|| (!for_update && context.request.options.sql.exclude_primary_keys_from_insert && is_primary_key);
|
||||
// Exclude only auto-generated primary keys; manually-assigned key
|
||||
// columns must survive "without primary keys" INSERT copies
|
||||
// (composite keys would otherwise lose NOT NULL data).
|
||||
|| (!for_update
|
||||
&& context.request.options.sql.exclude_primary_keys_from_insert
|
||||
&& is_primary_key
|
||||
&& info.is_some_and(is_auto_generated_column));
|
||||
if omit {
|
||||
omitted.push(source_name.to_string());
|
||||
} else {
|
||||
|
||||
@@ -952,7 +952,24 @@ fn sql_insert_honors_primary_key_exclusion_and_row_by_row_mode() {
|
||||
schema: Some("public".to_string()),
|
||||
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: "name".to_string(),
|
||||
data_type: "text".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
]),
|
||||
});
|
||||
request.options.sql.exclude_primary_keys_from_insert = true;
|
||||
request.options.sql.insert_mode = crate::data_grid_sql::DataGridCopyInsertMode::RowByRow;
|
||||
@@ -966,6 +983,53 @@ fn sql_insert_honors_primary_key_exclusion_and_row_by_row_mode() {
|
||||
assert_eq!(result.omitted_columns, vec!["id"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_primary_key_exclusion_keeps_manual_composite_key_members() {
|
||||
let mut request = request(DataGridExtractorId::SqlInserts);
|
||||
request.table_meta = Some(DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: None,
|
||||
table_name: "daily_stats".to_string(),
|
||||
primary_keys: vec!["id".to_string(), "stat_date".to_string()],
|
||||
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: "stat_date".to_string(),
|
||||
data_type: "date".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: true,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
DataGridColumnInfo {
|
||||
name: "name".to_string(),
|
||||
data_type: "text".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
},
|
||||
]),
|
||||
});
|
||||
request.columns = vec![column("id", 0), column("stat_date", 1), column("name", 2)];
|
||||
request.selected_column_indexes = vec![0, 1, 2];
|
||||
request.rows = vec![vec![json!(1), json!("2026-08-18"), json!("Ada")]];
|
||||
request.options.sql.exclude_primary_keys_from_insert = true;
|
||||
|
||||
let result = extract_data_grid_selection(request).expect("SQL INSERT extraction");
|
||||
|
||||
assert_eq!(result.text, "INSERT INTO \"daily_stats\" (\"stat_date\", \"name\") VALUES ('2026-08-18', 'Ada');");
|
||||
assert_eq!(result.omitted_columns, vec!["id"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_dsv_configuration() {
|
||||
let mut request = request(DataGridExtractorId::Dsv);
|
||||
|
||||
@@ -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