开发者

Using Django Test to Analyse and Repair Data of a Database

开发者 https://www.devze.com 2023-03-25 18:02 出处:网络
I had an idea and before implementing I would like to throw it out there to get some feedback or even discover that somebody has already done.

I had an idea and before implementing I would like to throw it out there to get some feedback or even discover that somebody has already done.

Here is the thing: I have a website running with a database that is growing quite rapidly and over the months, and over the开发者_JAVA百科 several issues that took place, got filled with a good amount of garbage. I was thinking in put-up some scripts to run across the database and clean up the mess. So, my idea was to use Django Tests, in a way that one would write a large sum of small simple tests that would do exactly that, except that instead of raising a flag, would actually fix things.

What do you guys think? I can't think in any reason why this wouldn't work. But I'm not that seasoned in Django. Would it be hard? Any foreseeable issue?

Thanks!


No, this is a bad idea for many reasons, starting from the design perspective to implementation issues. Just to mention a few:

  1. Tests do not run on the actual database. A separate database is created from scratch for it. You'd have to hack this.
  2. Normally each test case that touches the db, runs in a transaction and then it's rolled back. So in the end the DB has not changed at all. Ofc you can also avoid this, but that's not the point.
  3. Tests are supposed to be run all the time when you change something. But the kind of issues you are talking about should be one-time fix only (mostly).

But there is a very simple and appropriate solution for what you want to do:

  1. Install South
  2. Fix bug that causes incorrect/unwanted data.
  3. Write data migration that fixes/cleans up the already existing "broken" data.
  4. Update and migrate.
  5. Repeat 2-5

Now, this is for one-time fixes only, but in most cases this is the correct way to do it. You fix the bug that causes the data issue, along with fixing the data.

If you really need the same data altering functionality to run more than once (periodically), then for that you can create a custom management command (or just a simple executable python script) and schedule it to run from cron.

0

精彩评论

暂无评论...
验证码 换一张
取 消