To follow best practices for running Alembic migrations on two separate PostgreSQL databases while using SQLModel
, you should:
- Assign Models to Specific Databases: Define separate
metadata
objects to distinguish models belonging to each database. - Use Separate Migration Folders: Store migration scripts in distinct directories to avoid conflicts and keep each database's schema changes independent.
- Modify
env.py
to Handle Multiple Databases: Configure Alembic to run migrations sequentially for both databases by looping through their configurations and applying changes to the correct metadata. - Ensure Autogeneration Works Properly: Implement logic in
env.py
to apply--autogenerate
migrations to the correct metadata. - Use Async and Sync Engines Correctly: Handle async database connections properly in the Alembic migration flow.
I'll prepare a detailed setup with env.py
modifications, migration folder structures, and best practices to ensure Alembic knows which models