Skip to content

Commit

Permalink
Additional pg_dump args (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-r committed Jul 23, 2021
1 parent 98d1f1f commit d65c0b3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### 🚀 Added
- Additional arguments for pg_dump [#78](https://github.com/datanymizer/datanymizer/pull/78)
([@evgeniy-r](https://github.com/evgeniy-r))
- `base64_token` and `base64url_token` transformers. [#77](https://github.com/datanymizer/datanymizer/pull/77)
([@evgeniy-r](https://github.com/evgeniy-r))
- Key-value store that allows to share information between template transformers
Expand Down
5 changes: 3 additions & 2 deletions cli/pg_datanymizer/src/app.rs
Expand Up @@ -42,6 +42,7 @@ impl App {
engine,
self.options.pg_dump_location.clone(),
self.options.file.clone(),
self.options.pg_dump_args.clone(),
)
}

Expand Down Expand Up @@ -78,10 +79,10 @@ impl App {
} else {
let mut builder = TlsConnector::builder();

if self.options.accept_invalid_hostnames.unwrap_or(false) {
if self.options.accept_invalid_hostnames {
builder.danger_accept_invalid_hostnames(true);
}
if self.options.accept_invalid_certs.unwrap_or(false) {
if self.options.accept_invalid_certs {
builder.danger_accept_invalid_certs(true);
}

Expand Down
85 changes: 58 additions & 27 deletions cli/pg_datanymizer/src/options.rs
Expand Up @@ -9,48 +9,46 @@ pub struct Options {
database: String,

#[structopt(
short = "c",
long = "config",
short,
long,
help = "Path to config file",
default_value = "./config.yml"
)]
pub config: String,

#[structopt(
short = "f",
long = "file",
short,
long,
name = "FILE",
help = "Path to dump file, example: /tmp/dump.sql"
)]
pub file: Option<String>,

#[structopt(
short = "d",
short,
long = "dbname",
help = "database to dump",
default_value = "postgres"
)]
pub db_name: String,

#[structopt(
short = "h",
long = "host",
short,
long,
help = "Database server host or socket directory",
default_value = "localhost"
)]
pub host: String,
#[structopt(
short = "p",
long = "port",
help = "Database server port number [default: 5432]"
)]

#[structopt(short, long, help = "Database server port number [default: 5432]")]
pub port: Option<u16>,
#[structopt(
short = "U",
long = "username",
help = "Connect as specified database user"
)]

#[structopt(short = "U", long, help = "Connect as specified database user")]
pub username: Option<String>,
#[structopt(short = "W", long = "password", help = "User password")]

#[structopt(short = "W", long, help = "User password")]
pub password: Option<String>,

#[structopt(
long = "pg_dump",
help = "pg_dump file location",
Expand All @@ -60,14 +58,21 @@ pub struct Options {

#[structopt(
long = "accept_invalid_hostnames",
help = "Accept or not invalid hostnames when using SSL [default: false]"
help = "Accept or not invalid hostnames when using SSL"
)]
pub accept_invalid_hostnames: Option<bool>,
pub accept_invalid_hostnames: bool,

#[structopt(
long = "accept_invalid_certs",
help = "Accept or not invalid certificates (e.g., self-signed) when using SSL [default: false]"
help = "Accept or not invalid certificates (e.g., self-signed) when using SSL"
)]
pub accept_invalid_certs: bool,

#[structopt(
name = "PG_DUMP_ARGS",
help = "The remaining arguments are passed directly to `pg_dump` calls. You should add `--` before <DBNAME> in such cases"
)]
pub accept_invalid_certs: Option<bool>,
pub pg_dump_args: Vec<String>,
}

impl Options {
Expand Down Expand Up @@ -106,7 +111,7 @@ impl Options {

#[cfg(test)]
mod tests {
use super::Options;
use super::*;

#[test]
fn parse_empty_config() {
Expand All @@ -120,8 +125,9 @@ mod tests {
username: None,
password: None,
pg_dump_location: "pg_dump".to_string(),
accept_invalid_hostnames: Some(false),
accept_invalid_certs: Some(false),
accept_invalid_hostnames: false,
accept_invalid_certs: false,
pg_dump_args: vec![],
};

let expected = "postgres://hostname/test".to_string();
Expand All @@ -140,8 +146,9 @@ mod tests {
username: None,
password: None,
pg_dump_location: "pg_dump".to_string(),
accept_invalid_hostnames: Some(false),
accept_invalid_certs: Some(false),
accept_invalid_hostnames: false,
accept_invalid_certs: false,
pg_dump_args: vec![],
};

let cfg2 = Options {
Expand All @@ -166,4 +173,28 @@ mod tests {
assert_eq!(cfg3.database_url().unwrap().to_string(), expected3);
assert_eq!(cfg4.database_url().unwrap().to_string(), expected4);
}

#[test]
fn parse_args() {
let cmd = vec![
"pg_datanymizer",
"-c",
"some_config.yml",
"-f",
"some_file.sql",
"--",
"postgres://user@hostname/test",
"--no-owner",
"--no-acl",
];
let options = Options::from_iter(cmd);

assert_eq!(
options.database_url().unwrap().as_str(),
"postgres://user@hostname/test"
);
assert_eq!(options.config, "some_config.yml");
assert_eq!(options.file, Some("some_file.sql".to_string()));
assert_eq!(options.pg_dump_args, vec!["--no-owner", "--no-acl"]);
}
}
11 changes: 10 additions & 1 deletion datanymizer_dumper/src/postgres/dumper.rs
Expand Up @@ -18,23 +18,31 @@ pub struct PgDumper {
engine: Engine,
dump_writer: DumpWriter,
pg_dump_location: String,
pg_dump_args: Vec<String>,
progress_bar: ProgressBar,
}

impl PgDumper {
pub fn new(engine: Engine, pg_dump_location: String, target: Option<String>) -> Result<Self> {
pub fn new(
engine: Engine,
pg_dump_location: String,
target: Option<String>,
pg_dump_args: Vec<String>,
) -> Result<Self> {
let dump_writer = DumpWriter::new(target)?;
let pb: ProgressBar = if dump_writer.can_log_to_stdout() {
ProgressBar::new(0)
} else {
ProgressBar::hidden()
};

Ok(Self {
engine,
dump_writer,
pg_dump_location,
schema_inspector: PgSchemaInspector {},
progress_bar: pb,
pg_dump_args,
})
}

Expand All @@ -45,6 +53,7 @@ impl PgDumper {
let db_url = self.engine.settings.source.get_database_url();

let dump_output = Command::new(program)
.args(&self.pg_dump_args)
.args(&args)
.args(&table_args)
.arg(&db_url)
Expand Down
14 changes: 7 additions & 7 deletions docs/pg_datanymizer.md
Expand Up @@ -5,7 +5,7 @@
#### Usage:

```
pg_datanymizer [OPTIONS] <DBNAME>
pg_datanymizer [OPTIONS] <DBNAME> [PG_DUMP_ARGS]...
```

#### FLAGS
Expand All @@ -22,8 +22,8 @@ pg_datanymizer [OPTIONS] <DBNAME>
| `-f`, `--file` `<FILE>` | Path to the dump output file, example: `/tmp/dump.sql`
| `-c`, `--config` `<config>` | Path to the config file. Default: `./config.yml`
| `--pg_dump` `<pg-dump-location>` | Postgres `pg_dump` utility program file location. Default: just `pg_dump`
| `--accept_invalid_certs` `<accept-invalid-certs>` | Accept or not invalid certificates (e.g., self-signed) when using SSL. Default: `false`
| `--accept_invalid_hostnames` `<accept-invalid-hostnames>` | Accept or not invalid hostnames when using SSL. Default: `false`
| `--accept_invalid_certs` | Accept invalid certificates (e.g., self-signed) when using SSL
| `--accept_invalid_hostnames` | Accept invalid hostnames when using SSL
| When `<DBNAME>` is just a database name (not a full url):
| `-h`, `--host` `<host>` | Database server host or a socket directory. Default: `localhost`
| `-W`, `--password` `<password>` | User password
Expand All @@ -32,7 +32,7 @@ pg_datanymizer [OPTIONS] <DBNAME>

#### ARGS

| Name | Description
|--- |---
| `<DBNAME>` | Postgres database URL, e.g. `postgres://postgres:password@localhost:5432/database_name?sslmode=disable` (you can omit some parts), or just a database name, e.g. `my_db`

| Name | Description
|--- |---
| `<DBNAME>` | Postgres database URL, e.g. `postgres://postgres:password@localhost:5432/database_name?sslmode=disable` (you can omit some parts), or just a database name, e.g. `my_db`
| `<PG_DUMP_ARGS>` | The remaining arguments are passed directly to `pg_dump` calls. You should add `--` before `<DBNAME>` in such cases

0 comments on commit d65c0b3

Please sign in to comment.