Skip to content

Commit

Permalink
Support in memory database specification
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Jul 13, 2023
1 parent 954dcb7 commit a3b3189
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 86 deletions.
154 changes: 91 additions & 63 deletions investing_algorithm_framework/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,71 +37,11 @@ def __init__(self, config=None, stateless=False, web=False):
def initialize(self):

if self._web:

if RESOURCE_DIRECTORY not in self._config \
and RESOURCE_DIRECTORY.upper() not in self._config:
raise ImproperlyConfigured(
"RESOURCE_DIRECTORY not set in configuration"
)
resource_dir = self._config.get(RESOURCE_DIRECTORY, None)

if not resource_dir:
resource_dir = self._config.get(
RESOURCE_DIRECTORY.upper(), None
)

if not os.path.exists(resource_dir):
try:
os.makedirs(resource_dir)
except OSError as e:
logger.error(e)
raise OperationalException(
f"Could not create resource directory: {e}"
)

self._config[DATABASE_DIRECTORY_PATH] = os.path.join(
resource_dir, "databases"
)
self._config[DATABASE_NAME] = "prod-database.sqlite3"
self._config[SQLALCHEMY_DATABASE_URI] = \
"sqlite:///" + os.path.join(
self._config[DATABASE_DIRECTORY_PATH],
self._config[DATABASE_NAME]
)
self._flask_app = create_flask_app(self._config)
self._initialize_web()
elif self._stateless:
self._config[SQLALCHEMY_DATABASE_URI] = "sqlite://"
self._initialize_stateless()
else:
if RESOURCE_DIRECTORY not in self._config \
and RESOURCE_DIRECTORY.upper() not in self._config:
raise ImproperlyConfigured(
"RESOURCE_DIRECTORY not set in configuration"
)
resource_dir = self._config.get(RESOURCE_DIRECTORY, None)

if not resource_dir:
resource_dir = self._config.get(
RESOURCE_DIRECTORY.upper(), None
)

if not os.path.exists(resource_dir):
try:
os.makedirs(resource_dir)
except OSError as e:
logger.error(e)
raise OperationalException(
f"Could not create resource directory: {e}"
)

self._config[DATABASE_DIRECTORY_PATH] = os.path.join(
resource_dir, "databases"
)
self._config[DATABASE_NAME] = "prod-database.sqlite3"
self._config[SQLALCHEMY_DATABASE_URI] = \
"sqlite:///" + os.path.join(
self._config[DATABASE_DIRECTORY_PATH],
self._config[DATABASE_NAME]
)
self._initialize_standard()

setup_sqlalchemy(self)
create_all_tables()
Expand Down Expand Up @@ -337,3 +277,91 @@ def create_portfolios(self):
"portfolio_id": portfolio.id
}
)

def _initialize_web(self):
resource_dir = self._config[RESOURCE_DIRECTORY]

if resource_dir is None:
self._config[SQLALCHEMY_DATABASE_URI] = "sqlite://"
else:
resource_dir = self._create_resource_directory_if_not_exists()
self._config[DATABASE_DIRECTORY_PATH] = os.path.join(
resource_dir, "databases"
)
self._config[DATABASE_NAME] = "prod-database.sqlite3"
self._config[SQLALCHEMY_DATABASE_URI] = \
"sqlite:///" + os.path.join(
self._config[DATABASE_DIRECTORY_PATH],
self._config[DATABASE_NAME]
)
self._create_database_if_not_exists()

self._flask_app = create_flask_app(self._config)

def _initialize_stateless(self):
self._config[SQLALCHEMY_DATABASE_URI] = "sqlite://"

def _initialize_standard(self):
resource_dir = self._config[RESOURCE_DIRECTORY]

if resource_dir is None:
self._config[SQLALCHEMY_DATABASE_URI] = "sqlite://"
else:
resource_dir = self._create_resource_directory_if_not_exists()
self._config[DATABASE_DIRECTORY_PATH] = os.path.join(
resource_dir, "databases"
)
self._config[DATABASE_NAME] = "prod-database.sqlite3"
self._config[SQLALCHEMY_DATABASE_URI] = \
"sqlite:///" + os.path.join(
self._config[DATABASE_DIRECTORY_PATH],
self._config[DATABASE_NAME]
)
self._create_database_if_not_exists()

def _create_resource_directory_if_not_exists(self):
if self._stateless:
return

resource_dir = self._config.get(RESOURCE_DIRECTORY, None)

if resource_dir is None:
return

if not os.path.exists(resource_dir):
try:
os.makedirs(resource_dir)
open(resource_dir, 'w').close()
except OSError as e:
logger.error(e)
raise OperationalException(
"Could not create resource directory"
)

return resource_dir

def _create_database_if_not_exists(self):
if self._stateless:
return

database_dir = self._config.get(DATABASE_DIRECTORY_PATH, None)

if database_dir is None:
return

database_name = self._config.get(DATABASE_NAME, None)

if database_name is None:
return

database_path = os.path.join(database_dir, database_name)

if not os.path.exists(database_dir):
try:
os.makedirs(database_dir)
open(database_path, 'w').close()
except OSError as e:
logger.error(e)
raise OperationalException(
"Could not create database directory"
)
2 changes: 1 addition & 1 deletion investing_algorithm_framework/domain/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DATABASE_URL = 'DATABASE_URL'
DEFAULT_DATABASE_NAME = "database"

RESOURCE_DIRECTORY = "resource_directory"
RESOURCE_DIRECTORY = "RESOURCE_DIRECTORY"
LOG_LEVEL = 'LOG_LEVEL'
BASE_DIR = 'BASE_DIR'
SQLALCHEMY_DATABASE_URI = 'SQLALCHEMY_DATABASE_URI'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from os import mkdir, path
import logging

from sqlalchemy import create_engine
from sqlalchemy import create_engine, StaticPool
from sqlalchemy.orm import DeclarativeBase, sessionmaker

from investing_algorithm_framework.domain import SQLALCHEMY_DATABASE_URI, \
OperationalException, DATABASE_NAME, DATABASE_DIRECTORY_PATH
OperationalException

Session = sessionmaker()
logger = logging.getLogger(__name__)
Expand All @@ -14,28 +13,24 @@
class SQLAlchemyAdapter:

def __init__(self, app):

self._app = app
if SQLALCHEMY_DATABASE_URI not in app.config \
or app.config[SQLALCHEMY_DATABASE_URI] is None:
raise OperationalException("SQLALCHEMY_DATABASE_URI not set")

if not app.stateless:
database_dir = app.config[DATABASE_DIRECTORY_PATH]
database_name = app.config[DATABASE_NAME]
database_path = path.join(database_dir, database_name)

if not path.exists(database_dir):
try:
mkdir(database_dir)
open(database_path, 'w').close()
except OSError as e:
logger.error(e)
raise OperationalException(
"Could not create database directory"
)

global Session
engine = create_engine(app.config[SQLALCHEMY_DATABASE_URI])

if app.config[SQLALCHEMY_DATABASE_URI] != "sqlite:///:memory:":
engine = create_engine(
app.config[SQLALCHEMY_DATABASE_URI],
connect_args={'check_same_thread': False},
poolclass=StaticPool
)
else:
engine = create_engine(
app.config[SQLALCHEMY_DATABASE_URI],
)

Session.configure(bind=engine)


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ wrapt==1.11.2
Flask==2.3.2
Flask-Migrate==2.6.0
Flask-Cors==3.0.9
SQLAlchemy==2.0.5.post1
SQLAlchemy==2.0.18
marshmallow==3.5.0
setuptools>=60.9.0
ccxt>=3.0.57
Expand Down
2 changes: 1 addition & 1 deletion tests/app/test_add_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def test_add(self):
)
self.assertIsNotNone(app.config)
self.assertIsNotNone(app.config.get("test"))
self.assertIsNotNone(app.config.get("resource_directory"))
self.assertIsNotNone(app.config.get(RESOURCE_DIRECTORY))

0 comments on commit a3b3189

Please sign in to comment.