Skip to content

Commit

Permalink
Merge pull request #80 from PerfectFit-project/fix-sessions
Browse files Browse the repository at this point in the history
Fix sessions
  • Loading branch information
wbaccinelli authored Sep 22, 2023
2 parents 9e18398 + d6bd045 commit f96cf4b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
6 changes: 1 addition & 5 deletions helper/create_new_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ def create_test_data(user_id: int):
test_user_id = int(sys.argv[1])
except IndexError:
test_user_id = os.environ['TEST_USER_ID']
try:
db_url = os.environ['DATABASE_URL']
session = get_db_session(db_url)
except KeyError:
session = get_db_session()
session = get_db_session()

create_user_data(session, test_user_id)
logging.info('Successfully populated database with fixed data')
44 changes: 29 additions & 15 deletions helper/helper_functions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import json
import logging

import importlib_resources
import sys
import os

from sqlalchemy import MetaData, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool

DATABASE_URL = os.getenv('DATABASE_URL')

if not DATABASE_URL:
DATABASE_URL = 'postgresql://root:root@db:5432/perfectfit'

DB_URL_DEFAULT = 'postgresql://root:root@db:5432/perfectfit'

def santize_db_url(db_url):
"""
Expand All @@ -22,21 +29,29 @@ def santize_db_url(db_url):

return db_url

def get_db_session(db_url=DB_URL_DEFAULT):

sanitized_db_url = santize_db_url(db_url)
engine = create_engine(sanitized_db_url)
meta = MetaData()
meta.reflect(bind=engine)
def init_engine():
try:
# initialize engine and session maker
engine = create_engine(santize_db_url(DATABASE_URL), poolclass=NullPool)
meta = MetaData()
meta.reflect(bind=engine)

return sessionmaker(bind=engine)
except:
logging.error('Connection to DB failed. Empty session maker')
return None


# Check that db actually has a Users table
# (have the alembic migrations been run to set it up appropriately?)
if 'users' not in meta.tables:
sys.exit('"users" table not found in db. Has the schema been '
'set up correctly with the alembic migrations? See '
'instructions in README in db/ directory.')
session_maker = init_engine()


def get_db_session():
global session_maker

if not session_maker:
session_maker = init_engine()

session_maker = sessionmaker(bind=engine)
session = session_maker()

return session
Expand All @@ -49,4 +64,3 @@ def get_timing():
timing = json.loads(string_json)

return timing

6 changes: 1 addition & 5 deletions helper/populate_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,7 @@ def initialize_phases_table():

# docker-compose will provide the db url as environment variable. If this variable
# cant be found, the defaults from the helper module will be used.
try:
db_url = os.environ['DATABASE_URL']
session = get_db_session(db_url)
except KeyError:
session = get_db_session()
session = get_db_session()

populate_db_fixed_data(session)
logging.info('Successfully populated database with fixed data')
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
psycopg2
databases[postgresql]
python-dateutil
SQLAlchemy
SQLAlchemy==1.4.49
pypandoc
alembic
alembic==1.11.3
python-dotenv

0 comments on commit f96cf4b

Please sign in to comment.