Skip to content

Commit

Permalink
Sort ACL entries before writing them into the file
Browse files Browse the repository at this point in the history
For e.g. schema diffing purposes, the unsorted order is very difficult
to predict.
  • Loading branch information
johto committed Sep 14, 2023
1 parent d78206e commit bd0d0f3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/custom_dump_reader.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::io::{self, BufReader, Read};

Expand Down Expand Up @@ -120,6 +121,8 @@ impl CustomDump {
filepath = vec!["index.sql".to_string()];
},
(0, "ACL") => {
contents = self.sort_acl(&item.definition);

filepath = self.get_filepath_from_combo_tag(&item, "ACL");
},
(0, "COMMENT") => {
Expand Down Expand Up @@ -435,6 +438,31 @@ impl CustomDump {
};
}

// Sorts a string of ACL entries. The unsorted order can be difficult to
// predict.
fn sort_acl(&self, acl: &str) -> Vec<String> {
let mut parts = vec![];
for entry in acl.split(";\n") {
if entry == "" {
continue;
}
parts.push(entry.to_string() + ";");
}

parts.sort_unstable_by(|a, b| {
let revoke_grant = a.starts_with("REVOKE").partial_cmp(&b.starts_with("REVOKE")).unwrap();
if revoke_grant != Ordering::Equal {
// REVOKE before GRANT
return revoke_grant.reverse();
}
return a.partial_cmp(b).unwrap();
});

parts.push(String::new());

return parts;
}

fn is_view(&self, schema: &str, pg_class_entry: &str) -> bool {
let hash_entry = View{
schema: schema.to_string(),
Expand Down

0 comments on commit bd0d0f3

Please sign in to comment.