Skip to content

Commit ba1c44b

Browse files
committed
Entry point and documentation
1 parent 1029e46 commit ba1c44b

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1-
# TODO
1+
# pg-dump-filtered - Postgres partial dump
22

3-
# Installation
3+
This module was designed to do partial dump (filtered dump) on a set of tables taking in account
4+
all their foreign_keys, so table they are referencing.
5+
If you have a huge database and you want to extract a small set of data (with there dependencies in other tables) to work on it and them re-import the datas
6+
this script is made for you.
7+
8+
Filtering is made using SQL where statement on a SELECT statement with all tables relations handeled as INNER or LEFT JOIN (depending on the nullability of the foreign_key).
9+
10+
The generated dump is a set of COPY statements with raw values, so that it can handle all type of values (dates, binary, postgis points ...).
11+
12+
## Installation
413
```bash
514
pip install -r requirements.txt
15+
python setup.py install
16+
```
17+
18+
## How to use it
19+
20+
### Command line interface
21+
22+
```bash
23+
Usage:
24+
pg-dump-filtered [options] <db-uri> <table-list>
25+
26+
Arguments:
27+
db-uri URI of the postgres database, for instance : postgresql://pg_dump_test:pg_dump_test@localhost:5432/pg_dump_test
28+
table-list List of the table that needs to be exported, separated by commas (related tables will automatically be exported).
29+
Eg : 'table1,table2,table3'
30+
31+
Options:
32+
-h --help Show this screen.
33+
--filters=<SQL> SQL filters. Eg: mytable.mycol = 'value' AND myothertable.toto LIKE 'titi'
34+
--ignored-constraints=<str> List of constraints to be ignored. Eg : "myconstraint,myotherconstraint"
35+
--output=<str> Dump file path. [default: dump.sql]
36+
```
37+
38+
```bash
39+
pg-dump-filtered "postgres://user:pwd@host/db" "tableA,tableB" --debug --filters="mytable.id=85 AND ....." --ignored-constraints="a_circular_constraint_name"
40+
```
41+
42+
### Python Interface
43+
44+
For [Open Path View](https://openpathview.fr) whe needed to export small set a data depending on their geolocalisation and list some row of the exported datas (files UUID as files where saved in the database).
45+
46+
```python
47+
TODO
648
```

pg_dump_filtered/pg_dump_filtered.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import logging
2020
import psycopg2
2121
from urllib.parse import urlparse
22-
from typing import List
22+
from typing import List, Tuple
2323

2424
from pg_dump_filtered.helpers import SchemaUtils, RequestBuilder, DumpBuilder
2525

@@ -145,11 +145,12 @@ def request_builder(self, request_builder):
145145
"""
146146
self._request_builder = request_builder
147147

148-
def dump(self, tables_to_export: List[str]):
148+
def generate_tables_to_request_and_join(self, tables_to_export: List[str]) -> Tuple[List[str], str]:
149149
"""
150-
Dump some tables and all related datas. Dump to the output file directly.
150+
Generate a tuple with tables to request and join statement.
151151
152-
:param table_to_export: List of tables names that needs to be exported and all their related tables.
152+
:param tables_to_export: Table names to be exported.
153+
:return: A tuple (tables_to_requests, join_statement)
153154
"""
154155
tables_to_request = self.schema_utils.list_all_related_tables(table_names=tables_to_export)
155156

@@ -161,6 +162,19 @@ def dump(self, tables_to_export: List[str]):
161162
join_req = self.request_builder.generate_join_statments(table_names=tables_to_request, exclude_from_statment=[from_table_name])
162163
self.logger.debug("Join request : %s", join_req)
163164

165+
return (tables_to_request, join_req)
166+
167+
def dump(self, tables_to_export: List[str]):
168+
"""
169+
Dump some tables and all related datas. Dump to the output file directly.
170+
171+
:param table_to_export: List of tables names that needs to be exported and all their related tables.
172+
"""
173+
self.logger.debug("Dump generation from %s")
174+
tables_to_request, join_req = self.generate_tables_to_request_and_join(tables_to_export=tables_to_export)
175+
176+
from_table_name = tables_to_export[0] # Table that will be used in the FROM statment
177+
164178
# generating select statements
165179
selects = self.request_builder.generate_all_select_statements(
166180
table_to_be_exported=tables_to_request,

0 commit comments

Comments
 (0)