After two days of return over the weekend, I came to the company early today, ready to fix the bugs left last week, and then write more bugs.
Before I had put my backpack away, I saw some feedback from colleagues in the group, and there was a table of data missing.
I looked, yes, there were thousands of them, and now there are only a dozen.
The brain quickly turned a few circles, the relevant code I really changed, but it was last week, preliminary judgment, should be with me. However, as a helpful red scarf, I still want to help the screening.
It went something like this:
We have a timer that pulls data from the data source, puts it into a table, creates it if it doesn’t, updates it if it does.
There is also a timing program that removes expired data based on the update_time field of the table, with an expiration time of 7 days.
After a quick look at the code, I felt that there was no problem. My colleagues also said that they had not touched the relevant code recently.
At first glance, there may be a problem with the data source.
Then manually execute a pull program, the data source is no problem.
That’s weird. Is it the code I changed last week that’s wrong?
Guilty make my back slightly sweat, with uneasy mood before the code carefully read again. After reading it, three words came to mind: No problem.
I called two colleagues to help me review it. The three of them looked at the computer screen and sighed: This is no problem!
One of my colleagues looked at my update() method and said, “Update () doesn’t update because the data doesn’t change?”
Another colleague and I countered, “No, it’s impossible.”
And I added, “Come on, have faith in what we’re learning.”
However, it’s hard to see where the problem is.
I ran the program again and didn’t expect it to come so fast. The update_time field was not updated.
So I changed the program a little bit to make sure that the data in the database was different from the data in the database, and ran it again.
I found that the field I changed was updated, but only the update_time field was not updated.
Then I changed the update() method to update_or_create() and ran it again because LAST time I changed update_or_create() to update() and the update_time field was updated.
What? Why on earth is that? Meng ~
Ask Google for help.
In general, we define the time field in the model like this:
create_time = models.DateTimeField(auto_now_add=True, verbose_name="Creation time")
update_time = models.DateTimeField(auto_now=True, verbose_name="Update Time")
Copy the code
Create_time is created when the data is added for the first time and does not change thereafter. Update_time is updated with each data update.
However, update_time is not updated every time, for example with the update_or_create() method mentioned above, as is the save() method. Because both methods are django-orMs.
The more suitable update() method for batch operations is to execute database SQL directly without using Django ORM, so update_time is not updated. What about updating? Manually assign a value to update_time.
This is too pit, the program happily executed for a week without a problem, did not expect to hide such a big BUG, really learned.
Finally, I’d like to thank the company’s DBA for helping to restore the data. For whatever reason, in the past 7 days, I could restore the data to any time I wanted. The experience was not so good that I couldn’t even delete the database and run away.
Tech Blog:
Github.com/yongxinz/te…
Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.