I would like to automatically generate some sort of log of all the database changes that are made via the Django shell in the production environment.
We use schema and data migration scripts to alter the production database and they are version controlled. Therefore if we introduce a bug, it's easy to track it back. But if a developer in the team changes the database via the Django shell which then introduces an issue, at the moment we can only hope that they remember what they did or/and we can find their commands in the Python shell history.
Example. Let's imagine that the following code was executed by a developer in the team via the Python shell:
>>> tm = TeamMembership.objects.get(person=alice)
>>> tm.end_date = date(2022,1,1)
>>> tm.save()
It changes a team membership object in the database. I would like to log this somehow.
I'm aware that there are a bunch of Django packages related to audit logging, but I'm only interested in the changes that are triggered from the Django shell, and I want to log the Python code that updated the data.
So the questions I have in mind:
- I can log the statements from IPython but how do I know which one touched the database?
- I can listen to the
pre_savesignal for all model to know if data changes, but how do I know if the source was from the Python shell? How do I know what was the original Python statement?
This solution logs all commands in the session if any database changes were made.
How to detect database changes
Wrap
execute_sqlofSQLInsertCompiler,SQLUpdateCompilerandSQLDeleteCompiler.SQLDeleteCompiler.execute_sqlreturns a cursor wrapper.How to log commands made via the Django shell
atexit.register()an exit handler that doesreadline.write_history_file().IPython
Check whether IPython was used by comparing
HistoryAccessor.get_last_session_id().Put it all together
Add the following in manage.py before
execute_from_command_line(sys.argv).