from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.7.2'
+__version__ = '0.7.3'
LOG = logging.getLogger(__name__)
# =============================================================================
self.import_perm_templ_items()
self.import_users()
self.import_zone_templ()
+ self.import_zone_templ_records()
+ self.import_zones()
finally:
self._close_all()
if self.tgt_db_is_poweradmin:
sequences.append('perm_templ_items_id_seq')
sequences.append('zone_templ_id_seq')
- sequences.append('zone_templ_records_id_seq')
- sequences.append('zones_id_seq')
LOG.info("Truncating all tables in target database ...")
if not self.simulate:
tgt_cursor.execute(sql, (max_id, ))
+ sql = "SELECT SETVAL('zone_templ_records_id_seq', 0)"
+ LOG.debug("Setting curval of zone_templ_records_id_seq to {} ...".format(0))
+ if self.verbose > 1:
+ LOG.debug("SQL: {}".format(sql))
+ if not self.simulate:
+ tgt_cursor.execute(sql, (max_id, ))
+
+ sql = "SELECT SETVAL('zones_id_seq', 0)"
+ LOG.debug("Setting curval of zones_id_seq to {} ...".format(0))
+ 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()
LOG.debug("Commiting changes ...")
self.tgt_connection.commit()
+ # -------------------------------------------------------------------------
+ def import_zone_templ_records(self):
+
+ LOG.info("Importing all zone template records ...")
+
+ src_sql = textwrap.dedent('''\
+ SELECT zone_templ_id, name, type, content, ttl, prio
+ FROM zone_templ_records
+ ORDER BY id
+ ''').strip()
+ if self.verbose > 1:
+ LOG.debug("Source SQL:\n{}".format(src_sql))
+
+ tgt_sql = textwrap.dedent('''\
+ INSERT INTO zone_templ_records (
+ zone_templ_id, name, type, content, ttl, prio)
+ VALUES (
+ %(zone_templ_id)s, %(name)s, %(type)s, %(content)s, %(ttl)s, %(prio))
+ ''').strip()
+ if self.verbose > 1:
+ LOG.debug("Target 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 > 3:
+ LOG.debug("Got zone templates:\n{}".format(pp(results)))
+
+ if not results:
+ LOG.info("No zone templates in source database.")
+ LOG.debug("Commiting changes ...")
+ self.tgt_connection.commit()
+ return
+
+ for result in results:
+ i += 1
+ if not self.simulate:
+ tgt_cursor.execute(tgt_sql, result)
+ LOG.info("Imported {} zone templates.".format(i))
+
+ LOG.debug("Commiting changes ...")
+ self.tgt_connection.commit()
+
+ # -------------------------------------------------------------------------
+ def import_zones(self):
+
+ LOG.info("Importing all zones ...")
+
+ src_sql = textwrap.dedent('''\
+ SELECT domain_id, owner, comment, zone_templ_id
+ FROM zones
+ WHERE domain_id IN (
+ SELECT id FROM domains)
+ ORDER BY id
+ ''').strip()
+ if self.verbose > 1:
+ LOG.debug("Source SQL:\n{}".format(src_sql))
+
+ tgt_sql = textwrap.dedent('''\
+ INSERT INTO zones (
+ domain_id, owner, comment, zone_templ_id)
+ VALUES (
+ %(domain_id)s, %(owner)s, %(comment)s, %(zone_templ_id)s)
+ ''').strip()
+ if self.verbose > 1:
+ LOG.debug("Target 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 > 3:
+ LOG.debug("Got zones:\n{}".format(pp(results)))
+
+ if not results:
+ LOG.info("No zones in source database.")
+ LOG.debug("Commiting changes ...")
+ self.tgt_connection.commit()
+ return
+
+ for result in results:
+ i += 1
+
+ comment = result['comment']
+ if comment is not None:
+ comment = comment.strip()
+ if comment == '':
+ comment = None
+ result['comment'] = comment
+
+ zone_templ_id = result['zone_templ_id']
+ if zone_templ_id is not None and zone_templ_id == 0:
+ result['zone_templ_id'] = None
+
+ if not self.simulate:
+ tgt_cursor.execute(tgt_sql, result)
+
+ LOG.info("Imported {} zones.".format(i))
+
+ LOG.debug("Commiting changes ...")
+ self.tgt_connection.commit()
+
# -------------------------------------------------------------------------
def _close_all(self):