Skip to content

Commit

Permalink
Merge pull request #55 from edx/hassan/mysql-create-database
Browse files Browse the repository at this point in the history
MySql S3 loading: create database if it does not exists.
  • Loading branch information
HassanJaveed84 authored Jan 6, 2022
2 parents 12f9f1e + f5efd21 commit de84d51
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions edx_prefectutils/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ def create_mysql_connection(credentials: dict, database: str, autocommit: bool =
password = credentials['password']
host = credentials['host']

connection = mysql.connector.connect(
user=user,
password=password,
host=host,
database=database,
autocommit=autocommit,
)
try:
connection = mysql.connector.connect(
user=user,
password=password,
host=host,
database=database,
autocommit=autocommit,
)
except mysql.connector.errors.ProgrammingError as err:
if 'Unknown database' in err.msg:
# Create the database if it doesn't exist.
connection = mysql.connector.connect(
user=user,
password=password,
host=host,
autocommit=autocommit,
)
cursor = connection.cursor()
cursor.execute(f'CREATE DATABASE IF NOT EXISTS {database}')
cursor.execute(f'USE {database}')
cursor.close()
else:
raise err

return connection

Expand Down

0 comments on commit de84d51

Please sign in to comment.