From bdaffd16b1012efd5c43c1eee12c172b4d061f8d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 11 Jul 2017 11:34:59 +0200 Subject: [PATCH] Importing records --- pp_lib/import_pdnsdata.py | 78 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index f49bfd1..d646f99 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.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): -- 2.39.5