|
17 | 17 |
|
18 | 18 | from langchain_core.stores import BaseStore |
19 | 19 | from sqlalchemy import ( |
20 | | - Engine, |
21 | 20 | LargeBinary, |
| 21 | + Text, |
22 | 22 | and_, |
23 | 23 | create_engine, |
24 | 24 | delete, |
25 | 25 | select, |
26 | 26 | ) |
| 27 | +from sqlalchemy.engine.base import Engine |
27 | 28 | from sqlalchemy.ext.asyncio import ( |
28 | 29 | AsyncEngine, |
29 | 30 | AsyncSession, |
30 | | - async_sessionmaker, |
31 | 31 | create_async_engine, |
32 | 32 | ) |
33 | 33 | from sqlalchemy.orm import ( |
34 | 34 | Mapped, |
35 | 35 | Session, |
36 | 36 | declarative_base, |
37 | | - mapped_column, |
38 | 37 | sessionmaker, |
39 | 38 | ) |
40 | 39 |
|
| 40 | +try: |
| 41 | + from sqlalchemy.ext.asyncio import async_sessionmaker |
| 42 | +except ImportError: |
| 43 | + # dummy for sqlalchemy < 2 |
| 44 | + async_sessionmaker = type("async_sessionmaker", (type,), {}) # type: ignore |
| 45 | + |
41 | 46 | Base = declarative_base() |
42 | 47 |
|
| 48 | +try: |
| 49 | + from sqlalchemy.orm import mapped_column |
43 | 50 |
|
44 | | -def items_equal(x: Any, y: Any) -> bool: |
45 | | - return x == y |
| 51 | + class LangchainKeyValueStores(Base): # type: ignore[valid-type,misc] |
| 52 | + """Table used to save values.""" |
| 53 | + |
| 54 | + # ATTENTION: |
| 55 | + # Prior to modifying this table, please determine whether |
| 56 | + # we should create migrations for this table to make sure |
| 57 | + # users do not experience data loss. |
| 58 | + __tablename__ = "langchain_key_value_stores" |
| 59 | + |
| 60 | + namespace: Mapped[str] = mapped_column( |
| 61 | + primary_key=True, index=True, nullable=False |
| 62 | + ) |
| 63 | + key: Mapped[str] = mapped_column(primary_key=True, index=True, nullable=False) |
| 64 | + value = mapped_column(LargeBinary, index=False, nullable=False) |
| 65 | + |
| 66 | +except ImportError: |
| 67 | + # dummy for sqlalchemy < 2 |
| 68 | + from sqlalchemy import Column |
| 69 | + |
| 70 | + class LangchainKeyValueStores(Base): # type: ignore[valid-type,misc,no-redef] |
| 71 | + """Table used to save values.""" |
46 | 72 |
|
| 73 | + # ATTENTION: |
| 74 | + # Prior to modifying this table, please determine whether |
| 75 | + # we should create migrations for this table to make sure |
| 76 | + # users do not experience data loss. |
| 77 | + __tablename__ = "langchain_key_value_stores" |
47 | 78 |
|
48 | | -class LangchainKeyValueStores(Base): # type: ignore[valid-type,misc] |
49 | | - """Table used to save values.""" |
| 79 | + namespace = Column(Text(), primary_key=True, index=True, nullable=False) |
| 80 | + key = Column(Text(), primary_key=True, index=True, nullable=False) |
| 81 | + value = Column(LargeBinary, index=False, nullable=False) |
50 | 82 |
|
51 | | - # ATTENTION: |
52 | | - # Prior to modifying this table, please determine whether |
53 | | - # we should create migrations for this table to make sure |
54 | | - # users do not experience data loss. |
55 | | - __tablename__ = "langchain_key_value_stores" |
56 | 83 |
|
57 | | - namespace: Mapped[str] = mapped_column(primary_key=True, index=True, nullable=False) |
58 | | - key: Mapped[str] = mapped_column(primary_key=True, index=True, nullable=False) |
59 | | - value = mapped_column(LargeBinary, index=False, nullable=False) |
| 84 | +def items_equal(x: Any, y: Any) -> bool: |
| 85 | + return x == y |
60 | 86 |
|
61 | 87 |
|
62 | 88 | # This is a fix of original SQLStore. |
@@ -135,7 +161,10 @@ def __init__( |
135 | 161 | self.namespace = namespace |
136 | 162 |
|
137 | 163 | def create_schema(self) -> None: |
138 | | - Base.metadata.create_all(self.engine) |
| 164 | + Base.metadata.create_all(self.engine) # problem in sqlalchemy v1 |
| 165 | + # sqlalchemy.exc.CompileError: (in table 'langchain_key_value_stores', |
| 166 | + # column 'namespace'): Can't generate DDL for NullType(); did you forget |
| 167 | + # to specify a type on this Column? |
139 | 168 |
|
140 | 169 | async def acreate_schema(self) -> None: |
141 | 170 | assert isinstance(self.engine, AsyncEngine) |
|
0 commit comments