From 8247ac074f55fe5e9d0286195512a4afd47101c8 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 10 Jul 2017 18:14:30 +0200 Subject: [PATCH] Adding impot of domains. --- pp_lib/import_pdnsdata.py | 68 ++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index d685bb4..5c02979 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.3.0' +__version__ = '0.4.0' LOG = logging.getLogger(__name__) # ============================================================================= @@ -112,6 +112,7 @@ class ImportPdnsdataApp(PpConfigApplication): try: self.get_src_info() self.clean_tgt_db() + self.import_domains() finally: self._close_all() @@ -181,8 +182,6 @@ class ImportPdnsdataApp(PpConfigApplication): # ------------------------------------------------------------------------- def clean_tgt_db(self): - result = None - tables = [ 'comments', 'cryptokeys', 'domainmetadata', 'records', 'supermasters', 'tsigkeys', 'domains', @@ -190,9 +189,7 @@ class ImportPdnsdataApp(PpConfigApplication): LOG.info("Truncating all tables in target database ...") - cur = self.tgt_connection.cursor() - - try: + with self.tgt_connection.cursor() as tgt_cursor: for table in tables: @@ -200,13 +197,62 @@ class ImportPdnsdataApp(PpConfigApplication): sql = 'TRUNCATE TABLE {} RESTART IDENTITY CASCADE'.format(table) if self.verbose > 1: LOG.debug("SQL: {}".format(sql)) - cur.execute(sql) + tgt_cursor.execute(sql) - LOG.debug("Commiting changes ...") - self.tgt_connection.commit() + LOG.debug("Commiting changes ...") + self.tgt_connection.commit() - finally: - cur.close() + # ------------------------------------------------------------------------- + def import_domains(self): + + LOG.info("Importing all domains ...") + + src_sql = textwrap.dedent('''\ + SELECT id, name, master, last_check, type, notified_serial, account + FROM domains + ORDER by name + ''').strip() + if self.verbose > 1: + LOG.debug("Source SQL: {}".format(src_sql)) + + tgt_sql = textwrap.dedent('''\ + INSERT INTO domains (id, name, master, last_check, type, notified_serial, account) + VALUES (%(id)s, %(name)s, %(master)s, %(last_check)s, + %(type)s, %(notified_serial)s, %(account)s) + ''').strip() + if self.verbose > 1: + LOG.debug("Source SQL: {}".format(tgt_sql)) + + + with self.tgt_connection.cursor() as tgt_cursor: + with self.src_connection.cursor() as src_cursor: + + src_cursor.execute(src_sql) + results = src_cursor.fetchall() + + if self.verbose > 2: + LOG.debug("Got domains:\n{}".format(pp(results))) + + for result in results: + tgt_cursor.execute(tgt_sql, result) + + LOG.debug("Get max. Domain Id ...") + sql = "SELECT MAX(id) AS max_id from domains" + 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 domain Id:\n{}".format(pp(result))) + max_id = int(result[0]) + sql = "select setval('domains_id_seq', %s)" + LOG.debug("Setting curval of domains_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() # ------------------------------------------------------------------------- -- 2.39.5