From: Frank Brehm Date: Fri, 21 Jul 2017 10:38:28 +0000 (+0200) Subject: Starting with rewrite for MySQL target DB X-Git-Tag: 0.1.2~159 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=a8df183a74a076d5beededd50ca960d4d5655edf;p=pixelpark%2Fadmin-tools.git Starting with rewrite for MySQL target DB --- diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index cfe942b..f11eb4c 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.1' +__version__ = '0.6.2' LOG = logging.getLogger(__name__) # ============================================================================= @@ -282,6 +282,12 @@ class ImportPdnsdataApp(PpConfigApplication): # ------------------------------------------------------------------------- def pre_run(self): + self.connect_src_db() + self.connect_tgt_db() + + # ------------------------------------------------------------------------- + def connect_src_db(self): + result = None LOG.debug("Connecting to source MySQL database on {}@{}:{}/{} ...".format( @@ -312,6 +318,52 @@ class ImportPdnsdataApp(PpConfigApplication): e.__class__.__name__, e)) self.exit(6) + # ------------------------------------------------------------------------- + def connect_tgt_db(self): + + if self.tgt_db_type == 'mysql': + self.connect_tgt_db_mysql() + else: + self.connect_tgt_db_psql() + + # ------------------------------------------------------------------------- + def connect_tgt_db_mysql(self): + + result = None + + LOG.debug("Connecting to target MySQL database on {}@{}:{}/{} ...".format( + self.tgt_db_user, self.tgt_db_host, self.tgt_db_port, self.tgt_db_schema)) + try: + self.tgt_connection = pymysql.connect( + host=self.tgt_db_host, + port=self.tgt_db_port, + db=self.tgt_db_schema, + user=self.tgt_db_user, + password=self.tgt_db_pass, + charset='utf8', + cursorclass=pymysql.cursors.DictCursor + ) + + sql = 'SHOW VARIABLES LIKE "version"' + if self.verbose > 1: + LOG.debug("SQL: {}".format(sql)) + with self.tgt_connection.cursor() as cursor: + cursor.execute(sql) + result = cursor.fetchone() + if self.verbose > 2: + LOG.debug("Got version info:\n{}".format(pp(result))) + LOG.info("Target database is MySQL version {!r}.".format(result['Value'])) + + except (pymysql.err.OperationalError) as e: + LOG.error("Could not connect to target database ({}): {}".format( + e.__class__.__name__, e)) + self.exit(6) + + # ------------------------------------------------------------------------- + def connect_tgt_db_psql(sellf): + + result = None + LOG.debug("Connecting to target PostgreSQL database on {}@{}:{}/{} ...".format( self.tgt_db_user, self.tgt_db_host, self.tgt_db_port, self.tgt_db_schema)) try: