Django detect and rollback unintentional database tables change
For sharing purpose, I accidentally modified my database tables. This is my ‘semi-automatic’ way to detect and rollback the changes.
Please share if you have better detection and rollback methods
Context
- I give django full control over my database (not recommended)
- I do it in ‘strict’ mode, database tables should be exactly as defined in
models.py
- no manual change allow
- no other web framework / software change allow
Detect
-
./manage.py inspectdb
→ this will introspects the database tables in the given database and outputs a Django model module - manually compare the output with the
models.py
I am not satisfy with this manual inspection, probably can write some logic to automatically compare the output with models.py
Rollback
- export the affected table(s) data 1st
-
./manage.py migrate thatchangedapp 0xxx
→ rollback -
./manage.py migrate
→ apply migration - import the data programmatically, with the necessary data transformation