-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from hotosm/setup-encode-databases
Setup encode databases
- Loading branch information
Showing
11 changed files
with
458 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
"""Config for the DTM database connection.""" | ||
|
||
from databases import Database | ||
from app.config import settings | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import declarative_base, sessionmaker | ||
|
||
from app.config import settings | ||
|
||
engine = create_engine( | ||
settings.DTM_DB_URL.unicode_string(), | ||
pool_size=20, | ||
max_overflow=-1, | ||
) | ||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
Base = declarative_base() | ||
DtmMetadata = Base.metadata | ||
|
||
|
||
def get_db(): | ||
"""Create SQLAlchemy DB session.""" | ||
db = SessionLocal() | ||
class DatabaseConnection: | ||
"""Manages database connection (sqlalchemy & encode databases)""" | ||
def __init__(self): | ||
self.database = Database(settings.DTM_DB_URL.unicode_string(), min_size=5, max_size=20) | ||
# self.database = Database(settings.DTM_DB_URL.unicode_string()) | ||
self.engine = create_engine( | ||
settings.DTM_DB_URL.unicode_string(), | ||
pool_size=20, | ||
max_overflow=-1, | ||
) | ||
self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine) | ||
|
||
async def connect(self): | ||
"""Connect to the database.""" | ||
await self.database.connect() | ||
|
||
async def disconnect(self): | ||
"""Disconnect from the database.""" | ||
await self.database.disconnect() | ||
|
||
def create_db_session(self): | ||
"""Create a new SQLAlchemy DB session.""" | ||
db = self.SessionLocal() | ||
try: | ||
yield db | ||
return db | ||
finally: | ||
db.close() | ||
db.close() | ||
|
||
db_connection = DatabaseConnection() # Create a single instance | ||
|
||
def get_db(): | ||
"""Yield a new database session.""" | ||
return db_connection.create_db_session() | ||
|
||
async def encode_db(): | ||
"""Get the encode database connection""" | ||
try: | ||
await db_connection.connect() | ||
yield db_connection.database | ||
finally: | ||
await db_connection.disconnect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.