Skip to content

Refactor: Change the DeclarativeBase sqlalchemy style#748

Merged
aldbr merged 8 commits intoDIRACGrid:mainfrom
HeloiseJoffe:refactor/sqlalchemy-DeclarativeBase
Feb 11, 2026
Merged

Refactor: Change the DeclarativeBase sqlalchemy style#748
aldbr merged 8 commits intoDIRACGrid:mainfrom
HeloiseJoffe:refactor/sqlalchemy-DeclarativeBase

Conversation

@HeloiseJoffe
Copy link
Contributor

closed: #612

Changes:

  • Replaced declarative_base with DeclarativeBase style
  • Cast 'table' to Table type to satisfy mypy

Questions:
There is other new styles in SQLAlchemy, for example the declarative use of Column is replaced by mapped_column.
Should this be applied as well?
In the Bookkeeping schema, this has been done, but the other changes haven’t been applied. Is there a reason for that?

@ryuwd
Copy link
Contributor

ryuwd commented Jan 23, 2026

All the steps under https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#migrating-an-existing-mapping should be followed before closing #612

substituting mapped_column is enough to start fixing the typing errors but we lose the usefulness of the type checks without the additional annotations (which I still need to do for the bookkeeping schema)

Copy link
Contributor

@ryuwd ryuwd left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Next steps:

  1. Column to mapped_column (see diff comment example in job_logging/schema.py)
  2. Add Mapped[...] annotations for all schema files
  3. Remove cast(__table__, Table) everywhere

@HeloiseJoffe HeloiseJoffe force-pushed the refactor/sqlalchemy-DeclarativeBase branch from 7c3dfb0 to c258093 Compare February 3, 2026 09:13
@read-the-docs-community
Copy link

read-the-docs-community bot commented Feb 3, 2026

Documentation build overview

📚 diracx | 🛠️ Build #31365623 | 📁 Comparing a7e2ada against latest (60e5846)


🔍 Preview build

Show files changed (2 files in total): 📝 2 modified | ➕ 0 added | ➖ 0 deleted
File Status
dev/reference/coding-conventions/index.html 📝 modified
dev/explanations/components/db/index.html 📝 modified

@HeloiseJoffe HeloiseJoffe requested a review from ryuwd February 3, 2026 13:06
Copy link
Contributor

@ryuwd ryuwd left a comment

Choose a reason for hiding this comment

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

This is looking great! I only have small questions remaining


Column: partial[RawColumn] = partial(RawColumn, nullable=False)
NullColumn: partial[RawColumn] = partial(RawColumn, nullable=True)
DateNowColumn = partial(Column, type_=DateTime(timezone=True), server_default=utcnow())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can all of these be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept them for compatibility reasons, in case other extensions or modules use them. Once it's confirmed that no other code uses them, they can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would you be happy to check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These helpers aren’t used by diracx itself, but I can’t tell whether external or private extensions rely on them. Should I remove them anyway?

Copy link
Contributor

@ryuwd ryuwd Feb 9, 2026

Choose a reason for hiding this comment

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

Yes, please remove them. Extensions should update to be consistent with us.

Copy link
Contributor

Choose a reason for hiding this comment

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

Theoretically we should avoid to remove them in one go because there are explicitly part of the public API that extensions can use (see https://github.com/DIRACGrid/diracx/pull/748/changes#diff-f2cfd457649455e1dbe7583fcd08cbd9524ed972d423b420d10a526b5083dd7eL6-L8). Extensions already relying on it could then be broken.
It's a breaking change, so ideally, it should be first deprecated and then removed in a major release.

In practice, there are not a lot of extensions in production (only LHCb? May be CTAO?).
As a good practice, you can first add a deprecation warning if one tries to use one of these types, open an issue to explain that you want to remove them. Then we can briefly discuss about that during a Dops meeting, just to make sure no one is using them. And then make another PR based on the issue you opened to remove them.

Copy link
Contributor

Choose a reason for hiding this comment

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

(The process is heavy, and this is why we should be very careful in what we want to make public through the __all__ keywords)

@HeloiseJoffe HeloiseJoffe force-pushed the refactor/sqlalchemy-DeclarativeBase branch 2 times, most recently from 87d433c to 09ba1cc Compare February 5, 2026 10:20
@aldbr aldbr force-pushed the refactor/sqlalchemy-DeclarativeBase branch 2 times, most recently from 88dfb6e to 559a912 Compare February 6, 2026 10:25
@aldbr aldbr requested a review from ryuwd February 6, 2026 10:25
Copy link
Contributor

@ryuwd ryuwd left a comment

Choose a reason for hiding this comment

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

Thank you for the hard work! I'm happy with how you've migrated the tables, there are just a couple more old utility functions we should try to get rid of if possible (see comments)

A TODO for the future - we should keep track with tests and test fixtures how these schema definitions change the DB schema expected by SQLAlchemy

@HeloiseJoffe HeloiseJoffe requested a review from ryuwd February 9, 2026 14:11
@HeloiseJoffe HeloiseJoffe force-pushed the refactor/sqlalchemy-DeclarativeBase branch from 559a912 to 8868d09 Compare February 10, 2026 09:43
@HeloiseJoffe HeloiseJoffe requested a review from aldbr February 10, 2026 10:31
@HeloiseJoffe
Copy link
Contributor Author

I checked the schema changes using a script that extracts the DDL from the SQLAlchemy metadata.
I generated the DDL on the main branch and on this PR branch, then compared the two outputs.
There was no structural difference.

@aldbr aldbr force-pushed the refactor/sqlalchemy-DeclarativeBase branch from 8868d09 to 61fe079 Compare February 10, 2026 14:23
@ryuwd
Copy link
Contributor

ryuwd commented Feb 10, 2026

LGTM !

Copy link
Contributor

@aldbr aldbr left a comment

Choose a reason for hiding this comment

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

I just have one last minor comment. The issue was initially opened for that specific comment: #608 (comment)

Basically to change Any in search and summary I guess:

self, table: Any, group_by: list[str], search: list[SearchSpec]

Do you think we can have something better now?

@aldbr aldbr linked an issue Feb 11, 2026 that may be closed by this pull request
@aldbr
Copy link
Contributor

aldbr commented Feb 11, 2026

Very last comment, sorry 😅
While I was going through the open issues, I noticed that #433 was very similar but contains a few additional info about type ignore and mypy.
Could you check that please?

@HeloiseJoffe HeloiseJoffe force-pushed the refactor/sqlalchemy-DeclarativeBase branch from 61fe079 to 69868b4 Compare February 11, 2026 09:51
@aldbr aldbr enabled auto-merge (squash) February 11, 2026 10:18
@aldbr aldbr merged commit 6152cbf into DIRACGrid:main Feb 11, 2026
28 checks passed
@HeloiseJoffe HeloiseJoffe deleted the refactor/sqlalchemy-DeclarativeBase branch February 13, 2026 07:25
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.

Use the new-style sqlalchemy DeclarativeBase SQLAlchemy typing and drop deprecated mypy plugin

3 participants