From f181cb5f2aa2824ce9ec5e043e58a19ebbe13533 Mon Sep 17 00:00:00 2001 From: Henning Kage Date: Fri, 16 Apr 2021 09:12:44 +0200 Subject: [PATCH] #16: Preserve table and column name cases --- CHANGELOG.md | 4 ++++ pganonymizer/utils.py | 15 ++++++++------- pganonymizer/version.py | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95ad2c4..e93cf5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Development +## 0.3.3 (2021-04-16) + +* [#16](https://github.com/rheinwerk-verlag/postgresql-anonymizer/issues/16): Preserve column and table cases during the copy process + ## 0.3.2 (2021-01-25) * [#15](https://github.com/rheinwerk-verlag/postgresql-anonymizer/pull/15): Fix for exclude bug ([abhinavvaidya90](https://github.com/abhinavvaidya90)) diff --git a/pganonymizer/utils.py b/pganonymizer/utils.py index 4f9265b..81f0f16 100644 --- a/pganonymizer/utils.py +++ b/pganonymizer/utils.py @@ -71,7 +71,7 @@ def build_data(connection, table, columns, excludes, total_count, verbose=False) row[key] = value if verbose: progress_bar.next() - table_columns = row.keys() + table_columns = ['"{}"'.format(column) for column in row.keys()] if not row_column_dict: continue data.append(row.values()) @@ -135,18 +135,19 @@ def import_data(connection, column_dict, source_table, table_columns, primary_ke :param list data: The table data. """ primary_key = primary_key if primary_key else DEFAULT_PRIMARY_KEY + temp_table = '"tmp_{table}"'.format(table=source_table) cursor = connection.cursor() - cursor.execute('CREATE TEMP TABLE source(LIKE %s INCLUDING ALL) ON COMMIT DROP;' % source_table) - copy_from(connection, data, 'source', table_columns) - set_columns = ', '.join(['{column} = s.{column}'.format(column=key) for key in column_dict.keys()]) + cursor.execute('CREATE TEMP TABLE %s (LIKE %s INCLUDING ALL) ON COMMIT DROP;' % (temp_table, source_table)) + copy_from(connection, data, temp_table, table_columns) + set_columns = ', '.join(['{column} = s.{column}'.format(column='"{}"'.format(key)) for key in column_dict.keys()]) sql = ( 'UPDATE {table} t ' 'SET {columns} ' - 'FROM source s ' + 'FROM {source} s ' 'WHERE t.{primary_key} = s.{primary_key};' - ).format(table=source_table, primary_key=primary_key, columns=set_columns) + ).format(table=source_table, columns=set_columns, source=temp_table, primary_key=primary_key) cursor.execute(sql) - cursor.execute('DROP TABLE source;') + cursor.execute('DROP TABLE %s;' % temp_table) cursor.close() diff --git a/pganonymizer/version.py b/pganonymizer/version.py index d71dfb6..a5d8d98 100644 --- a/pganonymizer/version.py +++ b/pganonymizer/version.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- -__version__ = '0.3.2' +__version__ = '0.3.3'