From c022f40bc6966103bbd3f283f10185e4ee24343f Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 5 Mar 2023 15:16:46 +0200 Subject: [PATCH] check if foreign key is unique when adding entry --- src/generate_sql.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/generate_sql.rs b/src/generate_sql.rs index b449d74..2e48fb3 100644 --- a/src/generate_sql.rs +++ b/src/generate_sql.rs @@ -125,11 +125,8 @@ pub fn generate_fake_entries( .get(column.name.as_str()) .expect("Failed to get column guess"); for entry_idx in 0..(rows_per_table as usize) { - entries[entry_idx].push(generate_value( - &mut rng, - &value_guess, - &mut auto_increment_counter, - )); + let value = generate_value(&mut rng, &value_guess, &mut auto_increment_counter); + entries[entry_idx].push(value); } } } @@ -140,17 +137,17 @@ pub fn generate_fake_entries( let before_retain = entries_with_foreign_keys.len(); entries_with_foreign_keys.retain(|(table_idx, entry_idx)| { - for (column_idx, foreign_table_idx, foreign_column_idx) in - &all_foreign_columns[*table_idx] + for (column_idx, foreign_table_idx, foreign_column_idx) in &all_foreign_columns[*table_idx] { - let available_values: Vec<&str>; + let mut available_values: Vec<&str>; // If the foreign column, is also a foreign of the other table, ... // Then we need to filter out available options which have not been filled in - if all_foreign_columns[*foreign_table_idx] + let is_foreign_column_also_foreign = all_foreign_columns[*foreign_table_idx] .iter() .find(|(idx, _, _)| idx == foreign_column_idx) - .is_some() + .is_some(); + if is_foreign_column_also_foreign { available_values = all_entries[*foreign_table_idx] .iter() @@ -167,6 +164,14 @@ pub fn generate_fake_entries( .collect(); } + let used_values = all_entries[*table_idx].iter() + .enumerate() + .filter(|(entry_idx, _)| entries_with_foreign_keys_copy.contains(&(*table_idx, *entry_idx))) + .map(|(_, entry)| entry[*column_idx].as_str()) + .collect::>(); + + available_values.retain(|value| !used_values.contains(value)); + if let Some(chosen_value) = available_values.choose(&mut rng) { all_entries[*table_idx][*entry_idx][*column_idx] = chosen_value.to_string(); } else {