Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with column position #69

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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