Skip to content

Commit 02db3b1

Browse files
committed
Omit Postgres dump COPY statements if table is configured to skip rows
Leave out any `COPY` statements for table if that table is listed in Configuration.skip_rows_for_table.
1 parent e9ef6c0 commit 02db3b1

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

database_sanitizer/dump/postgres.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def sanitize(url, config):
5454
sanitize_value_line = None
5555
current_table = None
5656
current_table_columns = None
57+
skip_table = False
5758

5859
for line in codecs.getreader("utf-8")(process.stdout):
5960
# Eat the trailing new line.
@@ -65,7 +66,12 @@ def sanitize(url, config):
6566
if line == "\\.":
6667
current_table = None
6768
current_table_columns = None
68-
yield "\\."
69+
if not skip_table:
70+
yield "\\."
71+
skip_table = False
72+
continue
73+
74+
if skip_table:
6975
continue
7076

7177
if not sanitize_value_line:
@@ -84,6 +90,12 @@ def sanitize(url, config):
8490
current_table = copy_line_match.group("table")
8591
current_table_columns = parse_column_names(copy_line_match.group("columns"))
8692

93+
# Skip `COPY` statement if table rows are configured
94+
# to be skipped.
95+
if config and current_table in config.skip_rows_for_tables:
96+
skip_table = True
97+
continue
98+
8799
sanitize_value_line = get_value_line_sanitizer(
88100
config, current_table, current_table_columns)
89101

database_sanitizer/tests/test_dump_postgres.py

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
2020
COMMENT ON SCHEMA "public" IS 'standard public schema';
2121
22+
CREATE TABLE "public"."test" (
23+
"id" integer NOT NULL,
24+
"created_at" timestamp with time zone NOT NULL,
25+
"notes" character varying(255) NOT NULL
26+
);
27+
2228
COPY "public"."test" ("id", "created_at", "notes") FROM stdin;
2329
1\t2018-01-01 00:00:00\tTest data 1
2430
2\t2018-01-02 00:00:00\tTest data 2
@@ -65,6 +71,31 @@ def test_sanitize():
6571
assert "2\t2018-01-02 00:00:00\tSanitized" in dump_output_lines
6672

6773

74+
def test_skip_table_rows():
75+
url = urlparse.urlparse("postgres://localhost/test")
76+
config = Configuration()
77+
config.skip_rows_for_tables.append('test')
78+
79+
with mock.patch("subprocess.Popen",
80+
side_effect=create_mock_popen(MOCK_PG_DUMP_OUTPUT)):
81+
output = list(sanitize(url, config))
82+
83+
assert output == [
84+
'--- Fake PostgreSQL database dump',
85+
'',
86+
'COMMENT ON SCHEMA "public" IS \'standard public schema\';',
87+
'',
88+
'CREATE TABLE "public"."test" (',
89+
'"id" integer NOT NULL,',
90+
'"created_at" timestamp with time zone NOT NULL,',
91+
'"notes" character varying(255) NOT NULL',
92+
');',
93+
'',
94+
'',
95+
'--- Final line after `COPY` statement'
96+
]
97+
98+
6899
def test_sanitizer_invalid_input():
69100
url = urlparse.urlparse("postgres://localhost/test")
70101

0 commit comments

Comments
 (0)