-
Notifications
You must be signed in to change notification settings - Fork 5
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
Database migrations #48
Comments
To my understanding we removed it until the schema becomes somewhat stable. Then it has to be re-introduced. |
Pretty much. Migrations are implemented, but it seems like half the time they don't work properly and won't allow the application to start (at least with sqlite). So the idea is to try to stabilize the schema as much as possible so hopefully they actually work when small changes are needed. |
We should fix this for the next release. It is a major issue for our setup. |
I'm not sure if that's because we don't have migrations in git, or because migrations get generated locally and applied on startup. For me, I end up having to delete them every so often because they stop the app from starting at all. Seems like mostly an issue with SQLite not having some functionality for migrations, and aerich not being completely fully implemented yet... |
With my Docker image, built with
Manually removing the
Having migration files as part of the package and preventing them from being generated at start-up seems to me like the way to go. Or is that not how aerich is supposed to be used? |
Not sure. My goal would be that when we make changes to the DB, the user updates goosebit, then runs it, and it generates migrations which then get applied, as each user's migrations in theory could be different. |
I haven't had time to look into Besides the initial migrations, they only need to be created when models (actually the DB schema) are modified and they should be written in a DB-agnostic manner. So, it should be possible to ship them with the package and provide a way for smooth DB migrations on updates. Other frameworks work the same, so I would really assume that this is the case here too. If the files in the |
For what it's worth, this is what ChatGPT spits out when asked "Can I get an example of a migration for an edge case that works with PostgreSQL, MySQL and SQLite?": from tortoise import BaseDBAsyncClient
async def upgrade(db: BaseDBAsyncClient) -> None:
# Determine the database type
dialect = db.schema_generator.client.capabilities.dialect
if dialect == "postgres":
# PostgreSQL: Use native JSONB type
await db.execute_script("""
ALTER TABLE your_table_name
ADD COLUMN json_data JSONB;
""")
elif dialect == "mysql":
# MySQL: Use native JSON type
await db.execute_script("""
ALTER TABLE your_table_name
ADD COLUMN json_data JSON;
""")
else:
# SQLite: Use TEXT to store JSON data
await db.execute_script("""
ALTER TABLE your_table_name
ADD COLUMN json_data TEXT;
""")
async def downgrade(db: BaseDBAsyncClient) -> None:
# Drop the JSON field during downgrade
await db.execute_script("""
ALTER TABLE your_table_name
DROP COLUMN json_data;
""") Of course, I have no idea if this is correct. |
I found an interesting project that can create app templates for fastapi / tortoise orm / aerich (and more) projects: /~https://github.com/s3rius/FastAPI-template Will rework our migrations based on this popular sample. Also think it can give other insights concerning best practices in this space. So worthwhile to have a look at IMHO. |
goosebit/.gitignore
Line 168 in 21975c0
Shouldn't migrations be committed? Am I missing something here?
Unfortunately, the commit message of 6e7c328 is not very informative 😞
The text was updated successfully, but these errors were encountered: