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

misc: Migrate to SQLAlchemy declarative models #954

Conversation

adamantike
Copy link
Collaborator

This change applies the guided migration process recommended by SQLAlchemy [1], up to step 4, to have declarative ORM models that better support Python typing.

The change was tested by running alembic check, which does not find any schema changes.

Errors reported by mypy go down to 170, from the original 223 in the current master commit.

[1] https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#migrating-an-existing-mapping

from sqlalchemy import BigInteger, DateTime, ForeignKey, String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

if TYPE_CHECKING:
Copy link
Member

Choose a reason for hiding this comment

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

AAAAHHH I forgot you can do this 😫



class Save(RomAsset):
__tablename__ = "saves"
__table_args__ = {"extend_existing": True}

emulator = Column(String(length=50), nullable=True)
emulator: Mapped[str | None] = mapped_column(String(length=50))
Copy link
Member

Choose a reason for hiding this comment

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

don't we have to mark this nullable=True?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't! The typehint takes care of that, as explained in step four, so Mapped[str] implicitly sets nullable=False, while Mapped[str | None] sets nullable=True.

This change applies the guided migration process recommended by
SQLAlchemy [1], up to step 4, to have declarative ORM models that better
support Python typing.

The change was tested by running `alembic check`, which does not find
any schema changes.

Errors reported by `mypy` go down to 170, from the original 223 in the
current `master` commit.

[1] https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#migrating-an-existing-mapping
@adamantike adamantike force-pushed the misc/migrate-to-sqlalchemy-declarative-models branch from 66ccd38 to b99eded Compare June 26, 2024 02:04
@gantoine
Copy link
Member

gantoine commented Jun 26, 2024

@adamantike If you happen to be in the discord, give me (arcaneasada) a ping in #open-development. And if not, feel free to join. We do all of our dev planning on there, and if you have more PRs in mind I'd love to hear what you have planned. https://discord.gg/ThzzNYq9

@gantoine gantoine merged commit 6f92a19 into rommapp:master Jun 26, 2024
3 checks passed
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.

2 participants