Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

Commit e74141e

Browse files
committed
Add argument to specify schema name
1 parent 225de1f commit e74141e

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

pgdatadiff/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""
22
Usage:
3-
pgdatadiff --firstdb=<firstconnectionstring> --seconddb=<secondconnectionstring> [--only-data|--only-sequences] [--count-only] [--chunk-size=<size>] [--exclude-tables=<table1,table2>]
3+
pgdatadiff --firstdb=<firstconnectionstring> --seconddb=<secondconnectionstring> [--schema=<schema>] [--only-data|--only-sequences] [--count-only] [--chunk-size=<size>] [--exclude-tables=<table1,table2>]
44
pgdatadiff --version
55
66
Options:
77
-h --help Show this screen.
88
--version Show version.
99
--firstdb=postgres://postgres:password@localhost/firstdb The connection string of the first DB
1010
--seconddb=postgres://postgres:password@localhost/seconddb The connection string of the second DB
11+
--schema=schemaName The schema of tables in comparison
1112
--only-data Only compare data, exclude sequences
1213
--only-sequences Only compare seqences, exclude data
1314
--exclude-tables="" Exclude tables from data comparison Must be a comma separated string [default: empty string]
@@ -27,6 +28,7 @@ def main():
2728
__doc__, version=pkg_resources.require("pgdatadiff")[0].version)
2829
first_db_connection_string=arguments['--firstdb']
2930
second_db_connection_string=arguments['--seconddb']
31+
arguments['--schema'] = 'public' if not arguments['--schema'] else True
3032
if not first_db_connection_string.startswith("postgres://") or \
3133
not second_db_connection_string.startswith("postgres://"):
3234
print(red("Only Postgres DBs are supported"))
@@ -35,7 +37,8 @@ def main():
3537
differ = DBDiff(first_db_connection_string, second_db_connection_string,
3638
chunk_size=arguments['--chunk-size'],
3739
count_only=arguments['--count-only'],
38-
exclude_tables=arguments['--exclude-tables'])
40+
exclude_tables=arguments['--exclude-tables'],
41+
schema=arguments['--schema'])
3942

4043
if not arguments['--only-sequences']:
4144
if differ.diff_all_table_data():

pgdatadiff/pgdatadiff.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def make_session(connection_string):
1919

2020
class DBDiff(object):
2121

22-
def __init__(self, firstdb, seconddb, chunk_size=10000, count_only=False, exclude_tables=""):
22+
def __init__(self, firstdb, seconddb, schema, chunk_size=10000, count_only=False, exclude_tables=""):
2323
firstsession, firstengine = make_session(firstdb)
24-
secondsession, secondengine = make_session(seconddb)
24+
secondsession, secondengine = make_session(seconddb,)
2525
self.firstsession = firstsession
2626
self.firstengine = firstengine
2727
self.secondsession = secondsession
@@ -33,6 +33,7 @@ def __init__(self, firstdb, seconddb, chunk_size=10000, count_only=False, exclud
3333
self.chunk_size = int(chunk_size)
3434
self.count_only = count_only
3535
self.exclude_tables = exclude_tables.split(',')
36+
self.schema = schema
3637

3738
def diff_table_data(self, tablename):
3839
try:
@@ -62,7 +63,7 @@ def diff_table_data(self, tablename):
6263
SELECT md5(array_agg(md5((t.*)::varchar))::varchar)
6364
FROM (
6465
SELECT *
65-
FROM {tablename}
66+
FROM {self.schema}.{tablename}
6667
ORDER BY {pk} limit :row_limit offset :row_offset
6768
) AS t;
6869
"""
@@ -91,7 +92,7 @@ def get_all_sequences(self):
9192
self.firstsession.execute(GET_SEQUENCES_SQL).fetchall()]
9293

9394
def diff_sequence(self, seq_name):
94-
GET_SEQUENCES_VALUE_SQL = f"SELECT last_value FROM {seq_name};"
95+
GET_SEQUENCES_VALUE_SQL = f"SELECT last_value FROM {self.schema}.{seq_name};"
9596

9697
try:
9798
firstvalue = \
@@ -141,7 +142,7 @@ def diff_all_table_data(self):
141142
with warnings.catch_warnings():
142143
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
143144
tables = sorted(
144-
self.firstinspector.get_table_names(schema="public"))
145+
self.firstinspector.get_table_names(schema=self.schema))
145146
for table in tables:
146147
if table in self.exclude_tables:
147148
print(bold(yellow(f"Ignoring table {table}")))

0 commit comments

Comments
 (0)