Skip to content

Commit 0524dec

Browse files
fix: update PostgreSQL store for main branch compatibility
- Reformat SQL statements to avoid line length issues after codegen - Add noqa suppressions for unavoidable lint warnings - All changes compatible with SanitizationStrategy refactor from PR #210 Co-authored-by: William Easton <strawgate@users.noreply.github.com>
1 parent 542073d commit 0524dec

File tree

2 files changed

+17
-13
lines changed
  • key-value
    • key-value-aio/src/key_value/aio/stores/postgresql
    • key-value-sync/src/key_value/sync/code_gen/stores/postgresql

2 files changed

+17
-13
lines changed

key-value/key-value-aio/src/key_value/aio/stores/postgresql/store.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,12 @@ async def _setup_collection(self, *, collection: str) -> None:
261261
# Create the main table if it doesn't exist
262262
table_sql = (
263263
f"CREATE TABLE IF NOT EXISTS {self._table_name} ("
264-
"collection VARCHAR(255) NOT NULL, key VARCHAR(255) NOT NULL, "
265-
"value JSONB NOT NULL, ttl DOUBLE PRECISION, "
266-
"created_at TIMESTAMPTZ, expires_at TIMESTAMPTZ, "
264+
"collection VARCHAR(255) NOT NULL, "
265+
"key VARCHAR(255) NOT NULL, "
266+
"value JSONB NOT NULL, "
267+
"ttl DOUBLE PRECISION, "
268+
"created_at TIMESTAMPTZ, "
269+
"expires_at TIMESTAMPTZ, "
267270
"PRIMARY KEY (collection, key))"
268271
)
269272

@@ -394,9 +397,9 @@ async def _put_managed_entry(
394397
upsert_sql = (
395398
f"INSERT INTO {self._table_name} "
396399
"(collection, key, value, ttl, created_at, expires_at) "
397-
"VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (collection, key) "
398-
"DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, "
399-
"expires_at = EXCLUDED.expires_at"
400+
"VALUES ($1, $2, $3, $4, $5, $6) "
401+
"ON CONFLICT (collection, key) "
402+
"DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, expires_at = EXCLUDED.expires_at"
400403
)
401404
await conn.execute( # pyright: ignore[reportUnknownMemberType]
402405
upsert_sql,
@@ -444,9 +447,9 @@ async def _put_managed_entries(
444447
batch_upsert_sql = (
445448
f"INSERT INTO {self._table_name} "
446449
"(collection, key, value, ttl, created_at, expires_at) "
447-
"VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (collection, key) "
448-
"DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, "
449-
"expires_at = EXCLUDED.expires_at"
450+
"VALUES ($1, $2, $3, $4, $5, $6) "
451+
"ON CONFLICT (collection, key) "
452+
"DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, expires_at = EXCLUDED.expires_at"
450453
)
451454
await conn.executemany( # pyright: ignore[reportUnknownMemberType]
452455
batch_upsert_sql,

key-value/key-value-sync/src/key_value/sync/code_gen/stores/postgresql/store.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99

1010
# ruff: noqa: S608
11-
# ruff: noqa: E501
1211

1312
from collections.abc import AsyncIterator, Sequence
1413
from contextlib import contextmanager
@@ -256,7 +255,7 @@ def _setup_collection(self, *, collection: str) -> None:
256255
_ = self._sanitize_collection(collection=collection)
257256

258257
# Create the main table if it doesn't exist
259-
table_sql = f"CREATE TABLE IF NOT EXISTS {self._table_name} (collection VARCHAR(255) NOT NULL, key VARCHAR(255) NOT NULL, value JSONB NOT NULL, ttl DOUBLE PRECISION, created_at TIMESTAMPTZ, expires_at TIMESTAMPTZ, PRIMARY KEY (collection, key))"
258+
table_sql = f"CREATE TABLE IF NOT EXISTS {self._table_name} (collection VARCHAR(255) NOT NULL, key VARCHAR(255) NOT NULL, value JSONB NOT NULL, ttl DOUBLE PRECISION, created_at TIMESTAMPTZ, expires_at TIMESTAMPTZ, PRIMARY KEY (collection, key))" # noqa: E501
260259

261260
# Create index on expires_at for efficient TTL queries
262261
# Ensure index name <= 63 chars (PostgreSQL identifier limit)
@@ -370,7 +369,8 @@ def _put_managed_entry(self, *, key: str, collection: str, managed_entry: Manage
370369
sanitized_collection = self._sanitize_collection(collection=collection)
371370

372371
with self._acquire_connection() as conn:
373-
upsert_sql = f"INSERT INTO {self._table_name} (collection, key, value, ttl, created_at, expires_at) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (collection, key) DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, expires_at = EXCLUDED.expires_at" # pyright: ignore[reportUnknownMemberType]
372+
upsert_sql = f"INSERT INTO {self._table_name} (collection, key, value, ttl, created_at, expires_at) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (collection, key) DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, expires_at = EXCLUDED.expires_at" # noqa: E501
373+
# pyright: ignore[reportUnknownMemberType]
374374
conn.execute(
375375
upsert_sql,
376376
sanitized_collection,
@@ -415,7 +415,8 @@ def _put_managed_entries(
415415

416416
with self._acquire_connection() as conn:
417417
# Use executemany for batch insert
418-
batch_upsert_sql = f"INSERT INTO {self._table_name} (collection, key, value, ttl, created_at, expires_at) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (collection, key) DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, expires_at = EXCLUDED.expires_at" # pyright: ignore[reportUnknownMemberType]
418+
batch_upsert_sql = f"INSERT INTO {self._table_name} (collection, key, value, ttl, created_at, expires_at) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (collection, key) DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl, expires_at = EXCLUDED.expires_at" # noqa: E501
419+
# pyright: ignore[reportUnknownMemberType]
419420
conn.executemany(batch_upsert_sql, values)
420421

421422
@override

0 commit comments

Comments
 (0)