igdb/Code/database/db_setup.py

26 lines
681 B
Python
Raw Normal View History

2020-01-02 21:36:35 +01:00
from os import environ
2020-01-02 04:27:45 +01:00
from sqlalchemy import create_engine
def create_connection():
db_name = get_env_variable("DB_NAME")
db_user = get_env_variable("DB_USER")
db_password = get_env_variable("DB_PASSWORD")
db_url = get_env_variable("DB_URL")
db_connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
user=db_user, pw=db_password, url=db_url, db=db_name
)
2020-01-02 21:36:35 +01:00
engine = create_engine(db_connection_uri, echo=True)
def create_tables():
2020-01-02 04:27:45 +01:00
def get_env_variable(name):
try:
2020-01-02 21:36:35 +01:00
return environ[name]
2020-01-02 04:27:45 +01:00
except KeyError:
message = "Expected environment variable '{}' not set.".format(name)
raise Exception(message)