Skip to content

Commit ffe2ad4

Browse files
authored
Merge pull request chenyme#113 from chenyme/fix_mysql
feat: enhance SQL storage URL normalization and update README examples
2 parents b11aace + 51f6ce1 commit ffe2ad4

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

app/core/storage.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,24 @@ async def close(self):
691691
class StorageFactory:
692692
"""存储后端工厂"""
693693
_instance: Optional[BaseStorage] = None
694+
695+
@staticmethod
696+
def _normalize_sql_url(storage_type: str, url: str) -> str:
697+
if not url or "://" not in url:
698+
return url
699+
if storage_type == "mysql":
700+
if url.startswith("mysql://"):
701+
return f"mysql+aiomysql://{url[len('mysql://'):]}"
702+
if url.startswith("mariadb://"):
703+
return f"mariadb+aiomysql://{url[len('mariadb://'):]}"
704+
if storage_type == "pgsql":
705+
if url.startswith("postgres://"):
706+
return f"postgresql+asyncpg://{url[len('postgres://'):]}"
707+
if url.startswith("postgresql://"):
708+
return f"postgresql+asyncpg://{url[len('postgresql://'):]}"
709+
if url.startswith("pgsql://"):
710+
return f"postgresql+asyncpg://{url[len('pgsql://'):]}"
711+
return url
694712

695713
@classmethod
696714
def get_storage(cls) -> BaseStorage:
@@ -709,6 +727,7 @@ def get_storage(cls) -> BaseStorage:
709727

710728
elif storage_type in ("mysql", "pgsql"):
711729
if not storage_url: raise ValueError("SQL 存储需要设置 SERVER_STORAGE_URL")
730+
storage_url = cls._normalize_sql_url(storage_type, storage_url)
712731
cls._instance = SQLStorage(storage_url)
713732

714733
else:

docs/README.en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Default password: `grok2api` (config key `app.app_key`, change it in production)
4747
| `SERVER_STORAGE_TYPE` | Storage type (`local`/`redis`/`mysql`/`pgsql`) | `local` | `pgsql` |
4848
| `SERVER_STORAGE_URL` | Storage URL (empty for local) | `""` | `postgresql+asyncpg://user:password@host:5432/db` |
4949

50+
> MySQL example: `mysql+aiomysql://user:password@host:3306/db` (if you set `mysql://`, it will be normalized to `mysql+aiomysql://`)
51+
5052
### Usage limits
5153

5254
- Basic account: 80 requests / 20h

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ docker compose up -d
5757
| `SERVER_STORAGE_TYPE` | 存储类型(`local`/`redis`/`mysql`/`pgsql`| `local` | `pgsql` |
5858
| `SERVER_STORAGE_URL` | 存储连接串(local 时可为空) | `""` | `postgresql+asyncpg://user:password@host:5432/db` |
5959

60+
> MySQL 示例:`mysql+aiomysql://user:password@host:3306/db`(若填 `mysql://` 会自动转为 `mysql+aiomysql://`
61+
62+
> MySQL 示例:`mysql+aiomysql://user:password@host:3306/db`(若填 `mysql://` 会自动转为 `mysql+aiomysql://`
63+
6064
### 可用次数
6165

6266
- Basic 账号:80 次 / 20h

0 commit comments

Comments
 (0)