-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract PyMySQL specifics into a separate file.
This lays some groundwork for perhaps supporting multiple drivers in the future.
- Loading branch information
Showing
6 changed files
with
127 additions
and
59 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import urllib.parse | ||
from collections.abc import Iterator | ||
from contextlib import contextmanager | ||
|
||
import pymysql | ||
import pymysql.constants.ER | ||
from pymysql.cursors import DictCursor | ||
from typing_extensions import Self, override | ||
|
||
from langgraph.checkpoint.mysql import BaseSyncMySQLSaver, _get_connection | ||
|
||
|
||
class PyMySQLSaver(BaseSyncMySQLSaver[pymysql.Connection, DictCursor]): | ||
@classmethod | ||
@contextmanager | ||
def from_conn_string( | ||
cls, | ||
conn_string: str, | ||
) -> Iterator[Self]: | ||
"""Create a new PyMySQLSaver instance from a connection string. | ||
Args: | ||
conn_string (str): The MySQL connection info string. | ||
Returns: | ||
PyMySQLSaver: A new PyMySQLSaver instance. | ||
""" | ||
parsed = urllib.parse.urlparse(conn_string) | ||
|
||
with pymysql.connect( | ||
host=parsed.hostname, | ||
user=parsed.username, | ||
password=parsed.password or "", | ||
database=parsed.path[1:], | ||
port=parsed.port or 3306, | ||
autocommit=True, | ||
) as conn: | ||
yield cls(conn) | ||
|
||
@override | ||
@staticmethod | ||
def _is_no_such_table_error(e: Exception) -> bool: | ||
return ( | ||
isinstance(e, pymysql.ProgrammingError) | ||
and e.args[0] == pymysql.constants.ER.NO_SUCH_TABLE | ||
) | ||
|
||
@override | ||
@contextmanager | ||
def _cursor(self) -> Iterator[DictCursor]: | ||
with _get_connection(self.conn) as conn: | ||
with self.lock, conn.cursor(DictCursor) as cur: | ||
yield cur |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "langgraph-checkpoint-mysql" | ||
version = "1.0.1" | ||
version = "1.0.2" | ||
description = "Library with a MySQL implementation of LangGraph checkpoint saver." | ||
authors = ["Theodore Ni <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -13,6 +13,7 @@ python = "^3.9.0,<4.0" | |
langgraph-checkpoint = "^1.0.11" | ||
pymysql = { version = "^1.1.1", optional = true } | ||
aiomysql = { version = "^0.2.0", optional = true } | ||
typing-extensions = "^4.12.2" | ||
|
||
[tool.poetry.extras] | ||
pymysql = ["pymysql"] | ||
|
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