Skip to content

Commit 37e082f

Browse files
committed
Add support for "--exclude-table-data=pattern" parameter
1 parent 601051c commit 37e082f

File tree

4 files changed

+234
-9
lines changed

4 files changed

+234
-9
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 17.0 (Debian 17.0-1.pgdg120+1)
6+
-- Dumped by pg_dump version 17.0 (Debian 17.0-1.pgdg120+1)
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET transaction_timeout = 0;
12+
SET client_encoding = 'UTF8';
13+
SET standard_conforming_strings = on;
14+
SELECT pg_catalog.set_config('search_path', '', false);
15+
SET check_function_bodies = false;
16+
SET xmloption = content;
17+
SET client_min_messages = warning;
18+
SET row_security = off;
19+
20+
--
21+
-- Name: other_schema; Type: SCHEMA; Schema: -; Owner: test-user
22+
--
23+
24+
CREATE SCHEMA other_schema;
25+
26+
27+
ALTER SCHEMA other_schema OWNER TO "test-user";
28+
29+
SET default_tablespace = '';
30+
31+
SET default_table_access_method = heap;
32+
33+
--
34+
-- Name: persons; Type: TABLE; Schema: other_schema; Owner: test-user
35+
--
36+
37+
CREATE TABLE other_schema.persons (
38+
id integer NOT NULL,
39+
name text NOT NULL
40+
);
41+
42+
43+
ALTER TABLE other_schema.persons OWNER TO "test-user";
44+
45+
--
46+
-- Name: employees; Type: TABLE; Schema: public; Owner: test-user
47+
--
48+
49+
CREATE TABLE public.employees (
50+
id integer NOT NULL,
51+
first_name character varying(50),
52+
last_name character varying(50),
53+
email character varying(100),
54+
hire_date date,
55+
salary numeric(10,2)
56+
);
57+
58+
59+
ALTER TABLE public.employees OWNER TO "test-user";
60+
61+
--
62+
-- Data for Name: persons; Type: TABLE DATA; Schema: other_schema; Owner: test-user
63+
--
64+
65+
COPY other_schema.persons (id, name) FROM stdin;
66+
1 John
67+
2 Jane
68+
3 Emily
69+
\.
70+
71+
72+
--
73+
-- Name: persons persons_pkey; Type: CONSTRAINT; Schema: other_schema; Owner: test-user
74+
--
75+
76+
ALTER TABLE ONLY other_schema.persons
77+
ADD CONSTRAINT persons_pkey PRIMARY KEY (id);
78+
79+
80+
--
81+
-- Name: employees employees_pkey; Type: CONSTRAINT; Schema: public; Owner: test-user
82+
--
83+
84+
ALTER TABLE ONLY public.employees
85+
ADD CONSTRAINT employees_pkey PRIMARY KEY (id);
86+
87+
88+
--
89+
-- PostgreSQL database dump complete
90+
--
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 17.0 (Debian 17.0-1.pgdg120+1)
6+
-- Dumped by pg_dump version 17.0 (Debian 17.0-1.pgdg120+1)
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET transaction_timeout = 0;
12+
SET client_encoding = 'UTF8';
13+
SET standard_conforming_strings = on;
14+
SELECT pg_catalog.set_config('search_path', '', false);
15+
SET check_function_bodies = false;
16+
SET xmloption = content;
17+
SET client_min_messages = warning;
18+
SET row_security = off;
19+
20+
--
21+
-- Name: other_schema; Type: SCHEMA; Schema: -; Owner: test-user
22+
--
23+
24+
CREATE SCHEMA other_schema;
25+
26+
27+
ALTER SCHEMA other_schema OWNER TO "test-user";
28+
29+
SET default_tablespace = '';
30+
31+
SET default_table_access_method = heap;
32+
33+
--
34+
-- Name: persons; Type: TABLE; Schema: other_schema; Owner: test-user
35+
--
36+
37+
CREATE TABLE other_schema.persons (
38+
id integer NOT NULL,
39+
name text NOT NULL
40+
);
41+
42+
43+
ALTER TABLE other_schema.persons OWNER TO "test-user";
44+
45+
--
46+
-- Name: employees; Type: TABLE; Schema: public; Owner: test-user
47+
--
48+
49+
CREATE TABLE public.employees (
50+
id integer NOT NULL,
51+
first_name character varying(50),
52+
last_name character varying(50),
53+
email character varying(100),
54+
hire_date date,
55+
salary numeric(10,2)
56+
);
57+
58+
59+
ALTER TABLE public.employees OWNER TO "test-user";
60+
61+
--
62+
-- Data for Name: employees; Type: TABLE DATA; Schema: public; Owner: test-user
63+
--
64+
65+
INSERT INTO public.employees VALUES (1, 'John', 'Doe', 'john.doe@example.com', '2022-01-15', 60000.00);
66+
INSERT INTO public.employees VALUES (2, 'Jane', 'Smith', 'jane.smith@example.com', '2023-03-22', 75000.00);
67+
INSERT INTO public.employees VALUES (3, 'Emily', 'Johnson', 'emily.johnson@example.com', '2021-05-10', 50000.00);
68+
INSERT INTO public.employees VALUES (4, 'Michael', 'Brown', 'michael.brown@example.com', '2020-08-30', 80000.00);
69+
INSERT INTO public.employees VALUES (5, 'Sarah', 'Davis', 'sarah.davis@example.com', '2024-06-01', 70000.00);
70+
71+
72+
--
73+
-- Name: persons persons_pkey; Type: CONSTRAINT; Schema: other_schema; Owner: test-user
74+
--
75+
76+
ALTER TABLE ONLY other_schema.persons
77+
ADD CONSTRAINT persons_pkey PRIMARY KEY (id);
78+
79+
80+
--
81+
-- Name: employees employees_pkey; Type: CONSTRAINT; Schema: public; Owner: test-user
82+
--
83+
84+
ALTER TABLE ONLY public.employees
85+
ADD CONSTRAINT employees_pkey PRIMARY KEY (id);
86+
87+
88+
--
89+
-- PostgreSQL database dump complete
90+
--

src/main/java/de/cronn/postgres/snapshot/util/PostgresDump.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ public static String dumpToString(String jdbcUrl, String username, String passwo
3737

3838
public static String dumpToString(String jdbcUrl, String username, String password, List<Schema> schemas,
3939
PostgresDumpOption... options) {
40+
return dumpToString(jdbcUrl, username, password, schemas, List.of(), options);
41+
}
42+
43+
public static String dumpToString(String jdbcUrl, String username, String password, List<Schema> schemas,
44+
List<String> excludeTableDataPatterns, PostgresDumpOption... options) {
4045
try (StringWriter writer = new StringWriter()) {
41-
dump(writer, jdbcUrl, username, password, schemas, options);
46+
dump(writer, jdbcUrl, username, password, schemas, excludeTableDataPatterns, options);
4247
return writer.toString();
4348
} catch (IOException e) {
4449
throw new RuntimeException(e);
@@ -52,8 +57,14 @@ public static void dumpToFile(Path path, String jdbcUrl, String username, String
5257

5358
public static void dumpToFile(Path path, String jdbcUrl, String username, String password,
5459
PostgresDumpFormat format, List<Schema> schemas, PostgresDumpOption... options) {
60+
dumpToFile(path, jdbcUrl, username, password, format, schemas, List.of(), options);
61+
}
62+
63+
public static void dumpToFile(Path path, String jdbcUrl, String username, String password,
64+
PostgresDumpFormat format, List<Schema> schemas, List<String> excludeTableDataPatterns,
65+
PostgresDumpOption... options) {
5566
try (OutputStream outputStream = Files.newOutputStream(path)) {
56-
dump(outputStream, jdbcUrl, username, password, format, schemas, options);
67+
dump(outputStream, jdbcUrl, username, password, format, schemas, excludeTableDataPatterns, options);
5768
} catch (IOException e) {
5869
throw new RuntimeException(e);
5970
}
@@ -66,7 +77,12 @@ public static void dump(Writer writer, String jdbcUrl, String username, String p
6677

6778
public static void dump(Writer writer, String jdbcUrl, String username, String password, List<Schema> schemas,
6879
PostgresDumpOption... options) {
69-
dump(jdbcUrl, username, password, PostgresDumpFormat.PLAIN_TEXT, schemas, inputStream -> {
80+
dump(writer, jdbcUrl, username, password, schemas, List.of(), options);
81+
}
82+
83+
public static void dump(Writer writer, String jdbcUrl, String username, String password, List<Schema> schemas,
84+
List<String> excludeTableDataPatterns, PostgresDumpOption... options) {
85+
dump(jdbcUrl, username, password, PostgresDumpFormat.PLAIN_TEXT, schemas, excludeTableDataPatterns, inputStream -> {
7086
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, ENCODING)) {
7187
return inputStreamReader.transferTo(writer);
7288
}
@@ -80,14 +96,21 @@ public static void dump(OutputStream outputStream, String jdbcUrl, String userna
8096

8197
public static void dump(OutputStream outputStream, String jdbcUrl, String username, String password,
8298
PostgresDumpFormat format, List<Schema> schemas, PostgresDumpOption... options) {
83-
dump(jdbcUrl, username, password, format, schemas, inputStream -> inputStream.transferTo(outputStream), options);
99+
dump(outputStream, jdbcUrl, username, password, format, schemas, List.of(), options);
84100
}
85101

86-
public static void dump(String jdbcUrl, String username, String password, PostgresDumpFormat format,
87-
List<Schema> schemas, ThrowingFunction<InputStream, Long> inputStreamProcessor,
102+
public static void dump(OutputStream outputStream, String jdbcUrl, String username, String password,
103+
PostgresDumpFormat format, List<Schema> schemas, List<String> excludeTableDataPatterns,
88104
PostgresDumpOption... options) {
105+
dump(jdbcUrl, username, password, format, schemas, excludeTableDataPatterns,
106+
inputStream -> inputStream.transferTo(outputStream), options);
107+
}
108+
109+
private static void dump(String jdbcUrl, String username, String password, PostgresDumpFormat format,
110+
List<Schema> schemas, List<String> excludeTableDataPatterns,
111+
ThrowingFunction<InputStream, Long> inputStreamProcessor, PostgresDumpOption... options) {
89112
try (GenericContainer<?> container =
90-
createPgDumpInContainer(jdbcUrl, username, password, format, schemas, options)) {
113+
createPgDumpInContainer(jdbcUrl, username, password, format, schemas, excludeTableDataPatterns, options)) {
91114
container.start();
92115

93116
container.copyFileFromContainer(CONTAINER_DUMP_FILE, inputStreamProcessor);
@@ -96,9 +119,10 @@ public static void dump(String jdbcUrl, String username, String password, Postgr
96119

97120
private static GenericContainer<?> createPgDumpInContainer(String jdbcUrl, String username, String password,
98121
PostgresDumpFormat format, List<Schema> schemas,
122+
List<String> excludeTableDataPatterns,
99123
PostgresDumpOption... options) {
100124
ConnectionInformation connectionInformation = PostgresUtils.parseConnectionInformation(jdbcUrl, username, password);
101-
String[] command = createPgDumpCommand(connectionInformation, format, schemas, options);
125+
String[] command = createPgDumpCommand(connectionInformation, format, schemas, excludeTableDataPatterns, options);
102126

103127
log.debug("Executing {}", String.join(" ", command));
104128

@@ -113,7 +137,8 @@ private static GenericContainer<?> createPgDumpInContainer(String jdbcUrl, Strin
113137
}
114138

115139
private static String[] createPgDumpCommand(ConnectionInformation connectionInformation, PostgresDumpFormat format,
116-
List<Schema> schemas, PostgresDumpOption... options) {
140+
List<Schema> schemas, List<String> excludeTableDataPatterns,
141+
PostgresDumpOption... options) {
117142
List<String> commandArgs = new ArrayList<>(List.of(
118143
"pg_dump",
119144
"--host=" + connectionInformation.host(),
@@ -131,6 +156,10 @@ private static String[] createPgDumpCommand(ConnectionInformation connectionInfo
131156
commandArgs.addAll(schema.getCommandArguments());
132157
}
133158

159+
for (String excludeTableDataPattern : excludeTableDataPatterns) {
160+
commandArgs.add("--exclude-table-data=" + excludeTableDataPattern);
161+
}
162+
134163
for (PostgresDumpOption option : options) {
135164
commandArgs.add(option.getCommandArgument());
136165
}

src/test/java/de/cronn/postgres/snapshot/util/PostgresDumpTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ void testDumpWithIllegalParameters() {
170170
.withMessageStartingWith("Container startup failed for image postgres:");
171171
}
172172

173+
@Test
174+
void testDumpExcludingTableData() {
175+
String dump = PostgresDump.dumpToString(jdbcUrl, USERNAME, PASSWORD,
176+
List.of(), List.of("emplo*"));
177+
compareActualWithValidationFile(dump);
178+
}
179+
180+
@Test
181+
void testDumpToFileExcludingTableData(@TempDir Path tempDir) throws Exception {
182+
Path dumpFile = tempDir.resolve("dump.sql");
183+
PostgresDump.dumpToFile(dumpFile, jdbcUrl, USERNAME, PASSWORD, PostgresDumpFormat.PLAIN_TEXT,
184+
List.of(), List.of("other_schema.persons"), PostgresDumpOption.INSERTS);
185+
String fileContent = Files.readString(dumpFile, PostgresDump.ENCODING);
186+
compareActualWithValidationFile(fileContent);
187+
}
188+
173189
@Test
174190
void testConnectViaHostName() throws Exception {
175191
String jdbcUrl = postgresContainer.getJdbcUrl();

0 commit comments

Comments
 (0)