New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent behavior for paranoid records since #5897 (see issue #7204) #7290
Conversation
Codecov Report
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its a good addition to soft delete system, It should really be querying CURRENT_TIMESTAMP
. This wasn't a problem for me because mostly my database and app servers are running on UTC. This will help users using fragmented timezones for app and db server.
I have a few comments that needs to be addressed
}).spread(function(users) { | ||
expect(users[0].username).to.equal('Peter'); | ||
expect(users[1].username).to.equal('Paul'); | ||
|
||
expect(moment(new Date(users[0].deletedAt)).utc().format('YYYY-MM-DD h:mm')).to.equal(this.date); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests can still work, current timestamp will be returning UTC date, which can be compared with this.date
defined above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why, but when I run this test the locally instantiated date is set to 1970-01-01 12:00
which is nowhere near the CURRENT_TIMESTAMP
of the database, thereby causing the test to fail. Perhaps this is part of the test suite?
Regardless, this seems to highlight the problems that can arise when database and system clocks for whatever reason aren't synced. We could still compare the deletedAt
field assigned by the database with the locally instantiated one, but seeing as the server time is no longer used to populate the database I'm not sure it buys us anything.
@@ -1810,7 +1810,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { | |||
return this.ParanoidUser.create({ username: 'fnord' }).then(function() { | |||
return self.ParanoidUser.findAll().then(function(users) { | |||
return users[0].destroy().then(function() { | |||
expect(users[0].deletedAt.getMonth).to.exist; | |||
expect(users[0].deletedAt.val).to.be.equal('CURRENT_TIMESTAMP'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be populated with actual date object, which is returned by CURRENT_TIMESTAMP
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since updates only return the number of affected rows, we would need to issue a subsequent SELECT to retrieve the actual timestamp assigned by the database.
Hello Sushant, The caveat as things currently exist (caveat probably being an understatement), is that unless both your server and database are hosted in UTC, deletions may take effect several hours after expected. On the other hand, with the introduction of this PR, instances returned by Of course a third option is to revert back to treating I've only been using Sequelize a short while so I'll be relying on you guys to suggest the most preferred solution. I do think we have to do something to address the current issue before 4.0 however - otherwise users will almost surely find their deleted records suddenly coming back from the dead. |
Thanks @Wsiegenthaler, I did some brainstorming about concerns raised by this PR. Although some of these concerns are valid but we can have timestamp backed soft delete, without much change. First, I think change Second, Sequelize consider your database server is running in UTC (Your application server can run in any timezone). Working of our timezone system as explained by @janmeier 2 years ago #854 (comment) Main concern you raise in #7204 was With correct timezone setting, the offset you were noticing should be fixed. Sequelize was considering your app was running in UTC the whole time. As I tested with Executing (default): INSERT INTO "Ms" ("id","test","createdAt","updatedAt") VALUES (DEFAULT,34,'2017-03-05 11:08:39.444 +05:30','2017-03-05 11:08:39.444 +05:30') RETURNING *;
{ id: 1,
test: 34,
updatedAt: 2017-03-05T05:38:39.444Z,
createdAt: 2017-03-05T05:38:39.444Z,
deletedAt: null }
Executing (default): UPDATE "Ms" SET "deletedAt"='2017-03-05 11:08:39.466 +05:30' WHERE "id" = 1 AND "deletedAt" IS NULL
{ id: 1,
test: 34,
updatedAt: 2017-03-05T05:38:39.444Z,
createdAt: 2017-03-05T05:38:39.444Z,
deletedAt: 2017-03-05T05:38:39.466Z } // Correct UTC based timestamp
Executing (default): SELECT "id", "test", "createdAt", "updatedAt", "deletedAt" FROM "Ms" AS "M" WHERE (("M"."deletedAt" > CURRENT_TIMESTAMP OR "M"."deletedAt" IS NULL) AND "M"."id" = 1);
null |
I just ran some tests and you're absolutely right - it appears the crux is to always keep your database in UTC. I did just this in my dev environment and haven't seen any problems, but are there any pitfalls I should be aware of when changing the timezone in production? I'll be closing this PR and opening a new one with only the $gte change to the paranoid clause. I think the UTC requirement really ought to be in the docs front and center to prevent others from having the same issues. Let me know if I can help with that. Thanks again for taking the time to work through this! |
Apologies for coming late to this, but issues like #8202 have me diving into the consistency of Sequelize in this area.
Let me know if there's a better place to continue this discussion. |
This is a fix for 2 issues introduced by #5880/#5897 and documented in #7204:
Given that this issue fundamentally affects the ability to delete paranoid records, I'd advocate for getting this into v4.
Relevant tests have been updated and now pass, and this change has been added to the 'future' section of the changelog. Please let me know if there's anything else I can do to expedite this!