Skip to content

Conversation

@taddes
Copy link
Collaborator

@taddes taddes commented Oct 21, 2025

Description

The integration test’s base TestCase writes directly to the database with raw mysql queries.

These have been adapted to work with postgres, while maintaining MySQL support. This is through:

  • Added a db_mode attribute that allows for checking if the SYNC_TOKENSERVER__DATABASE_URL env var resolves to a postgres or mysql prefix, to determine the logic paths.
  • Updating all queries to use the text method (aliased as sqltext) so that agnostic bind parameters work in both implementations. This also removes the clunky use of %s params.
  • Removing helper method that would call LAST_ROW_ID which only works for SQL. There were only 3 references to this method anyway, so it was not a particularly beneficial abstraction.
  • Refactored an instance of a query being built by string concatenation. While less verbose, it's a little harder to understand. Instead, created explicit queries and parameters for both versions, and then Postgres specific option.
  • General refactors for improved readability, a couple old style format strings replaced.

Testing

Running Tokenserver integration tests

Issue(s)

Closes STOR-384.

@taddes taddes self-assigned this Oct 21, 2025
@taddes taddes marked this pull request as ready for review October 21, 2025 16:50
@taddes taddes changed the title [WIP] test: refactor of tokenserver database.py to support postgres test: refactor of tokenserver python tests to support postgres Oct 21, 2025
@taddes taddes requested a review from chenba October 21, 2025 16:51
import unittest

from integration_tests.tokenserver.test_support import TestCase
from sqlalchemy.sql import text as sqltext
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this sqltext alias elsewhere, so keeping it consistent.

engine = create_engine(os.environ["SYNC_TOKENSERVER__DATABASE_URL"])
self.database = engine.execution_options(isolation_level="AUTOCOMMIT").connect()

self.db_mode = os.environ["SYNC_TOKENSERVER__DATABASE_URL"].split(":")[0]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enables us to check Postgres or SQL. This ended up being the best way to check.

if id:
query += ", id) VALUES(%s, %s, %s, %s, %s, %s, %s, %s)"
data += (id,)
if not id:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is verbose, but I have a personal dislike of constructing queries as strings and using concatenation, which in my experience have let to annoying bugs. I thought for our purposes, it's just simpler to have the different explicit queries, and then their Postgres variants. If/when we ever just move to a single DB for support, this can go, but for now, it's much less a headache.

@taddes taddes force-pushed the test/adapt-tokenserver-python-tests-postgres-STOR-384 branch 2 times, most recently from 71e150c to f224fca Compare October 21, 2025 19:54
@taddes taddes force-pushed the test/adapt-tokenserver-python-tests-postgres-STOR-384 branch from f224fca to f52320d Compare October 21, 2025 20:39
if self.db_mode == "postgres":
insert_sql = sqltext("""\
insert into users (service, email, nodeid, generation, keys_changed_at, client_state, created_at, replaced_at)
values (:service, :email, :nodeid, :generation, :keys_changed_at, :client_state, :created_at, :replaced_at)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(very much nit on formatting) I guess we don't have linter for this python code? These lines are long but also inconsistent with those in the else block.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have a linter (ruff) but it doesn't work/apply well to the multi-line comments, which can be annoying. I think this can be made to be similar to the SQL standard comment guide where the SQL statements can line up. No argument on it looking inconsistent...

Copy link
Collaborator

@chenba chenba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I like the explicit separation of MySQL and Postgres so we can delete the MySQL specific code easily.

@taddes taddes merged commit 835bc58 into master Oct 22, 2025
9 checks passed
@taddes taddes deleted the test/adapt-tokenserver-python-tests-postgres-STOR-384 branch October 22, 2025 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants