Skip to content

Commit

Permalink
simulator: create some tables with PRIMARY KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
jussisaurio committed Jan 13, 2025
1 parent 3b3549b commit 4bd60a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 15 additions & 3 deletions simulator/generation/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ impl Arbitrary for Name {
impl Arbitrary for Table {
fn arbitrary<R: Rng>(rng: &mut R) -> Self {
let name = Name::arbitrary(rng).0;
let columns = (1..=rng.gen_range(1..5))
let mut columns = (1..=rng.gen_range(1..5))
.map(|_| Column::arbitrary(rng))
.collect();
.collect::<Vec<_>>();
// ensure only one column is primary
let mut primary_exists = false;
for column in columns.iter_mut() {
if column.primary {
if primary_exists {
column.primary = false;
} else {
primary_exists = true;
}
}
}
Table {
rows: Vec::new(),
name,
Expand All @@ -30,10 +41,11 @@ impl Arbitrary for Column {
fn arbitrary<R: Rng>(rng: &mut R) -> Self {
let name = Name::arbitrary(rng).0;
let column_type = ColumnType::arbitrary(rng);
let primary = rng.gen_bool(0.1);
Self {
name,
column_type,
primary: false,
primary,
unique: false,
}
}
Expand Down
8 changes: 7 additions & 1 deletion simulator/model/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ impl Display for Query {
if i != 0 {
write!(f, ",")?;
}
write!(f, "{} {}", column.name, column.column_type)?;
write!(
f,
"{} {} {}",
column.name,
column.column_type,
if column.primary { "PRIMARY KEY" } else { "" }
)?;
}

write!(f, ")")
Expand Down

0 comments on commit 4bd60a0

Please sign in to comment.