Skip to content

Commit e8c61b0

Browse files
committed
LCORE-1150: Schema namespace used by conversation cache
1 parent b24cdaf commit e8c61b0

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/cache/postgres_cache.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import psycopg2
5+
from psycopg2.extensions import AsIs
56

67
from cache.cache import Cache
78
from cache.cache_error import CacheError
@@ -38,6 +39,10 @@ class PostgresCache(Cache):
3839
```
3940
"""
4041

42+
CREATE_SCHEMA = """
43+
CREATE SCHEMA IF NOT EXISTS %s;
44+
"""
45+
4146
CREATE_CACHE_TABLE = """
4247
CREATE TABLE IF NOT EXISTS cache (
4348
user_id text NOT NULL,
@@ -133,6 +138,9 @@ def connect(self) -> None:
133138
# even if PostgreSQL is not alive
134139
self.connection = None
135140
config = self.postgres_config
141+
namespace = "public"
142+
if config.namespace is not None:
143+
namespace = config.namespace
136144
try:
137145
self.connection = psycopg2.connect(
138146
host=config.host,
@@ -143,8 +151,9 @@ def connect(self) -> None:
143151
sslmode=config.ssl_mode,
144152
sslrootcert=config.ca_cert_path,
145153
gssencmode=config.gss_encmode,
154+
options=f"-c search_path={namespace}",
146155
)
147-
self.initialize_cache()
156+
self.initialize_cache(namespace)
148157
except Exception as e:
149158
if self.connection is not None:
150159
self.connection.close()
@@ -166,7 +175,7 @@ def connected(self) -> bool:
166175
logger.error("Disconnected from storage: %s", e)
167176
return False
168177

169-
def initialize_cache(self) -> None:
178+
def initialize_cache(self, namespace: str) -> None:
170179
"""Initialize cache - clean it up etc."""
171180
if self.connection is None:
172181
logger.error("Cache is disconnected")
@@ -177,6 +186,10 @@ def initialize_cache(self) -> None:
177186
# and it should not interfere with other statements
178187
cursor = self.connection.cursor()
179188

189+
logger.info("Initializing schema")
190+
if namespace != "public":
191+
cursor.execute(PostgresCache.CREATE_SCHEMA, (AsIs(namespace),))
192+
180193
logger.info("Initializing table for cache")
181194
cursor.execute(PostgresCache.CREATE_CACHE_TABLE)
182195

tests/unit/cache/test_postgres_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def test_initialize_cache_when_connected(
220220
mocker.patch("psycopg2.connect")
221221
cache = PostgresCache(postgres_cache_config_fixture)
222222
# should not fail
223-
cache.initialize_cache()
223+
cache.initialize_cache("public")
224224

225225

226226
def test_initialize_cache_when_disconnected(
@@ -234,7 +234,7 @@ def test_initialize_cache_when_disconnected(
234234
cache.connection = None
235235

236236
with pytest.raises(CacheError, match="cache is disconnected"):
237-
cache.initialize_cache()
237+
cache.initialize_cache("public")
238238

239239

240240
def test_ready_method(

0 commit comments

Comments
 (0)