From 564477c336e40330a4fb522c850b686b60a19f0c Mon Sep 17 00:00:00 2001 From: harshadyeola Date: Thu, 26 Mar 2015 19:52:51 +0530 Subject: [PATCH] added function to check if database already exists --- ee/core/mysql.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ee/core/mysql.py b/ee/core/mysql.py index df9112d7..8295c770 100644 --- a/ee/core/mysql.py +++ b/ee/core/mysql.py @@ -1,5 +1,6 @@ """EasyEngine MySQL core classes.""" import pymysql +from pymysql import connections, DatabaseError import configparser from os.path import expanduser import sys @@ -102,3 +103,17 @@ class EEMysql(): except Exception as e: Log.error(self, "Error: process exited with status %s" % e) + + def check_db_exists(self, db_name): + try: + connection = connections.Connection(db=db_name, + read_default_file='~/.my.cnf') + if connection: + return True + except DatabaseError as e: + if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name): + return False + else: + raise RuntimeError + except Exception as e: + Log.error(self, "Runtime Exception occured")