rust_pgdatadiff/diff/
diff_payload.rs

1use bon::bon;
2
3/// Represents a payload for performing database diffs.
4pub struct DiffPayload {
5    first_db: String,
6    second_db: String,
7    only_tables: bool,
8    only_sequences: bool,
9    only_count: bool,
10    chunk_size: i64,
11    start_position: i64,
12    max_connections: i64,
13    include_tables: Vec<String>,
14    exclude_tables: Vec<String>,
15    schema_name: String,
16    accept_invalid_certs_first_db: bool,
17    accept_invalid_certs_second_db: bool,
18}
19
20#[bon]
21impl DiffPayload {
22    /// Creates a new `DiffPayload` instance.
23    ///
24    /// # Arguments
25    ///
26    /// * `first_db` - The name of the first database.
27    /// * `second_db` - The name of the second database.
28    /// * `only_data` - A flag indicating whether to compare only data.
29    /// * `only_sequences` - A flag indicating whether to compare only sequences.
30    /// * `count_only` - A flag indicating whether to count differences only.
31    /// * `chunk_size` - The chunk size for processing large tables.
32    /// * `start_position` - The start position for the comparison.
33    /// * `max_connections` - The maximum number of database connections to use.
34    /// * `include_tables` - A list of tables to include in the comparison.
35    /// * `exclude_tables` - A list of tables to exclude in the comparison.
36    /// * `schema_name` - The name of the schema to compare.
37    ///
38    /// # Returns
39    ///
40    /// A new `DiffPayload` instance.
41    #[builder]
42    pub fn new(
43        first_db: impl Into<String>,
44        second_db: impl Into<String>,
45        only_tables: bool,
46        only_sequences: bool,
47        only_count: bool,
48        chunk_size: i64,
49        start_position: i64,
50        max_connections: i64,
51        include_tables: Vec<impl Into<String>>,
52        exclude_tables: Vec<impl Into<String>>,
53        schema_name: impl Into<String>,
54        accept_invalid_certs_first_db: bool,
55        accept_invalid_certs_second_db: bool,
56    ) -> Self {
57        let has_included_tables = !include_tables.is_empty();
58        let has_excluded_tables = !exclude_tables.is_empty();
59
60        if has_included_tables && has_excluded_tables {
61            panic!("Cannot include and exclude tables at the same time");
62        }
63
64        Self {
65            first_db: first_db.into(),
66            second_db: second_db.into(),
67            only_tables,
68            only_sequences,
69            only_count,
70            chunk_size,
71            start_position,
72            max_connections,
73            include_tables: include_tables.into_iter().map(|t| t.into()).collect(),
74            exclude_tables: exclude_tables.into_iter().map(|t| t.into()).collect(),
75            schema_name: schema_name.into(),
76            accept_invalid_certs_first_db,
77            accept_invalid_certs_second_db,
78        }
79    }
80
81    pub fn first_db(&self) -> &str {
82        &self.first_db
83    }
84    pub fn second_db(&self) -> &str {
85        &self.second_db
86    }
87    pub fn only_tables(&self) -> bool {
88        self.only_tables
89    }
90    pub fn only_sequences(&self) -> bool {
91        self.only_sequences
92    }
93    pub fn only_count(&self) -> bool {
94        self.only_count
95    }
96    pub fn chunk_size(&self) -> i64 {
97        self.chunk_size
98    }
99    pub fn start_position(&self) -> i64 {
100        self.start_position
101    }
102    pub fn max_connections(&self) -> u32 {
103        self.max_connections as u32
104    }
105    pub fn included_tables(&self) -> &Vec<String> {
106        &self.include_tables
107    }
108    pub fn excluded_tables(&self) -> &Vec<String> {
109        &self.exclude_tables
110    }
111    pub fn schema_name(&self) -> &str {
112        &self.schema_name
113    }
114    pub fn accept_invalid_certs_first_db(&self) -> bool {
115        self.accept_invalid_certs_first_db
116    }
117    pub fn accept_invalid_certs_second_db(&self) -> bool {
118        self.accept_invalid_certs_second_db
119    }
120    pub fn any_accept_invalid_certs(&self) -> bool {
121        self.accept_invalid_certs_first_db || self.accept_invalid_certs_second_db
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    #[should_panic = "Cannot include and exclude tables at the same time"]
131    fn test_new_diff_payload() {
132        _ = DiffPayload::builder()
133            .first_db("first_db")
134            .second_db("second_db")
135            .only_tables(false)
136            .only_sequences(false)
137            .only_count(false)
138            .chunk_size(10000)
139            .start_position(0)
140            .max_connections(10)
141            .include_tables(vec!["table1"])
142            .exclude_tables(vec!["table2"])
143            .schema_name("schema_name")
144            .accept_invalid_certs_first_db(false)
145            .accept_invalid_certs_second_db(false)
146            .build();
147    }
148}