|
17 | 17 |
|
18 | 18 |
|
19 | 19 | class MySQLBackend(DatabaseBackend): |
20 | | - def __init__(self, database_url: DatabaseURL) -> None: |
21 | | - self._database_url = database_url |
| 20 | + def __init__(self, database_url: typing.Union[DatabaseURL, str]) -> None: |
| 21 | + self._database_url = DatabaseURL(database_url) |
22 | 22 | self._dialect = pymysql.dialect(paramstyle="pyformat") |
23 | 23 | self._pool = None |
24 | 24 |
|
| 25 | + def _get_connection_kwargs(self) -> dict: |
| 26 | + options = self._database_url.options |
| 27 | + |
| 28 | + kwargs = {} |
| 29 | + min_size = options.get("min_size") |
| 30 | + max_size = options.get("max_size") |
| 31 | + ssl = options.get("ssl") |
| 32 | + |
| 33 | + if min_size is not None: |
| 34 | + kwargs["minsize"] = int(min_size) |
| 35 | + if max_size is not None: |
| 36 | + kwargs["maxsize"] = int(max_size) |
| 37 | + if ssl is not None: |
| 38 | + kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()] |
| 39 | + return kwargs |
| 40 | + |
25 | 41 | async def connect(self) -> None: |
26 | 42 | assert self._pool is None, "DatabaseBackend is already running" |
| 43 | + kwargs = self._get_connection_kwargs() |
27 | 44 | self._pool = await aiomysql.create_pool( |
28 | 45 | host=self._database_url.hostname, |
29 | 46 | port=self._database_url.port or 3306, |
30 | 47 | user=self._database_url.username or getpass.getuser(), |
31 | 48 | password=self._database_url.password, |
32 | 49 | db=self._database_url.database, |
33 | 50 | autocommit=True, |
| 51 | + **kwargs, |
34 | 52 | ) |
35 | 53 |
|
36 | 54 | async def disconnect(self) -> None: |
|
0 commit comments