From 24a8928f8dd8bb13e12eeabbdda74737702e7f57 Mon Sep 17 00:00:00 2001 From: unalsurmeli Date: Sun, 14 Feb 2021 02:16:59 +0300 Subject: [PATCH] [CONSISTENCY] The sum of numeric column values in the database are compared for consistency of data. --- .gitignore | 3 +++ pgdatadiff/pgdatadiff.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 894a44c..933cf5b 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ venv.bak/ # mypy .mypy_cache/ + +# idea +.idea diff --git a/pgdatadiff/pgdatadiff.py b/pgdatadiff/pgdatadiff.py index 1bb9be1..145b50e 100644 --- a/pgdatadiff/pgdatadiff.py +++ b/pgdatadiff/pgdatadiff.py @@ -33,6 +33,32 @@ def __init__(self, firstdb, seconddb, chunk_size=10000, count_only=False): self.chunk_size = int(chunk_size) self.count_only = count_only + def diff_sum_table_numeric_data(self): + + SQL_NUMERIC_TABLES_AND_COLUMNS = f""" + SELECT table_name, column_name + FROM information_schema.columns + WHERE table_schema = 'public' + AND data_type = 'numeric'; + """ + + result_set = self.firstsession.execute(SQL_NUMERIC_TABLES_AND_COLUMNS).fetchall() + + for row in result_set: + table_name = row['table_name'] + column_name = row['column_name'] + + SQL_PAYMENT_TRANSACTION_SUM = f""" + SELECT sum({column_name}) + FROM {table_name}; + """ + + first_sum_result = self.firstsession.execute(SQL_PAYMENT_TRANSACTION_SUM).fetchone() + second_sum_result = self.secondsession.execute(SQL_PAYMENT_TRANSACTION_SUM).fetchone() + if first_sum_result != second_sum_result: + print(bold(red(table_name + ' sum(' + column_name + ') is different'))) + print(bold(green(table_name + ' sum(' + column_name + ') data is identical'))) + def diff_table_data(self, tablename): try: firsttable = Table(tablename, self.firstmeta, autoload=True) @@ -137,6 +163,9 @@ def diff_all_sequences(self): def diff_all_table_data(self): failures = 0 print(bold(red('Starting table analysis.'))) + + self.diff_sum_table_numeric_data() + with warnings.catch_warnings(): warnings.simplefilter("ignore", category=sa_exc.SAWarning) tables = sorted( @@ -158,4 +187,3 @@ def diff_all_table_data(self): if failures > 0: return 1 return 0 -