Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 3209a63

Browse files
committed
updated
1 parent 3935d9d commit 3209a63

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/db.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ use tokio::sync::Semaphore;
1515
#[derive(Clone, Debug)]
1616
pub struct DB {
1717
pub client: Client,
18-
pub db: String
18+
pub db: String,
19+
pub renamedb: Option<String>
1920
}
2021

2122
type BoxResult<T> = std::result::Result<T, Box<dyn error::Error + Send + Sync>>;
2223

2324
impl DB {
24-
pub async fn init(url: &str, db: &str) -> BoxResult<Self> {
25+
pub async fn init(url: &str, db: &str, renamedb: Option<&str>) -> BoxResult<Self> {
2526
let mut client_options = ClientOptions::parse(url).await?;
2627
client_options.app_name = Some("mongodb-stream-rs".to_string());
2728
client_options.read_concern = Some(ReadConcern::local());
2829
Ok(Self {
2930
client: Client::with_options(client_options)?,
30-
db: db.to_owned()
31+
db: db.to_owned(),
32+
renamedb: renamedb.map(|s| s.into())
3133
})
3234
}
3335

@@ -113,7 +115,7 @@ impl DB {
113115
};
114116

115117
// Set counter to total
116-
counter.total(total);
118+
counter.set_total(total);
117119

118120
let cursor = collection_handle.find(query, find_options).await?;
119121

@@ -122,7 +124,10 @@ impl DB {
122124

123125
pub async fn insert_cursor(&mut self, collection: &str, mut cursor: Cursor, mut counter: Counter) -> BoxResult<()> {
124126
// Get handle on collection
125-
let coll = self.client.database(&self.db).collection(collection);
127+
let coll = match &self.renamedb {
128+
Some(db) => self.client.database(&db).collection(collection),
129+
None => self.client.database(&self.db).collection(collection)
130+
};
126131

127132
log::info!("{}.{}: Inserting {} docs", self.db, collection, counter.total);
128133

@@ -155,7 +160,10 @@ impl DB {
155160

156161
pub async fn validate_docs(&mut self, collection: &str, mut cursor: Cursor, mut counter: Counter) -> BoxResult<()> {
157162
// Get handle on collection
158-
let coll = self.client.database(&self.db).collection(collection);
163+
let coll = match &self.renamedb {
164+
Some(db) => self.client.database(&db).collection(collection),
165+
None => self.client.database(&self.db).collection(collection)
166+
};
159167

160168
log::info!("{}.{}: Validating that {} docs in destination exist in source", self.db, collection, counter.total);
161169

@@ -386,14 +394,18 @@ impl Counter {
386394
// self.count = count as f64;
387395
// }
388396

389-
pub fn total(&mut self, total: f64) {
397+
pub fn set_total(&mut self, total: f64) {
390398
self.total = total;
391399
}
392400

393401
pub fn count(&self) -> f64 {
394402
self.count
395403
}
396404

405+
pub fn total(&self) -> f64 {
406+
self.total
407+
}
408+
397409
pub fn incr(&mut self, db: &str, collection: &str, count: f64, start: i64) {
398410
self.count += count;
399411
let percent = self.count / self.total * 100.0;

src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ async fn main() -> BoxResult<()> {
117117
.help("Enable extra verbosity")
118118
.takes_value(false)
119119
)
120+
.arg(
121+
Arg::with_name("rename")
122+
.long("rename")
123+
.required(false)
124+
.value_name("MONGODB_RENAME")
125+
.env("MONGODB_RENAME")
126+
.help("Rename DB at destination")
127+
.takes_value(true)
128+
)
120129
.get_matches();
121130

122131
// Initialize log Builder
@@ -139,15 +148,16 @@ async fn main() -> BoxResult<()> {
139148
let source = &opts.value_of("source_uri").unwrap();
140149
let destination= &opts.value_of("destination_uri").unwrap();
141150
let db = &opts.value_of("db").unwrap();
151+
let renamedb = &opts.value_of("renamedb");
142152

143153
println!(
144154
"Starting mongodb-stream-rs:{}",
145155
crate_version!(),
146156
);
147157

148158
// Create connections to source and destination db's
149-
let source_db = DB::init(&source, &db).await?;
150-
let destination_db = DB::init(&destination, &db).await?;
159+
let source_db = DB::init(&source, &db, None).await?;
160+
let destination_db = DB::init(&destination, &db, *renamedb).await?;
151161

152162

153163
let collections = match &opts.is_present("collection") {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy