*******************************************************************************
Database
*******************************************************************************
Overview
===============================================================================
The database used in this project is `postgreSQL `_
Connection
===============================================================================
*If docker-compose is running*
.. code-block:: bash
docker exec -f docker-compose.prod.yml -it emergen-db_db_1 psql -U postgres -W postgres
UML schemas
===============================================================================
To generate UML schema in png and store in `static-apps/img/` (the image is thus usable in the application).
We use `django-extensions `_ , `pydotplus `_
and `graphviz `_ .
Note : *Docker-compose must be running*
.. code-block:: bash
docker-compose -f docker-compose.prod.yml exec web python manage.py graph_models -a -o static-apps/img/EMERGEN-DB_models.png
**Result**
.. image:: https://gitlab.com/ifb-elixirfr/covid19/EMERGEN-DB/-/raw/master/static-apps/img/EMERGEN-DB_models.png
:width: 100 %
:alt: UML
Create table
===============================================================================
To create a table in the database, we use the Django ORM. In each application of
the Django project (:code:`home`, :code:`collector`, :code:`stats` and :code:`api`)
you will find a :code:`models.py` file. The declaration of the tables is done in these files.
Then you have to translate these tables (:code:`makemigrations`) and migrate the tables into the database.
.. warning::
Docker and EMERGEN-DB must always be switched on !
.. code-block:: bash
docker-compose exec web python manage.py makemigrations
docker-compose exec web python manage.py migrate
Clear database (danger area)
===============================================================================
.. warning::
Be careful, these actions are final and all data will be permanently lost !
If you want to delete the table and all the "translations" to make a clean database,
for example when fields of a table are updated, you can use the following commands :
.. warning::
Did you read the warning below correctly?
.. code-block:: bash
# Stop application
docker-compose down # if EMERGEN-db is running
# Delete database
docker-compose rm db
# Remove migrations django files
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
# Start application
docker-compose up
# Make migration
docker-compose exec web python manage.py makemigrations
docker-compose exec web python manage.py migrate
# Populate database
docker-compose exec web python manage.py load_region
docker-compose exec web python manage.py load_departement
docker-compose exec web python manage.py load_description