|
1 | 1 | # FastApiArchitecture
|
2 | 2 | FastApi Project Architecture with database and alembic tools
|
| 3 | + |
| 4 | +Create virtual environment then activate to install requirements libraries from requirements.txt |
| 5 | + |
| 6 | +Project is set up in BaseApp folder. In this manager.py file have app configurations. |
| 7 | + |
| 8 | +### Run FastApi Server |
| 9 | +I am using uvicorn server to run app |
| 10 | + |
| 11 | +`uvicorn BaseApp.manager:app --reload` |
| 12 | + |
| 13 | +Database migrations using alembic. |
| 14 | + |
| 15 | +### Initialize alembic |
| 16 | +**db-migration** is folder name |
| 17 | + |
| 18 | +`alembic init db-migration` |
| 19 | + |
| 20 | +### Configure alembic for database and apps models |
| 21 | +Edit env.py file and change according to this repo env.py file. |
| 22 | +In this change Database url with environment variable and configure into mehod of run_migrations_offline and run_migrations_online. |
| 23 | + |
| 24 | +### Change database url in **alembic.ini** file also |
| 25 | + |
| 26 | +### Auto generate migration script |
| 27 | +import project app models all |
| 28 | +and import project database **Base** |
| 29 | +initialize it like this. |
| 30 | + |
| 31 | +`target_metadata = Base.metadata` |
| 32 | + |
| 33 | +Now we are connected with models for migrations. |
| 34 | + |
| 35 | +### Now generate migration script with this |
| 36 | +This will scan the models and generate upgrade & downgrade scripts. |
| 37 | + |
| 38 | +`alembic revision --autogenerate -m “message”` |
| 39 | + |
| 40 | +### Run migration into database |
| 41 | +This upgrade your model columns into database. |
| 42 | +### Run upgrade command to migrate models |
| 43 | +`alembic upgrade head` |
| 44 | + |
| 45 | +### Run downgrade command to drop models |
| 46 | +`alembic downgrade` |
| 47 | + |
| 48 | + |
0 commit comments