From 8483e884ec40f370fd9d53b24f29a8428c9ba8a2 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 21 Jul 2017 13:50:47 +0200 Subject: [PATCH] Finished support for MySQL as target DB --- pp_lib/import_pdnsdata.py | 188 ++++++++++++++++++++------------------ 1 file changed, 100 insertions(+), 88 deletions(-) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index f11eb4c..9aab1b1 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -30,7 +30,7 @@ from .common import pp from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.6.2' +__version__ = '0.6.3' LOG = logging.getLogger(__name__) # ============================================================================= @@ -360,7 +360,7 @@ class ImportPdnsdataApp(PpConfigApplication): self.exit(6) # ------------------------------------------------------------------------- - def connect_tgt_db_psql(sellf): + def connect_tgt_db_psql(self): result = None @@ -490,7 +490,10 @@ class ImportPdnsdataApp(PpConfigApplication): for table in tables: LOG.debug("Truncating {!r} ...".format(table)) - sql = 'TRUNCATE TABLE {} RESTART IDENTITY CASCADE'.format(table) + if self.tgt_db_type == 'mysql': + sql = 'DELETE FROM {}'.format(table) + else: + sql = 'TRUNCATE TABLE {} RESTART IDENTITY CASCADE'.format(table) if self.verbose > 1: LOG.debug("SQL: {}".format(sql)) if not self.simulate: @@ -536,21 +539,22 @@ class ImportPdnsdataApp(PpConfigApplication): tgt_cursor.execute(tgt_sql, result) LOG.info("Imported {} domains.".format(i)) - 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)) - if not self.simulate: - tgt_cursor.execute(sql, (max_id, )) + if self.tgt_db_type != 'mysql': + 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)) + if not self.simulate: + tgt_cursor.execute(sql, (max_id, )) LOG.debug("Commiting changes ...") self.tgt_connection.commit() @@ -595,29 +599,31 @@ class ImportPdnsdataApp(PpConfigApplication): i = 0 for result in results: i += 1 - if result['active']: - result['active'] = True - else: - result['active'] = False + if self.tgt_db_type != 'mysql': + if result['active']: + result['active'] = True + else: + result['active'] = False if not self.simulate: tgt_cursor.execute(tgt_sql, result) LOG.info("Imported {} cryptokeys.".format(i)) - LOG.debug("Get max. CryptoKey Id ...") - sql = "SELECT MAX(id) AS max_id FROM cryptokeys" - 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 cryptokey Id:\n{}".format(pp(result))) - max_id = int(result[0]) - sql = "SELECT SETVAL('cryptokeys_id_seq', %s)" - LOG.debug("Setting curval of cryptokeys_id_seq to {} ...".format(max_id)) - if self.verbose > 1: - LOG.debug("SQL: {}".format(sql)) - if not self.simulate: - tgt_cursor.execute(sql, (max_id, )) + if self.tgt_db_type != 'mysql': + LOG.debug("Get max. CryptoKey Id ...") + sql = "SELECT MAX(id) AS max_id FROM cryptokeys" + 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 cryptokey Id:\n{}".format(pp(result))) + max_id = int(result[0]) + sql = "SELECT SETVAL('cryptokeys_id_seq', %s)" + LOG.debug("Setting curval of cryptokeys_id_seq to {} ...".format(max_id)) + if self.verbose > 1: + LOG.debug("SQL: {}".format(sql)) + if not self.simulate: + tgt_cursor.execute(sql, (max_id, )) LOG.debug("Commiting changes ...") self.tgt_connection.commit() @@ -666,21 +672,22 @@ class ImportPdnsdataApp(PpConfigApplication): tgt_cursor.execute(tgt_sql, result) LOG.info("Imported {} domainmetadata.".format(i)) - LOG.debug("Get max. DomainMetadata Id ...") - sql = "SELECT MAX(id) AS max_id FROM domainmetadata" - 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 domainmetadata Id:\n{}".format(pp(result))) - max_id = int(result[0]) - sql = "SELECT SETVAL('domainmetadata_id_seq', %s)" - LOG.debug("Setting curval of domainmetadata_id_seq to {} ...".format(max_id)) - if self.verbose > 1: - LOG.debug("SQL: {}".format(sql)) - if not self.simulate: - tgt_cursor.execute(sql, (max_id, )) + if self.tgt_db_type != 'mysql': + LOG.debug("Get max. DomainMetadata Id ...") + sql = "SELECT MAX(id) AS max_id FROM domainmetadata" + 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 domainmetadata Id:\n{}".format(pp(result))) + max_id = int(result[0]) + sql = "SELECT SETVAL('domainmetadata_id_seq', %s)" + LOG.debug("Setting curval of domainmetadata_id_seq to {} ...".format(max_id)) + if self.verbose > 1: + LOG.debug("SQL: {}".format(sql)) + if not self.simulate: + tgt_cursor.execute(sql, (max_id, )) LOG.debug("Commiting changes ...") self.tgt_connection.commit() @@ -730,31 +737,35 @@ class ImportPdnsdataApp(PpConfigApplication): 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 + if self.tgt_db_type == 'mysql': + result['disabled'] = 0 + else: + result['disabled'] = False + if result['auth'] is not None: + if result['auth']: + result['auth'] = True + else: + result['auth'] = False if not self.simulate: 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)) - if not self.simulate: - tgt_cursor.execute(sql, (max_id, )) + if self.tgt_db_type != 'mysql': + 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)) + if not self.simulate: + tgt_cursor.execute(sql, (max_id, )) LOG.debug("Commiting changes ...") self.tgt_connection.commit() @@ -846,21 +857,22 @@ class ImportPdnsdataApp(PpConfigApplication): tgt_cursor.execute(tgt_sql, result) LOG.info("Imported {} tsigkeys.".format(i)) - LOG.debug("Get max. TsigKey Id ...") - sql = "SELECT MAX(id) AS max_id FROM tsigkeys" - 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 TsigKey Id:\n{}".format(pp(result))) - max_id = int(result[0]) - sql = "SELECT SETVAL('tsigkeys_id_seq', %s)" - LOG.debug("Setting curval of tsigkeys_id_seq to {} ...".format(max_id)) - if self.verbose > 1: - LOG.debug("SQL: {}".format(sql)) - if not self.simulate: - tgt_cursor.execute(sql, (max_id, )) + if self.tgt_db_type != 'mysql': + LOG.debug("Get max. TsigKey Id ...") + sql = "SELECT MAX(id) AS max_id FROM tsigkeys" + 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 TsigKey Id:\n{}".format(pp(result))) + max_id = int(result[0]) + sql = "SELECT SETVAL('tsigkeys_id_seq', %s)" + LOG.debug("Setting curval of tsigkeys_id_seq to {} ...".format(max_id)) + if self.verbose > 1: + LOG.debug("SQL: {}".format(sql)) + if not self.simulate: + tgt_cursor.execute(sql, (max_id, )) LOG.debug("Commiting changes ...") self.tgt_connection.commit() -- 2.39.5