]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding truncating of target tables
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 10 Jul 2017 15:16:54 +0000 (17:16 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 10 Jul 2017 15:16:54 +0000 (17:16 +0200)
pp_lib/import_pdnsdata.py

index 5643886bff630b83bfcaacf0d269bcbb6e541077..d685bb4d83964594aac068815819ecacc05913a7 100644 (file)
@@ -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):