Repository navigation
Conversation
Merging this PR will not alter performance1 warning
Comparing |
waketzheng
reviewed
Oct 25, 2026
| except Exception: | ||
| if not await self._table_exists(): | ||
| return [] | ||
| _, rows = await self.connection.execute_query(query) |
Contributor
There was a problem hiding this comment.
@rhymiz How about:
if not await self._table_exists():
return []
try:
_, rows = await self.connection.execute_query(query)
except Exception:
return []
MigrationRecorder.applied_migrations() caught every exception from its
SELECT and returned an empty list. A refused connection ("too many
clients already"), an authentication failure, a timeout or a permission
error therefore looked like a database with no migrations applied.
Check the dialect's catalog for the migrations table first (sqlite_master,
to_regclass, information_schema.tables, OBJECT_ID, all_tables) and return
an empty list only when it is absent. Every other error now propagates.
rhymiz
force-pushed
the
fix/applied-migrations-errors
branch
from
October 26, 2026 08:46
8506df0 to
62aa3b5
Compare
Author
|
@waketzheng mind taking a look again? |
…pplied-migrations-errors
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
MigrationRecorder.applied_migrations()caught every exception from itsSELECTand returned[]:This PR adds
_table_exists(). It asks the dialect's catalog whether the migrations table exists, using the lookup that matches how the unqualifiedSELECTresolves the name:sqlite_masterto_regclass(...)(followssearch_path)information_schema.tableswithtable_schema = DATABASE()OBJECT_ID(?, 'U')all_tableswithowner = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')applied_migrations()now returns[]only when the table is absent, and every other error propagates. Dialects outside these five run theSELECTdirectly, so their errors propagate too. Checking the catalog avoids depending on each backend's exception translation, which wraps driver errors differently. It also means a failedSELECTno longer breaks an open PostgreSQL transaction.Motivation and Context
We found this in production. Our PostgreSQL server refused connections with "too many clients already", and
applied_migrations()reported no applied migrations. Our startup check then said the database lacked the build's migrations, which pointed at the wrong problem. Authentication failures, timeouts and permission errors were reported the same way. Only a missing migrations table should mean "none applied".How Has This Been Tested?
New tests in
tests/migrations/test_recorder.py:[]without querying the table.DBConnectionErrorpropagates.OperationalErroron theSELECTpropagates when the table exists.applied_migrations()returns[]both before and afterMigrationExecutor.migrate()creates the table.The error-propagation tests fail on the old recorder and pass with this change.
Results:
make testoptions on sqlite: 2131 passed, 149 skipped, 2 xfailed.tests/migrationsandtests/clion PostgreSQL with asyncpg: 358 passed. With psycopg: 358 passed.ruff format --check,ruff check,mypyandbanditpass.Not run against real MySQL, MSSQL or Oracle; for those dialects, only the fake-connection tests cover the change.
Checklist: