-
-
Notifications
You must be signed in to change notification settings - Fork 301
Fix MySQL native ENUM autogenerate detection #1746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix MySQL native ENUM autogenerate detection #1746
Conversation
This commit addresses a critical bug where Alembic's autogenerate feature fails to detect when values are added to, removed from, or reordered in MySQL native ENUM columns. Changes: - Override compare_type() in MySQLImpl to properly compare ENUM values - Add comprehensive test suite for ENUM value changes (addition, removal, reordering) - Import MySQL ENUM type for proper type checking The bug was caused by the base implementation only comparing ENUM type names but not the actual enum values. This resulted in silent schema mismatches where migrations would run successfully but fail to update the database schema, leading to runtime errors when the application attempted to use new ENUM values. Fixes: sqlalchemy#1745 Test coverage: - test_enum_value_added: Verifies detection of new ENUM values - test_enum_value_removed: Verifies detection of removed ENUM values - test_enum_value_reordered: Verifies detection of reordered ENUM values - test_enum_no_change: Ensures identical ENUMs are not flagged - test_mysql_enum_dialect_type: Tests MySQL-specific ENUM type
zzzeek
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi and thanks for this.
Here are the changes I'd like to see:
- add "Fixes: #779" to the commit message and remove #1745 which is a dupe
- remove all the unnecessary isinstance/conditionals from the compare_type implementation
- replace the entire test suite here with a single suite in test_mysql.py that is adapted from https://github.com/sqlalchemy/alembic/blob/dcba64415a3ef898078f04890712add7a8d18789/tests/test_autogen_diffs.py#L728C5-L770C1
thanks!
Per reviewer feedback, simplified the compare_type() method and restructured tests to follow existing patterns. Changes: - Removed unnecessary MySQL_ENUM import from alembic/ddl/mysql.py - Simplified compare_type() method (removed redundant isinstance, hasattr checks, and elif blocks for MySQL_ENUM) - Since MySQL_ENUM is already a subclass of sqltypes.Enum, we only need to check for sqltypes.Enum - Moved tests from separate test_mysql_enum.py into test_mysql.py - Restructured tests to follow the @Combinations pattern from test_autogen_diffs.py (lines 728-770) - Removed setUp/tearDown in favor of @Combinations.fixture() Fixes: sqlalchemy#779
|
Thank you for the detailed feedback! I've addressed all the review comments: Changes Made:1. Simplified
|
|
ok thanks for the changes |
|
What a coincidence… I ran into this same issue on Friday as well. |
Description
This PR fixes a critical bug where Alembic's
--autogeneratefeature fails to detect when values are added to, removed from, or reordered in MySQL native ENUM columns.Problem
Currently, when you modify a MySQL native ENUM column in your model:
Running
alembic revision --autogenerateeither:This causes silent schema mismatches leading to runtime errors when the application tries to use the new ENUM value.
Solution
This PR overrides the
compare_type()method inMySQLImplto:sqlalchemy.types.Enumandmysql.ENUM)Changes
Core Fix (
alembic/ddl/mysql.py):compare_type()method toMySQLImplclassMySQL_ENUMfor proper type detectionTest Suite (
tests/test_mysql_enum.py):@combinations(("backend",))for actual database testingImport Updates (
tests/test_mysql.py):Checklist
This pull request is:
Fixes: #1745Code Quality
All checks passing:
Impact
Testing
To test locally:
Related
Closes #1745
Thank you for reviewing! This fix addresses a production-critical issue that has affected many Alembic users with MySQL native ENUMs.