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

Drop SQLX from dependencies #18

Merged
merged 1 commit into from
Jun 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 126 additions & 87 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-pgdatadiff"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
license = "MIT"
description = "Rust library for comparing two PostgreSQL databases"
Expand All @@ -27,7 +27,9 @@ tracing-subscriber = "0.3.18"
tracing = "0.1.40"
cargo-nextest = "0.9.72"
deadpool-postgres = "0.14.0"
rust-pgdatadiff = { path = ".", version = "0.1.5" }
postgres-native-tls = "0.5.0"
native-tls = "0.2.12"
rust-pgdatadiff = { path = ".", version = "0.1.6" }

[dependencies]
anyhow.workspace = true
Expand All @@ -40,6 +42,8 @@ tracing-subscriber.workspace = true
tracing.workspace = true
cargo-nextest.workspace = true
deadpool-postgres.workspace = true
postgres-native-tls.workspace = true
native-tls.workspace = true

[dependencies.clap]
version = "4.5.4"
Expand Down
10 changes: 10 additions & 0 deletions examples/example_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ enum Commands {
/// Schema name
#[arg(long, default_value = "public", required = false)]
schema_name: String,
/// Accept invalid TLS certificates for the first database
#[arg(long, default_value_t = false, required = false)]
accept_invalid_certs_first_db: bool,
/// Accept invalid TLS certificates for the second database
#[arg(long, default_value_t = false, required = false)]
accept_invalid_certs_second_db: bool,
},
}

Expand All @@ -77,6 +83,8 @@ async fn main() -> Result<()> {
include_tables,
exclude_tables,
schema_name,
accept_invalid_certs_first_db,
accept_invalid_certs_second_db,
} => {
let payload = DiffPayload::new(
first_db.clone(),
Expand All @@ -90,6 +98,8 @@ async fn main() -> Result<()> {
include_tables.to_vec(),
exclude_tables.to_vec(),
schema_name.clone(),
*accept_invalid_certs_first_db,
*accept_invalid_certs_second_db,
);
let _ = Differ::diff_dbs(payload).await;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion rust-pgdatadiff-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-pgdatadiff-client"
version = "0.1.11"
version = "0.1.13"
edition = "2021"
license = "MIT"
description = "Rust client for comparing two PostgreSQL databases"
Expand Down
20 changes: 20 additions & 0 deletions rust-pgdatadiff-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ enum Commands {
/// Schema name
#[arg(long, default_value = "public", required = false)]
schema_name: String,
/// Accept invalid TLS certificates for the first database
#[arg(long, default_value_t = false, required = false)]
accept_invalid_certs_first_db: bool,
/// Accept invalid TLS certificates for the second database
#[arg(long, default_value_t = false, required = false)]
accept_invalid_certs_second_db: bool,
},
}

Expand All @@ -77,6 +83,8 @@ async fn main_clap() -> Result<()> {
include_tables,
exclude_tables,
schema_name,
accept_invalid_certs_first_db,
accept_invalid_certs_second_db,
} => {
let payload = DiffPayload::new(
first_db.clone(),
Expand All @@ -90,6 +98,8 @@ async fn main_clap() -> Result<()> {
include_tables.to_vec(),
exclude_tables.to_vec(),
schema_name.clone(),
*accept_invalid_certs_first_db,
*accept_invalid_certs_second_db,
);
let _ = Differ::diff_dbs(payload).await;
Ok(())
Expand Down Expand Up @@ -143,6 +153,14 @@ async fn main_inquire() -> Result<()> {
.with_default("public")
.with_help_message("Enter the DB schema name to perform the comparison on")
.prompt()?;
let accept_invalid_certs_first_db =
Confirm::new("Do you want to accept invalid TLS cert for first DB?")
.with_default(false)
.prompt()?;
let accept_invalid_certs_second_db =
Confirm::new("Do you want to accept invalid TLS cert for second DB?")
.with_default(false)
.prompt()?;

let payload = DiffPayload::new(
first_db,
Expand All @@ -162,6 +180,8 @@ async fn main_inquire() -> Result<()> {
.flat_map(|t| t.split(','))
.collect(),
schema_name,
accept_invalid_certs_first_db,
accept_invalid_certs_second_db,
);
let _ = Differ::diff_dbs(payload).await;
Ok(())
Expand Down
38 changes: 32 additions & 6 deletions src/diff/diff_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,55 @@ impl Differ {
pub async fn diff_dbs(diff_payload: DiffPayload) -> Result<Vec<DiffOutput>> {
info!("{}", "Initiating DB diffing…".bold().blue());

let tls_connector = if diff_payload.any_accept_invalid_certs() {
use native_tls::TlsConnector;
use postgres_native_tls::MakeTlsConnector;

let tls_connector = TlsConnector::builder()
.danger_accept_invalid_certs(true)
.build()
.unwrap();

Some(MakeTlsConnector::new(tls_connector))
} else {
None
};

let mut first_cfg = Config::new();
first_cfg.url = Some(diff_payload.first_db().to_string());
// first_cfg.application_name = Some(String::from("rust-pgdatadiff"));
first_cfg.application_name = Some(String::from("rust-pgdatadiff"));
first_cfg.pool = Some(PoolConfig::new(diff_payload.max_connections() as usize));
first_cfg.manager = Some(ManagerConfig {
recycling_method: RecyclingMethod::Fast,
});

let mut second_cfg = Config::new();
second_cfg.url = Some(diff_payload.second_db().to_string());
// second_cfg.application_name = Some(String::from("rust-pgdatadiff"));
second_cfg.application_name = Some(String::from("rust-pgdatadiff"));
second_cfg.pool = Some(PoolConfig::new(diff_payload.max_connections() as usize));
second_cfg.manager = Some(ManagerConfig {
recycling_method: RecyclingMethod::Fast,
});

info!("{}", "Connected to first DB".magenta().bold());
let first_db_pool = first_cfg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
let first_db_pool = if diff_payload.accept_invalid_certs_first_db() {
first_cfg
.create_pool(Some(Runtime::Tokio1), tls_connector.clone().unwrap())
.unwrap()
} else {
first_cfg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap()
};

info!("{}", "Connected to second DB".magenta().bold());
let second_db_pool = second_cfg
.create_pool(Some(Runtime::Tokio1), NoTls)
.unwrap();
let second_db_pool = if diff_payload.accept_invalid_certs_second_db() {
second_cfg
.create_pool(Some(Runtime::Tokio1), tls_connector.unwrap())
.unwrap()
} else {
second_cfg
.create_pool(Some(Runtime::Tokio1), NoTls)
.unwrap()
};

let db_clients = DBClients::new(first_db_pool, second_db_pool);

Expand Down
17 changes: 17 additions & 0 deletions src/diff/diff_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct DiffPayload {
include_tables: Vec<String>,
exclude_tables: Vec<String>,
schema_name: String,
accept_invalid_certs_first_db: bool,
accept_invalid_certs_second_db: bool,
}

impl DiffPayload {
Expand Down Expand Up @@ -46,6 +48,8 @@ impl DiffPayload {
include_tables: Vec<impl Into<String>>,
exclude_tables: Vec<impl Into<String>>,
schema_name: impl Into<String>,
accept_invalid_certs_first_db: bool,
accept_invalid_certs_second_db: bool,
) -> Self {
let has_included_tables = !include_tables.is_empty();
let has_excluded_tables = !exclude_tables.is_empty();
Expand All @@ -66,6 +70,8 @@ impl DiffPayload {
include_tables: include_tables.into_iter().map(|t| t.into()).collect(),
exclude_tables: exclude_tables.into_iter().map(|t| t.into()).collect(),
schema_name: schema_name.into(),
accept_invalid_certs_first_db,
accept_invalid_certs_second_db,
}
}

Expand Down Expand Up @@ -102,6 +108,15 @@ impl DiffPayload {
pub fn schema_name(&self) -> &str {
&self.schema_name
}
pub fn accept_invalid_certs_first_db(&self) -> bool {
self.accept_invalid_certs_first_db
}
pub fn accept_invalid_certs_second_db(&self) -> bool {
self.accept_invalid_certs_second_db
}
pub fn any_accept_invalid_certs(&self) -> bool {
self.accept_invalid_certs_first_db || self.accept_invalid_certs_second_db
}
}

#[cfg(test)]
Expand All @@ -123,6 +138,8 @@ mod tests {
vec!["table1"],
vec!["table2"],
"schema_name",
false,
false,
);
}
}
2 changes: 1 addition & 1 deletion src/diff/table/table_differ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<TQE: TableSingleSourceQueryExecutor, DTQE: TableDualSourceQueryExecutor>
table_counts_elapsed.as_millis()
);

debug!(
info!(
"{}",
format!("Analyzing table: {}", table_name.clone())
.yellow()
Expand Down
6 changes: 6 additions & 0 deletions src/diff/table/table_differ_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ mod tests {
vec!["table1", "table2"],
EMPTY_STRING_VEC,
"schema_name",
false,
false,
);

let tables = table_differ.get_all_tables(&diff_payload).await.unwrap();
Expand Down Expand Up @@ -80,6 +82,8 @@ mod tests {
vec!["table1", "table2"],
EMPTY_STRING_VEC,
"schema_name",
false,
false,
);

let diff_output = table_differ
Expand Down Expand Up @@ -145,6 +149,8 @@ mod tests {
vec!["table1", "table2"],
EMPTY_STRING_VEC,
"schema_name",
false,
false,
);

let diff_output = table_differ
Expand Down
Loading