Testing with legacy databases

I have run into an issue when running testing on legacy unmanaged databases. They fail to create the necessary tables. Research shows this is because they are unmanaged the test sequencer (probably wrong name) does not create the table. The “best” solution I can find is to temporarily switch all the tables to managed for the test, then switch back to unmanaged. These articles also come with health warnings about migrations and changes not being registered. I could be wrong, but this feels like a fudge?

I am using out of the box tests and my question is that before I head into this particular rabbit hole, is there a better option out there that is designed to work with legacy databases?

I’d avoid using Django’s database creation mechanism in tests, since it may not create the exact same schema as your legacy DB. Instead you could dump your schema, for example with mysqldump --no-data (or whatever the command/flag is for your db), commit it to your repo, and load that in a custom test runner setup_databases. For an example of a custom setup_databases see https://adamj.eu/tech/2020/06/11/avoid-hardcoding-ids-in-your-tests/ . If the legacy DB is getting updates, you’d want to take a copy of that schema regularly, so maybe wrap the mysqldump (or whatever) in a management command.

Thanks Adam, this looks interesting and will give it a go.