from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.3.0'
+__version__ = '0.4.0'
LOG = logging.getLogger(__name__)
# =============================================================================
try:
self.get_src_info()
self.clean_tgt_db()
+ self.import_domains()
finally:
self._close_all()
# -------------------------------------------------------------------------
def clean_tgt_db(self):
- result = None
-
tables = [
'comments', 'cryptokeys', 'domainmetadata', 'records',
'supermasters', 'tsigkeys', 'domains',
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:
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()
# -------------------------------------------------------------------------