The ESAP API gateway has a (configuration) database per app (`query`, `batch`, `ida`), where at this moment the database that is associated with `query` app (`esap_config.sqlite3`) also contains tables for the whole project (like user information).
These databases are more like 'configuration files' than proper databases, because they mainly hold (static) configuration rather than dynamic (meta)data.
When this changes, for instance when dynamic data about 'jobs' or 'users' need to be stored, we may have to rethink the current mechanism.
For instance, we may want to store dynamic data in a Postgres database in a docker container instead of in the current binary file in a shared directory.
There is currently no mechanism in place to migrate the database that is in place, so if changes have been made then the database should be manually copied from your development to production.
Each app with a database must have a `database_router.py` to redirect the traffic to the correct database.
The order matters: the top router in the `DATABASE_ROUTER` dict is the router for the `query` app, whose database also still has some tables for the esap-gateway as a whole (this may change later).
So this first router, the `QueryRouter`, is configured to handle traffic to the 'default' `esap_config.sqlite3` database for the following
The 'esap_config.sqlite3' is a special case because it is the original default database that also contains Django administrative information. This is how the migrate command for that database looks:
After the migration, the database needs to be copied back to where it came from just before the new version is deployed. (**warning**: Don't forget to commit the new migration file that appeared the 'migrations' directory)
Because of the static nature of these configuration databases, there may not be any changes in the original content.
If you are not sure about that, or if the content has indeed changed, then it is better to re-apply the migration step on (a copy of) the database from production just before or after deployment.
See the Django documentation for more detailed documentation: https://docs.djangoproject.com/en/3.1/topics/migrations/
This is an example for a deployed ESAP in a production environment. For a development environment, or a deployment outside Docker, the commands need to be changed accordingly.
**Structural** changes require a database migration (based on the committed migration files).
If this is done for a **static** database (`ida` or `query`) then a new YAML fixture file should be created with the `dumpdata` command and then committed.
Example of a `dumpdata` command to create a YAML fixture for the `config` database:
##### Q: django.db.utils.IntegrityError: The row in table 'django_admin_log' with primary key '1' has an invalid foreign key
A: the contents of the `esap_config.users` table and `esap_config.django_admin_log` table went out of sync, probably because users were manually removed before.
Clear all records from the `esap_config.django_admin_log` table (for example with the [DB Browser for Sqlite](https://sqlitebrowser.org/) tool)