Skip to content
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

Avoid to use features that will be removed in SQLAlchemy v2.0 #4304

Merged
merged 7 commits into from Jan 11, 2023

Conversation

c-bata
Copy link
Member

@c-bata c-bata commented Jan 5, 2023

Motivation

CuPy team reported that Optuna depends on some SQLAlchemy's features that will be removed in the SQLAlchemy 2.0 release.
cupy/cupy#7276

Description of the changes

Fix warnings of sqlalchemy.exc.RemovedIn20Warning. We can detect them like:

$ SQLALCHEMY_WARN_20=1 pytest tests/storages_tests -W error::sqlalchemy.exc.RemovedIn20Warning

@github-actions github-actions bot added the optuna.storages Related to the `optuna.storages` submodule. This is automatically labeled by github-actions. label Jan 5, 2023
@c-bata c-bata added this to the v3.1.0 milestone Jan 5, 2023
@c-bata
Copy link
Member Author

c-bata commented Jan 5, 2023

Let me set this PR's milestone v3.1.0 since SQLAlchemy v2.0 will be released in January.

Version 2.0.0rc1 is hoped to be very close to the final 2.0.0 release which is anticipated in January. A few additional changes have been added since the 2.0.0b4 release, mostly in the area of Session behavior.
https://www.sqlalchemy.org/blog/#sqlalchemy-2.0.0rc1-released

@c-bata c-bata marked this pull request as ready for review January 5, 2023 08:33
@codecov-commenter
Copy link

Codecov Report

Merging #4304 (65abd8a) into master (f2945b4) will decrease coverage by 0.23%.
The diff coverage is 57.57%.

@@            Coverage Diff             @@
##           master    #4304      +/-   ##
==========================================
- Coverage   89.77%   89.53%   -0.24%     
==========================================
  Files         170      170              
  Lines       13265    13389     +124     
==========================================
+ Hits        11908    11988      +80     
- Misses       1357     1401      +44     
Impacted Files Coverage Δ
optuna/storages/_rdb/alembic/versions/v1.3.0.a.py 65.51% <50.00%> (-1.76%) ⬇️
optuna/storages/_rdb/alembic/versions/v2.4.0.a.py 90.52% <50.00%> (-1.87%) ⬇️
optuna/storages/_rdb/alembic/versions/v3.0.0.a.py 68.06% <50.00%> (-0.90%) ⬇️
optuna/storages/_rdb/alembic/versions/v3.0.0.b.py 83.33% <50.00%> (-2.95%) ⬇️
optuna/storages/_rdb/alembic/versions/v3.0.0.c.py 53.26% <50.00%> (-0.68%) ⬇️
optuna/storages/_rdb/alembic/versions/v3.0.0.d.py 51.00% <50.00%> (-0.55%) ⬇️
optuna/storages/_rdb/models.py 97.68% <50.00%> (-0.65%) ⬇️
optuna/storages/_rdb/storage.py 93.90% <100.00%> (+0.02%) ⬆️
optuna/trial/_fixed.py 89.69% <0.00%> (-5.19%) ⬇️
optuna/trial/_frozen.py 94.76% <0.00%> (-2.63%) ⬇️
... and 4 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Comment on lines +28 to +32
try:
from sqlalchemy.orm import declarative_base
except ImportError:
# TODO(c-bata): Remove this after dropping support for SQLAlchemy v1.3 or prior.
from sqlalchemy.ext.declarative import declarative_base
Copy link
Member Author

@c-bata c-bata Jan 5, 2023

Choose a reason for hiding this comment

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

optuna/storages/_rdb/models.py:40
/Users/c-bata/go/src/github.com/optuna/optuna/optuna/storages/_rdb/models.py:40: MovedIn20Warning: The declarative_base() function is now available as sqlalchemy.orm.declarative_base(). (deprecated since: 1.4) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
BaseModel: Any = declarative_base()

Comment on lines +1101 to +1105
connection = self.engine.connect()
context = alembic_migration.MigrationContext.configure(connection)
with connection.begin():
script = self._create_alembic_script()
context.stamp(script, revision)
Copy link
Member Author

@c-bata c-bata Jan 5, 2023

Choose a reason for hiding this comment

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

tests/storages_tests/rdb_tests/test_storage.py: 11 warnings
/Users/c-bata/go/src/github.com/optuna/optuna/optuna/storages/_rdb/storage.py:1103: RemovedIn20Warning: The current statement is being autocommitted using implicit autocommit, which will be removed in SQLAlchemy 2.0. Use the .begin() method of Engine or Connection in order to use an explicit transaction for DML and DDL statements. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
context.stamp(script, revision)

Copy link
Member

Choose a reason for hiding this comment

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

[Note] I confirmed that this logic worked with the following test case locally.

# TODO(ohta): Remove the following comment out when the second revision is introduced.
# with pytest.raises(RuntimeError):
# storage._set_alembic_revision(storage._version_manager._get_base_version())
# storage._check_table_schema_compatibility()

Maybe we can activate it in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

@toshihikoyanase toshihikoyanase self-assigned this Jan 6, 2023
@toshihikoyanase toshihikoyanase added the code-fix Change that does not change the behavior, such as code refactoring. label Jan 6, 2023
@c-bata
Copy link
Member Author

c-bata commented Jan 10, 2023

FYI: I pushed a temporary change to run tests with sqlalchemy 2.0.0rc2 at e57e151.

Copy link
Member

@contramundum53 contramundum53 left a comment

Choose a reason for hiding this comment

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

LGTM!

@c-bata
Copy link
Member Author

c-bata commented Jan 10, 2023

Thank you for your swift review. I'll revert e57e151 after reviewed by yanase-san 🙇

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

By using optuna-e2e, I confirmed that the change in the migration scripts worked with SQLAlchemy==[1.3.0, 1.4.46, 2.0.0rc2]. (Actually mssql did not worked with SQLAlchemy==1.3.0, but I don't think it is related to this PR.) LGTM!

@c-bata could you revert the tentative change in the CI settings?

Comment on lines +1101 to +1105
connection = self.engine.connect()
context = alembic_migration.MigrationContext.configure(connection)
with connection.begin():
script = self._create_alembic_script()
context.stamp(script, revision)
Copy link
Member

Choose a reason for hiding this comment

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

[Note] I confirmed that this logic worked with the following test case locally.

# TODO(ohta): Remove the following comment out when the second revision is introduced.
# with pytest.raises(RuntimeError):
# storage._set_alembic_revision(storage._version_manager._get_base_version())
# storage._check_table_schema_compatibility()

Maybe we can activate it in a follow-up PR.

@c-bata
Copy link
Member Author

c-bata commented Jan 11, 2023

@toshihikoyanase Thank you for your review! I've just pushed 3d20af1 to revert the tentative change.

@toshihikoyanase
Copy link
Member

I confirmed that the CI settings were reverted. Let me merge this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-fix Change that does not change the behavior, such as code refactoring. optuna.storages Related to the `optuna.storages` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants