Skip to content

fix: make get_current_schema async to fix schema.table() alter migrations - #56

Merged
tmgbedu merged 4 commits into
mainfrom
fix/schema-alter-async-get-current-schema
May 17, 2026
Merged

fix: make get_current_schema async to fix schema.table() alter migrations#56
tmgbedu merged 4 commits into
mainfrom
fix/schema-alter-async-get-current-schema

Conversation

@tmgbedu

@tmgbedu tmgbedu commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • get_current_schema() in PostgresPlatform, MySQLPlatform, and SQLitePlatform was calling connection.query(sql, ()), but Connection.query() takes no arguments — it's a factory that returns a QueryBuilder
  • Any migration using async with await self.schema.table(...) to alter an existing table would crash with TypeError: Connection.query() takes 1 positional argument but 3 were given
  • Fixed by making get_current_schema async and using await connection.select(sql, ()) instead
  • Moved the schema fetch into Blueprint.__aexit__ so it runs asynchronously before to_sql() compiles the ALTER statements
  • Updated test mocks from conn.query = MagicMock(return_value=[]) to conn.select = AsyncMock(return_value=[])
  • Added regression test test_add_nullable_columns_without_from_table that reproduces the exact migration pattern that was failing

Reproduction

class AddCompanyProfileToClientsTable(Migration):
    async def up(self):
        async with await self.schema.table("clients") as table:
            table.text("website").nullable()
            table.text("linkedin_url").nullable()

Running this migration would raise:

TypeError: Connection.query() takes 1 positional argument but 3 were given

Test plan

  • uv run pytest fastapi_startkit/tests/masoniteorm/sqlite/schema/test_sqlite_schema_builder_alter.py -v — all 14 tests pass including new regression test

🤖 Generated with Claude Code

tmgbedu and others added 4 commits May 16, 2026 18:35
…ions

get_current_schema() in all platform files called connection.query(sql, ())
but Connection.query() takes no arguments — it returns a QueryBuilder factory.
Migrations using `async with await self.schema.table(...)` would fail with
TypeError: Connection.query() takes 1 positional argument but 3 were given.

Fix: make get_current_schema async and use await connection.select(sql, ())
instead. Move the schema fetch into Blueprint.__aexit__ so it runs async
before to_sql() compiles the ALTER statements.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Schema._type_hints_map was referenced in all three platform get_current_schema
methods but was never ported from the backup version of Schema. Add it back
so ALTER TABLE migrations can correctly map database column types to Python types.

Also add an alter migration fixture (add_body_to_posts_table) to exercise the
schema.table() path in the migrate command integration tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tmgbedu
tmgbedu merged commit 20481e9 into main May 17, 2026
0 of 3 checks passed
@tmgbedu
tmgbedu deleted the fix/schema-alter-async-get-current-schema branch May 18, 2026 09:01
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.

1 participant