-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor database wrapper to use sqlalchemy engine instead of aiomysql
- Loading branch information
Showing
4 changed files
with
56 additions
and
87 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
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,42 +1,27 @@ | ||
from aiomysql.sa import create_engine | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
|
||
|
||
class FAFDatabase: | ||
def __init__(self, loop): | ||
self._loop = loop | ||
self.engine = None | ||
|
||
async def connect( | ||
def __init__( | ||
self, | ||
host="localhost", | ||
port=3306, | ||
user="root", | ||
password="", | ||
db="faf_test", | ||
minsize=1, | ||
maxsize=1, | ||
host: str = "localhost", | ||
port: int = 3306, | ||
user: str = "root", | ||
password: str = "", | ||
db: str = "faf_test", | ||
**kwargs | ||
): | ||
if self.engine is not None: | ||
raise ValueError("DB is already connected!") | ||
self.engine = await create_engine( | ||
host=host, | ||
port=port, | ||
user=user, | ||
password=password, | ||
db=db, | ||
autocommit=True, | ||
loop=self._loop, | ||
minsize=minsize, | ||
maxsize=maxsize, | ||
kwargs["future"] = True | ||
sync_engine = create_engine( | ||
f"mysql+aiomysql://{user}:{password}@{host}:{port}/{db}", | ||
**kwargs | ||
) | ||
|
||
self.engine = AsyncEngine(sync_engine) | ||
|
||
def acquire(self): | ||
return self.engine.acquire() | ||
return self.engine.begin() | ||
|
||
async def close(self): | ||
if self.engine is None: | ||
return | ||
|
||
self.engine.close() | ||
await self.engine.wait_closed() | ||
self.engine = None | ||
await self.engine.dispose() |
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