Skip to content

Commit 4a92b15

Browse files
authored
fix: SQL connection parsing error (#312)
1 parent ecad812 commit 4a92b15

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

veadk/memory/short_term_memory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ class ShortTermMemory(BaseModel):
137137
def model_post_init(self, __context: Any) -> None:
138138
if self.db_url:
139139
logger.info("The `db_url` is set, ignore `backend` option.")
140+
if self.db_url.count("@") > 1 or self.db_url.count(":") > 2:
141+
logger.warning(
142+
"Multiple `@` or `:` symbols detected in the database URL. "
143+
"Please encode `username` or `password` with `urllib.parse.quote_plus`. "
144+
"Examples: p@ssword→p%40ssword."
145+
)
140146
self._session_service = DatabaseSessionService(db_url=self.db_url)
141147
else:
142148
if self.backend == "database":

veadk/memory/short_term_memory_backends/mysql_backend.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from pydantic import Field
2323
from typing_extensions import override
24+
from urllib.parse import quote_plus
2425

2526
import veadk.config # noqa E401
2627
from veadk.configs.database_configs import MysqlConfig
@@ -33,7 +34,9 @@ class MysqlSTMBackend(BaseShortTermMemoryBackend):
3334
mysql_config: MysqlConfig = Field(default_factory=MysqlConfig)
3435

3536
def model_post_init(self, context: Any) -> None:
36-
self._db_url = f"mysql+pymysql://{self.mysql_config.user}:{self.mysql_config.password}@{self.mysql_config.host}/{self.mysql_config.database}"
37+
encoded_username = quote_plus(self.mysql_config.user)
38+
encoded_password = quote_plus(self.mysql_config.password)
39+
self._db_url = f"mysql+pymysql://{encoded_username}:{encoded_password}@{self.mysql_config.host}/{self.mysql_config.database}"
3740

3841
@cached_property
3942
@override

veadk/memory/short_term_memory_backends/postgresql_backend.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from pydantic import Field
2424
from typing_extensions import override
25+
from urllib.parse import quote_plus
2526

2627
import veadk.config # noqa E401
2728
from veadk.configs.database_configs import PostgreSqlConfig
@@ -34,7 +35,9 @@ class PostgreSqlSTMBackend(BaseShortTermMemoryBackend):
3435
postgresql_config: PostgreSqlConfig = Field(default_factory=PostgreSqlConfig)
3536

3637
def model_post_init(self, context: Any) -> None:
37-
self._db_url = f"postgresql://{self.postgresql_config.user}:{self.postgresql_config.password}@{self.postgresql_config.host}:{self.postgresql_config.port}/{self.postgresql_config.database}"
38+
encoded_username = quote_plus(self.postgresql_config.user)
39+
encoded_password = quote_plus(self.postgresql_config.password)
40+
self._db_url = f"postgresql://{encoded_username}:{encoded_password}@{self.postgresql_config.host}:{self.postgresql_config.port}/{self.postgresql_config.database}"
3841
logger.debug(self._db_url)
3942

4043
@cached_property

0 commit comments

Comments
 (0)