Fastapi
- instaall pip library with pipenv
$ pipenv --python 3.10
$ pipenv install --three fastapi fastapi-sqlalchemy sqlalchemy_utils mysqlclient pytest inject pydantic alembic uvicorn sqlalchemy
$ pipenv requirements > docker/api/requirements.txt
$ cd db
$ alembic init migrations
-> create maigration directory
- Please edit file of "alembic.ini"
"Comment out below code"
sqlalchemy.url = driver://user:pass@localhost/dbname
Edit "migrations/env.py"
quote -> "db/samples/env.py"
Please edit env.py while referring to sample/env.py
and edit parts are belows.
-- added code here --
-- changed code here --
cp samples/seed.py ./seed.py
$ docker network create fastapi_network
$ docker compose build
$ docker compose up
- try connect http://localhost:8888/docs
# enter api container
$ docker compose run api bash
or
$ docker ps
$ docker exec -it XXX bash
(*)xxx: container id
$ cd /usr/src/app/db
# create maigration file
$ alembic revision --autogenerate -m "create users, tasks"
-> db/migrations/versions/~~~_comment
# maigrate
$ alembic upgrade head
!!! run on docker in api container
# enter api container
$ cd /usr/src/app/db
$ python seed.py
- try connect http://localhost:8888/docs
$ docker-compose run api bash
$ cd /usr/src/app/db
> mysql -h"$MYSQL_HOST" -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"
> use test_db
- stop and rm container
$ docker ccompose down - only stop container
$ docker ccompose stop
- the first migrate
$ alembic upgrade head - step by step upgrade
$ alembic upgrade +1 - downgrade
$ alembic downgrade -1
if you remake migration file, delete migration file - all reset
$ alembic downgrade base