from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.4.2'
+__version__ = '0.4.3'
LOG = logging.getLogger(__name__)
# =============================================================================
self.import_domains()
self.import_cryptokeys()
self.import_domainmetadata()
+ self.import_records()
finally:
self._close_all()
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))
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):