Repository: datanymizer/datanymizer (language: rust)

Issue:
Create example for pg_datanymizer
Create example to introduce how to get starting with CLI tool `pg_datanymizer`.
Setup environment with docker-compose, test database dump and example config file.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c40ba396..0bd5a8b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### 🚀 Added
 - Add the uniqueness option for email, ip and random_num [#16](https://github.com/datanymizer/datanymizer/pull/16)
   ([@evgeniy-r](https://github.com/evgeniy-r))
+- Add demo configuration and docker-compose example [#20](https://github.com/datanymizer/datanymizer/pull/20) ([@akirill0v](https://github.com/akirill0v))
 
 ### ⚙️ Changed
 - Automate release process [#17](https://github.com/datanymizer/datanymizer/pull/17) ([@mgrachev](https://github.com/mgrachev))
diff --git a/README.md b/README.md
index ae03e083..c282d81b 100644
--- a/README.md
+++ b/README.md
@@ -107,7 +107,7 @@ And then start to make dump from your database instance:
 ``` shell
 pg_datanymizer -f /tmp/dump.sql -c ./config.yml postgres://postgres:postgres@localhost/test_database
 ```
-
+Uniqueness
 It creates new dump file `/tmp/dump.sql` with native SQL dump for Postgresql database.
 You can import fake data from this dump into new Postgresql database with command:
 
diff --git a/demo/Dockerfile.pg_datanynizer b/demo/Dockerfile.pg_datanynizer
new file mode 100644
index 00000000..f4be2278
--- /dev/null
+++ b/demo/Dockerfile.pg_datanynizer
@@ -0,0 +1,37 @@
+FROM rust:1.48 as cargo-chef
+WORKDIR /usr/src
+
+RUN apt-get update && \
+    apt-get dist-upgrade -y && \
+    apt-get install -y musl-tools && \
+    rustup target add x86_64-unknown-linux-musl
+# We only pay the installation cost once,
+# it will be cached from the second build onwards
+RUN cargo install cargo-chef
+
+FROM cargo-chef as planner
+WORKDIR /usr/src
+COPY . .
+RUN cargo chef prepare --recipe-path recipe.json
+
+FROM cargo-chef as cacher
+WORKDIR /usr/src
+
+RUN cargo install cargo-chef
+COPY --from=planner /usr/src/recipe.json recipe.json
+RUN cargo chef cook --target x86_64-unknown-linux-musl --release --recipe-path recipe.json
+
+FROM cargo-chef as builder
+WORKDIR /usr/src
+
+COPY . .
+# Copy over the cached dependencies
+COPY --from=cacher /usr/src/target target
+COPY --from=cacher $CARGO_HOME $CARGO_HOME
+RUN cargo build --target x86_64-unknown-linux-musl --release
+
+FROM postgres:13-alpine
+WORKDIR /
+COPY --from=builder /usr/src/target/x86_64-unknown-linux-musl/release/pg_datanymizer .
+USER 1000
+ENTRYPOINT ["./pg_datanymizer"]
diff --git a/demo/Makefile b/demo/Makefile
new file mode 100644
index 00000000..6d20fd04
--- /dev/null
+++ b/demo/Makefile
@@ -0,0 +1,39 @@
+example_database_url="https://sp.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip"
+tmpfile="/tmp/dump.zip"
+pause=5
+.SILENT:
+
+build:
+	docker-compose build
+
+up:
+	echo -e "\n==> Start containers"
+	docker-compose up -d
+	echo -e "\n==> Pause ${pause}s after starting Postgresql"
+	sleep ${pause}
+
+download: clean_file
+	echo -e "\n==> Download DVDRENTAL database"
+	curl $(example_database_url) -o "$(tmpfile)" && unzip $(tmpfile) -d /tmp
+
+clean_file:
+	echo -e "\n==> Clean all temporary files"
+	rm -rf $(tmpfile) && rm -rf /tmp/dvdrental.tar
+
+clean_db:
+	echo -e "\n==> Clean database if exists"
+	docker-compose exec pg_database psql -Upostgres -c "drop database if exists dvdrental;"
+
+create_db: up clean_db
+	echo -e "\n==> Create empty database"
+	docker-compose exec pg_database psql -Upostgres -c "create database dvdrental;"
+
+restore_db:
+	echo -e "\n==> Restore database from dump file"
+	docker-compose run --rm -v /tmp/dvdrental.tar:/tmp/dvdrental.tar -e PGPASSWORD=postgres pg_database pg_restore -h pg_database -Upostgres -d dvdrental /tmp/dvdrental.tar
+
+bootstrap: download create_db restore_db clean_file
+
+dump:
+	echo -e "\n==> Create dump"
+	docker-compose run --rm pg_datanymizer postgres://postgres:postgres@pg_database/dvdrental -c /dvdrental.yml > /tmp/fake_dump.sql
diff --git a/demo/README.md b/demo/README.md
new file mode 100644
index 00000000..14dc1d15
--- /dev/null
+++ b/demo/README.md
@@ -0,0 +1,26 @@
+# [Data]nymizer demo
+
+## Get started
+
+1. Install `docker` and `docker-compose` to your development environment.
+2. Go to `demo` directory:
+
+``` shell
+cd demo
+```
+and run:
+
+``` shell
+make bootstrap
+```
+
+It starts docker container with Postgresql, download demo database [PostgreSQL Sample Database](https://www.postgresqltutorial.com/postgresql-sample-database/), unpack it and restore to new `dvdrental` database.
+
+3. For dumping this base, run:
+
+``` shell
+make dump
+```
+It makes new `/tmp/fake_dump.sql` file with fake rows in tables from `dvdrental.yml` configuration.
+
+You can change configuration file and try again.
diff --git a/demo/docker-compose.yml b/demo/docker-compose.yml
new file mode 100644
index 00000000..02e6f4c9
--- /dev/null
+++ b/demo/docker-compose.yml
@@ -0,0 +1,24 @@
+version: '3.2'
+services:
+
+  pg_datanymizer:
+    build:
+      context: ..
+      dockerfile: demo/Dockerfile.pg_datanynizer
+    image: pg_datanymizer
+    volumes:
+      - ./dvdrental.yml:/dvdrental.yml
+
+  pg_database:
+    image: postgres:13-alpine
+    environment:
+      POSTGRES_PASSWORD: "postgres"
+    depends_on:
+      - pg_datanymizer
+    ports:
+      - "5432:5432"
+    volumes:
+      - demo-volume:/demo/:rw
+
+volumes:
+  demo-volume:
diff --git a/demo/dvdrental.yml b/demo/dvdrental.yml
new file mode 100644
index 00000000..57acdad0
--- /dev/null
+++ b/demo/dvdrental.yml
@@ -0,0 +1,14 @@
+filter:
+  only:
+    - public.actor
+tables:
+  - name: actor
+    rules:
+      first_name:
+        first_name: ~
+      last_name:
+        last_name: ~
+      last_update:
+        datetime:
+          from: 1990-01-01T00:00:00+00:00
+          to: 2010-12-31T00:00:00+00:00


Test patch: