Skip to content

Commit c1d433b

Browse files
committed
replace f-strings with parametrized queries to avoid codacy error
1 parent 357a051 commit c1d433b

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

ptbcontrib/postgres_persistence/postgrespersistence.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,29 @@ def __init_database(self) -> None:
144144
# If column exists, check if there's a valid row with id = 1
145145
data_valid = False
146146
if column_valid:
147-
check_data_qry = f"""
148-
SELECT 1 FROM persistence WHERE id = {self.PERSISTENCE_ID};"""
149-
data_valid = self._session.execute(text(check_data_qry)).first() is not None
147+
check_data_qry = """
148+
SELECT 1 FROM persistence WHERE id = :id;"""
149+
result = self._session.execute(text(check_data_qry), {"id": self.PERSISTENCE_ID})
150+
data_valid = result.first() is not None
150151

151152
needs_migration = not (column_valid and data_valid)
152153

153154
if needs_migration:
154155
self.logger.info("Old database schema detected. Running migration...")
155156
migration_commands = [
156157
"ALTER TABLE persistence ADD COLUMN id INT;",
157-
f"""UPDATE persistence SET id = {self.PERSISTENCE_ID} WHERE ctid = (
158-
SELECT ctid FROM persistence LIMIT 1);""",
158+
"""UPDATE persistence SET id = :id WHERE ctid = ("
159+
"SELECT ctid FROM persistence LIMIT 1);""",
159160
"DELETE FROM persistence WHERE id IS NULL;",
160161
"ALTER TABLE persistence ALTER COLUMN id SET NOT NULL;",
161162
"ALTER TABLE persistence ADD PRIMARY KEY (id);",
162-
(
163-
f"ALTER TABLE persistence ADD CONSTRAINT single_row "
164-
f"CHECK (id = {self.PERSISTENCE_ID});"
165-
),
163+
"ALTER TABLE persistence ADD CONSTRAINT single_row CHECK (id = :id);",
166164
]
167165
for command in migration_commands:
168-
self._session.execute(text(command))
166+
if ":id" in command:
167+
self._session.execute(text(command), {"id": self.PERSISTENCE_ID})
168+
else:
169+
self._session.execute(text(command))
169170
self.logger.info("Database migration successful!")
170171

171172
self._session.commit()

tests/test_postgres_persistence.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def mock_execute(query, params=None):
252252
return FakeExecResultValidPK()
253253

254254
# Check for data validation query (id=1 exists)
255-
if "WHERE id = 1" in query.text and "information_schema" not in query.text:
255+
if "WHERE id = :id" in query.text and "information_schema" not in query.text:
256256
return FakeExecResultValidData()
257257

258258
return FakeExecResult()
@@ -267,7 +267,7 @@ def mock_execute(query, params=None):
267267
# Verify no migration commands were run
268268
migration_commands = [
269269
"ALTER TABLE persistence ADD COLUMN id INT",
270-
"UPDATE persistence SET id = 1",
270+
"UPDATE persistence SET id = :id",
271271
"DELETE FROM persistence WHERE id IS NULL",
272272
]
273273
for migration_cmd in migration_commands:
@@ -347,11 +347,11 @@ def mock_execute(query, params=None): # pylint: disable=unused-argument
347347
# Verify migration commands were run in correct order
348348
expected_migration_steps = [
349349
"ALTER TABLE persistence ADD COLUMN id INT",
350-
"UPDATE persistence SET id = 1",
350+
"UPDATE persistence SET id = :id",
351351
"DELETE FROM persistence WHERE id IS NULL",
352352
"ALTER TABLE persistence ALTER COLUMN id SET NOT NULL",
353353
"ALTER TABLE persistence ADD PRIMARY KEY (id)",
354-
"ALTER TABLE persistence ADD CONSTRAINT single_row CHECK (id = 1)",
354+
"ALTER TABLE persistence ADD CONSTRAINT single_row CHECK (id = :id)",
355355
]
356356

357357
for expected_step in expected_migration_steps:
@@ -500,7 +500,7 @@ def mock_execute(query, params=None): # pylint: disable=unused-argument
500500

501501
# Verify that migration includes step to update first row to id=1
502502
update_first_row = any(
503-
"UPDATE persistence SET id = 1" in query and "LIMIT 1" in query
503+
"UPDATE persistence SET id = :id" in query and "LIMIT 1" in query
504504
for query in executed_queries
505505
)
506506
assert update_first_row

0 commit comments

Comments
 (0)