]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Importing records
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 11 Jul 2017 09:34:59 +0000 (11:34 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 11 Jul 2017 09:34:59 +0000 (11:34 +0200)
pp_lib/import_pdnsdata.py

index f49bfd103dd2947c535464dfc9dd4cf338881046..d646f996ba5044137a6a2a63a21d3161aa77b2dd 100644 (file)
@@ -29,7 +29,7 @@ from .common import pp
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.4.2'
+__version__ = '0.4.3'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -154,6 +154,7 @@ class ImportPdnsdataApp(PpConfigApplication):
             self.import_domains()
             self.import_cryptokeys()
             self.import_domainmetadata()
+            self.import_records()
         finally:
             self._close_all()
 
@@ -313,6 +314,8 @@ class ImportPdnsdataApp(PpConfigApplication):
         src_sql = textwrap.dedent('''\
             SELECT id, domain_id, flags, active, content
               FROM cryptokeys
+             WHERE domain_id IN (
+                    SELECT id FROM domains)
             ''').strip()
         if self.verbose > 1:
             LOG.debug("Source SQL:\n{}".format(src_sql))
@@ -429,6 +432,79 @@ class ImportPdnsdataApp(PpConfigApplication):
         LOG.debug("Commiting changes ...")
         self.tgt_connection.commit()
 
+    # -------------------------------------------------------------------------
+    def import_records(self):
+
+        LOG.info("Importing all records ...")
+
+        src_sql = textwrap.dedent('''\
+            SELECT id, domain_id, name, type, content,
+                   ttl, prio, change_date, ordername, auth
+              FROM records
+             WHERE domain_id IN (
+                    SELECT id FROM domains)
+             ORDER BY name
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL:\n{}".format(src_sql))
+
+        tgt_sql = textwrap.dedent('''\
+            INSERT INTO records (id, domain_id, name, type, content,
+                                 ttl, prio, change_date, disabled,
+                                 ordername, auth)
+                 VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s,
+                         %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s,
+                         %(ordername)s, %(auth)s)
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL:\n{}".format(tgt_sql))
+
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+            with self.src_connection.cursor() as src_cursor:
+
+                i = 0
+                src_cursor.execute(src_sql)
+                results = src_cursor.fetchall()
+
+                if self.verbose > 2:
+                    LOG.debug("Got records:\n{}".format(pp(results)))
+
+                if not results:
+                    LOG.info("No records in source database.")
+                    LOG.debug("Commiting changes ...")
+                    self.tgt_connection.commit()
+                    return
+
+                for result in results:
+                    i += 1
+                    result['disabled'] = False
+                    if result['auth'] is not None:
+                        if result['auth']:
+                            result['auth'] = True
+                        else:
+                            result['auth'] = False
+                    tgt_cursor.execute(tgt_sql, result)
+                LOG.info("Imported {} records.".format(i))
+
+            LOG.debug("Get max. records Id ...")
+            sql = "SELECT MAX(id) AS max_id FROM records"
+            if self.verbose > 1:
+                LOG.debug("SQL: {}".format(sql))
+            tgt_cursor.execute(sql)
+            result = tgt_cursor.fetchone()
+            if self.verbose > 2:
+                LOG.debug("Got max records Id:\n{}".format(pp(result)))
+            max_id = int(result[0])
+            sql = "SELECT SETVAL('records_id_seq', %s)"
+            LOG.debug("Setting curval of records_id_seq to {} ...".format(max_id))
+            if self.verbose > 1:
+                LOG.debug("SQL: {}".format(sql))
+            tgt_cursor.execute(sql, (max_id, ))
+
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
+
     # -------------------------------------------------------------------------
     def _close_all(self):