Database

Overview

The database used in this project is postgreSQL

Connection

If docker-compose is running

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

docker-compose -f docker-compose.prod.yml  exec web python manage.py graph_models -a -o static-apps/img/EMERGEN-DB_models.png

Result

UML

Create table

To create a table in the database, we use the Django ORM. In each application of the Django project (home, collector, stats and api) you will find a models.py file. The declaration of the tables is done in these files.

Then you have to translate these tables (makemigrations) and migrate the tables into the database.

Avertissement

Docker and EMERGEN-DB must always be switched on !

docker-compose exec web python manage.py makemigrations
docker-compose exec web python manage.py migrate

Clear database (danger area)

Avertissement

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 :

Avertissement

Did you read the warning below correctly?

# 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