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

Alembic integration and documentation #689

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

Jufik
Copy link

@Jufik Jufik commented Oct 31, 2023

This is an to integrates Alembic within SqlModel (mentioned in #654)

Implementation is pretty straight forward and lays fundation to provide an extendible sqlmodel CLI through sqlmodel entrypoints. (though about Pluggy be felt like an overkill).

Current CLI structure is:

── sqlmodel
    └── migrations
        ├── init
        ├── revision
        ├── show
        ├── upgrade
        └── downgrade

Command are typed wrappers for the original alembic.command functions, exposed through Typer.
Usage and Documentation can be found in the advanced section.

More that glad to get any feedback or suggestions for improvements!

Copy link

📝 Docs preview for commit 32afa19 at: https://50871b3d.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit 449882e at: https://f87a6f0c.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit 0083a16 at: https://0f49201d.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit b5944f0 at: https://e4e3fa97.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit cc42afb at: https://c12e3a89.sqlmodel.pages.dev

Copy link

📝 Docs preview for commit 1eb82af at: https://ebd5efa7.sqlmodel.pages.dev

@Jufik
Copy link
Author

Jufik commented Oct 31, 2023

Todo:

  • Test Alembic methods / migrations for both templates.

One missing piece is around testing Alembic it self.
Glad to get any guidance on that.

Looking forward:

I am planning support some method called inside templates' env.py.

Main idea is to get the model from the config's alembic:target_metadata, pass it to a sqlmodel that would dynamically load the metadata.

I'd like some community feedback on such an idea.

@antont
Copy link

antont commented Nov 1, 2023

Seems like great work with the template and docs to me.

We've used the Alembic CLI for using Alembic with SQLModel, what benefits does this CLI offer? I'm not sure if it's useful to wrap Alembic to own CLI if it just passes on the calls to be done the same way as the upstream Alembic CLI does. But I did not read carefully what happens there, maybe it's helpful.

@Jufik
Copy link
Author

Jufik commented Nov 1, 2023

Hey @antont,

At the moment not that much TBH 😅
Main idea is to provide the CLI fundations, but we could easily expand it further.
That moves FastApi/SqlModel one step closer to Django, and that opens few questions:

  • Does this belong here ? Tiangolo seems to have a vision on this, and IDK how much this should be tied to FastAPI.
  • Where is the limit, how simple should it be easy for developpers, with which restrictions ? (mostly: should cross-apps migration dependencies be handled ?)
  • How opinionated should it be ?

sqlalchemy2-stubs = {version = "*", allow-prereleases = true}
sqlalchemy2-stubs = { version = "*", allow-prereleases = true }
typer = "^0.9.0"
alembic = "^1.12.0"
Copy link

Choose a reason for hiding this comment

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

I feel that adding a dependency needs a formal discussion.

On one hand, alembic is pretty lean, seems to only depend on sqlalchemy which this package already depends on too.

On the other, there are bound to be users that don't generate or apply migrations, rather consume some existing database instead. These users get a bit of bloat, and possibly a restriction on sqlalchemy versions they can use in their application.

This specific library addition may have been carefully thought through and may be OK.

A random library addition would be a problem.

Thus, I'd love to see a discussion and maintainer vote/rationale.

Copy link

@antont antont Feb 22, 2024

Choose a reason for hiding this comment

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

As I mentioned above, we used alembic happily with sqlmodel, so maybe it's better indeed for sqlmodel to not depend on it, but just have things so that alembic can be used for it .. like it already can be.

Tiangolo does have this in the roadmap there so maybe he also has ideas for a tighter integration. I guess it could be also in an extra package, like sqlmodel-migrations, to avoid a runtime dep.

Choose a reason for hiding this comment

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

Maybe it should be a separate package, just like alembic is separated from sqlalchemy. That would also allow this to be released more easily and have people play with it, test it. Have you thought about releasing it @Jufik ?

@AmgadHasan
Copy link

Hey!
Will this be merged soon?

@hitchcock-william
Copy link

@tiangolo documentation in the advanced docs on migrations as well as the incorporation of something like this would be incredibly useful

@alejsdev alejsdev added docs Improvements or additions to documentation feature New feature or request labels Jul 12, 2024
@alessiamarcolini
Copy link

Hi @tiangolo is there an update on when this can be merged? 🙏 thank you so much for your work! ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants