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

[WIP] SQLAlchemy 2.0 follow-up: misc. improvements #19417

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

jdavcs
Copy link
Member

@jdavcs jdavcs commented Jan 17, 2025

(Please replace this header with a description of your pull request. Please include BOTH what you did and why you made the changes. The "why" may simply be citing a relevant Galaxy issue.)
(If fixing a bug, please add any relevant error or traceback)
(For UI components, it is recommended to include screenshots or screencasts)

How to test the changes?

(Select all options that apply)

  • I've included appropriate automated tests.
  • This is a refactoring of components with existing test coverage.
  • Instructions for manual testing are as follows:
    1. [add testing steps and prerequisites here if you didn't write automated tests covering all your changes]

License

  • I agree to license these and all my past contributions to the core galaxy codebase under the MIT license.

@jdavcs jdavcs added kind/refactoring cleanup or refactoring of existing code, no functional changes area/database Galaxy's database or data access layer labels Jan 17, 2025
@jdavcs jdavcs added this to the 25.0 milestone Jan 17, 2025
@jdavcs jdavcs changed the title SQLAlchemy 2.0 follow-up: misc. improvements [WIP] SQLAlchemy 2.0 follow-up: misc. improvements Jan 18, 2025
@jdavcs
Copy link
Member Author

jdavcs commented Jan 18, 2025

We have 168 model classes most of which have the same id column. So, following SA docs, we factor out the common column into a mixin:

class HasId:
    id: Mapped[int] = mapped_column(primary_key=True)

class Foo(HasId):
   ...
   bar = relationship(..., remote_side=[id])

However, this will cause errors when that column is used to specify other properties in a class. In the example above, the id identifier will resolve to Python's builtin id whenever it is used to specify a relationship in Foo. So this won't work. I'll check if there are recommended best practices, and if not, we'll just have to stick with defining all columns in each class.

Another neat refactoring that doesn't always work. If we replace foo: Mapped[str] = mapped_column() with foo = Mapped[str], because that's sufficient for mapping and is recommended as per SA docs, we won't be able to use foo to specify things like column properties, because without mapped_function() it won't exists at that time. So this won't work:

first: Mapped[str] = mapped_column()
last: Mapped[str]
full: column_property(first + " " + last)  # last doesn't exist yet!

I'll check for more info on the above, but overall, I think, we should be cautious in applying SA 2.0 model definition syntax optimizations.

UPDATE: it seems to be as simple as using the callable form for foreign_keys and remote_side. Waiting for tests now.

@jdavcs jdavcs force-pushed the dev_sa20_followup2 branch from 8889d8e to 8f40a12 Compare January 24, 2025 00:50
The id attribute is used in the definition of the `update_time`
column_property.

Since id is inherited from a mixin, it is not available on the History class at mapping time.
The `remote_side` and `foreign_keys` parameters of the relationship
function face the same problem; however they can be defined as a
callable, which defers resolving the identifiers they contain, which
solves this problem. `column_property` does not accept a callable in
place of its argument, so the only way around is to define it after the
class has been defined - and we do so for several classes (see bottom of
the model/__init__ module. However, it appears to be impossible to use
this approach for the update_time attribute because it is also the name
of the database table column, which is defined inside the class
definition. Moving that column definition outside the class would add
unnecessary complexity: redefining the id column, I think, is the
reasonable approach here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/database Galaxy's database or data access layer kind/refactoring cleanup or refactoring of existing code, no functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant