From 48d4d35ba56736e3d8a21ce3376f0ff7500bd31f Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 10 Jul 2017 17:16:54 +0200 Subject: [PATCH] Adding truncating of target tables --- pp_lib/import_pdnsdata.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index 5643886..d685bb4 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -29,7 +29,7 @@ from .common import pp from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.1.0' +__version__ = '0.3.0' LOG = logging.getLogger(__name__) # ============================================================================= @@ -111,6 +111,7 @@ class ImportPdnsdataApp(PpConfigApplication): try: self.get_src_info() + self.clean_tgt_db() finally: self._close_all() @@ -177,6 +178,37 @@ class ImportPdnsdataApp(PpConfigApplication): print("Found tsigkeys: {:>8}".format(nr_tsigkeys)) print() + # ------------------------------------------------------------------------- + def clean_tgt_db(self): + + result = None + + tables = [ + 'comments', 'cryptokeys', 'domainmetadata', 'records', + 'supermasters', 'tsigkeys', 'domains', + ] + + LOG.info("Truncating all tables in target database ...") + + cur = self.tgt_connection.cursor() + + try: + + for table in tables: + + LOG.debug("Truncating {!r} ...".format(table)) + sql = 'TRUNCATE TABLE {} RESTART IDENTITY CASCADE'.format(table) + if self.verbose > 1: + LOG.debug("SQL: {}".format(sql)) + cur.execute(sql) + + LOG.debug("Commiting changes ...") + self.tgt_connection.commit() + + finally: + cur.close() + + # ------------------------------------------------------------------------- def _close_all(self): -- 2.39.5