Skip to content

Commit 86d2d9a

Browse files
committed
Address review comments: Make migration check more robust (check for id column existence and whether it is a primary key and of the right data type. Also added check for valid row with id=1). Added further tests for migration detection and execution for more comprehensive coverage. Fixed callback_data key mismatch bug
1 parent 0cb744d commit 86d2d9a

15 files changed

+160
-13
lines changed

ptbcontrib/postgres_persistence/postgrespersistence.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(
8181
user_data_json = data.get("user_data", "{}")
8282
bot_data_json = data.get("bot_data", "{}")
8383
conversations_json = data.get("conversations", "{}")
84-
callback_data_json = data.get("callback_data_json", "")
84+
callback_data_json = data.get("callback_data", "")
8585

8686
self.logger.info("Database loaded successfully!")
8787

@@ -120,10 +120,31 @@ def __init_database(self) -> None:
120120
CONSTRAINT single_row CHECK (id = 1));"""
121121
self._session.execute(text(create_table_qry))
122122

123+
# Check if id column exists, is an integer type, and is a primary key
123124
check_column_qry = """
124-
SELECT 1 FROM information_schema.columns
125-
WHERE table_name = 'persistence' AND column_name = 'id';"""
126-
needs_migration = self._session.execute(text(check_column_qry)).first() is None
125+
SELECT 1 FROM information_schema.columns c
126+
JOIN information_schema.key_column_usage kcu
127+
ON c.table_name = kcu.table_name
128+
AND c.column_name = kcu.column_name
129+
AND c.table_schema = kcu.table_schema
130+
JOIN information_schema.table_constraints tc
131+
ON kcu.constraint_name = tc.constraint_name
132+
AND kcu.table_schema = tc.table_schema
133+
WHERE c.table_schema = current_schema()
134+
AND c.table_name = 'persistence'
135+
AND c.column_name = 'id'
136+
AND c.data_type IN ('integer', 'smallint', 'bigint')
137+
AND tc.constraint_type = 'PRIMARY KEY';"""
138+
column_valid = self._session.execute(text(check_column_qry)).first() is not None
139+
140+
# If column exists, check if there's a valid row with id = 1
141+
data_valid = False
142+
if column_valid:
143+
check_data_qry = """
144+
SELECT 1 FROM persistence WHERE id = 1;"""
145+
data_valid = self._session.execute(text(check_data_qry)).first() is not None
146+
147+
needs_migration = not (column_valid and data_valid)
127148

128149
if needs_migration:
129150
self.logger.info("Old database schema detected. Running migration...")

tests/data/game.gif

-35.7 KB
Binary file not shown.

tests/data/game.png

-40 KB
Binary file not shown.

tests/data/sticker_set_thumb.png

-1.75 KB
Binary file not shown.

tests/data/telegram.gif

-3.79 KB
Binary file not shown.

tests/data/telegram.jpg

-32.3 KB
Binary file not shown.

tests/data/telegram.ogg

-8.98 KB
Binary file not shown.

tests/data/telegram.png

-12.6 KB
Binary file not shown.

tests/data/telegram.webp

-38.6 KB
Binary file not shown.
-44.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)