QueryRouter design is not clear
Not sure if this is an issue or not.
The design intention of the QueryRouter
database router is not clear.
https://git.astron.nl/astron-sdc/esap-api-gateway/-/blob/master/esap/query/database_router.py
From what I understand from the the DJango documentation and experimenting with different configurations. Django will try each database router listed in the DATABASE_ROUTERS
array and use the first non-null response that it gets.
If a router matches a query it returns a result, the database name for db_for_read()
and db_for_write()
, or [True|False]
for allow_relation()
and allow_migrate()
. If a router doesn't match a query, it should return None
, allowing Django to move on to the next router in the list.
The code for QueryRouter always returns a result for db_for_read()
and db_for_write()
This means that any routers listed after it in the DATABASE_ROUTERS
array will never be used.
https://git.astron.nl/astron-sdc/esap-api-gateway/-/blob/master/esap/esap/settings/dev.py#L24-L31
The code for QueryRouter matchs requests for batch
, ida
, rucio
, and accounts
and routes them to the corresponding database.
Anything else is routed to the default
database.
The result is that when adding new database router for a new app it must be added before the QueryRouter
entry in DATABASE_ROUTERS
or it will be ignored.
If this is by design, it would be useful to document it in the source code, and we should probably remove the unused accounts
, ida
, batch
, and rucio
routers.