FastApi Project Architecture with database and alembic tools
Create virtual environment then activate to install requirements libraries from requirements.txt
Project is set up in BaseApp folder. In this manager.py file have app configurations.
To run and configure with your local details only change in .env file. For database migration we need to change same url of database in .env and also in alembic.ini file.
I am using uvicorn server to run app
uvicorn BaseApp.manager:app --reload
To run docker container
docker compose up
After docker app and database container started migrate our models to docker container database
Start app container shell
docker exec -i <Container-ID> sh
Execute alembic migration command
alembic upgrade head
db-migration is folder name
alembic init db-migration
Edit env.py file and change according to this repo env.py file. In this change Database url with environment variable and configure into mehod of run_migrations_offline and run_migrations_online.
Change database url in alembic.ini file also
import project app models all and import project database Base initialize it like this.
target_metadata = Base.metadata
Now we are connected with models for migrations.
This will scan the models and generate upgrade & downgrade scripts.
alembic revision --autogenerate -m “message”
This upgrade your model columns into database.
alembic upgrade head
alembic downgrade