Skip to content

Commit

Permalink
Fix bug with column position (#69)
Browse files Browse the repository at this point in the history
* Fix bug with column position

* Update CHANGELOG
  • Loading branch information
evgeniy-r committed May 31, 2021
1 parent f744ee8 commit 2e37946
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### ⚙️ Changed

### 🛠 Fixed
- Fix bug with dropped columns [#69](https://github.com/datanymizer/datanymizer/pull/69)
([@evgeniy-r](https://github.com/evgeniy-r))
- Update crates with security issues [#65](https://github.com/datanymizer/datanymizer/pull/65)
([@evgeniy-r](https://github.com/evgeniy-r))
- Add proper escaping after the transformation for Postgres [#60](https://github.com/datanymizer/datanymizer/pull/60)
Expand Down
14 changes: 9 additions & 5 deletions datanymizer_dumper/src/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ impl PgTable {

pub fn set_columns(&mut self, columns: Vec<PgColumn>) {
let mut map: HashMap<String, usize> = HashMap::with_capacity(columns.len());
for column in &columns {
map.insert(column.name.clone(), (column.position - 1) as usize);
let mut column_refs: Vec<_> = columns.iter().collect();
column_refs.sort_by_key(|c| c.position);
for (i, column) in column_refs.iter().enumerate() {
map.insert(column.name.clone(), i);
}

self.column_indexes = map;
Expand Down Expand Up @@ -240,8 +242,10 @@ mod tests {
inner_type: Some(0),
};
let col3 = PgColumn {
position: 3,
name: String::from("col3"),
// Column positions in Postgres are not always in sequence
// (e.g., when we have dropped some column).
position: 4,
name: String::from("col4"),
data_type: String::new(),
inner_type: Some(0),
};
Expand All @@ -253,7 +257,7 @@ mod tests {
assert_eq!(table.column_indexes.len(), 3);
assert_eq!(table.column_indexes["col1"], 0);
assert_eq!(table.column_indexes["col2"], 1);
assert_eq!(table.column_indexes["col3"], 2);
assert_eq!(table.column_indexes["col4"], 2);
}

mod query_to {
Expand Down

0 comments on commit 2e37946

Please sign in to comment.