Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 538a6192
authored
Sep 13, 2015
by
Kenji Tayama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use defaultValue of deletedAt instead of NULL in paranoidClause and restore
1 parent
2bfa312f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
5 deletions
lib/instance.js
lib/model.js
test/integration/sequelize.test.js
lib/instance.js
View file @
538a619
...
...
@@ -863,7 +863,11 @@ Instance.prototype.restore = function(options) {
return
this
.
Model
.
runHooks
(
'beforeRestore'
,
this
,
options
);
}
}).
then
(
function
()
{
this
.
setDataValue
(
this
.
Model
.
_timestampAttributes
.
deletedAt
,
null
);
var
deletedAtCol
=
this
.
Model
.
_timestampAttributes
.
deletedAt
,
deletedAtAttribute
=
this
.
Model
.
rawAttributes
[
deletedAtCol
]
,
deletedAtDefaultValue
=
deletedAtAttribute
.
hasOwnProperty
(
'defaultValue'
)
?
deletedAtAttribute
.
defaultValue
:
null
;
this
.
setDataValue
(
deletedAtCol
,
deletedAtDefaultValue
);
return
this
.
save
(
_
.
extend
({},
options
,
{
hooks
:
false
,
omitNull
:
false
}));
}).
tap
(
function
()
{
// Run after hook
...
...
lib/model.js
View file @
538a619
...
...
@@ -136,9 +136,11 @@ var paranoidClause = function(model, options) {
}
var
deletedAtCol
=
model
.
_timestampAttributes
.
deletedAt
,
deletedAtObject
=
{};
,
deletedAtAttribute
=
model
.
rawAttributes
[
deletedAtCol
]
,
deletedAtObject
=
{}
,
deletedAtDefaultValue
=
deletedAtAttribute
.
hasOwnProperty
(
'defaultValue'
)
?
deletedAtAttribute
.
defaultValue
:
null
;
deletedAtObject
[
model
.
rawAttributes
[
deletedAtCol
].
field
||
deletedAtCol
]
=
null
;
deletedAtObject
[
deletedAtAttribute
.
field
||
deletedAtCol
]
=
deletedAtDefaultValue
;
if
(
Utils
.
_
.
isEmpty
(
options
.
where
))
{
options
.
where
=
deletedAtObject
;
...
...
@@ -2286,8 +2288,12 @@ Model.prototype.restore = function(options) {
}
}).
then
(
function
()
{
// Run undelete query
var
attrValueHash
=
{};
attrValueHash
[
self
.
_timestampAttributes
.
deletedAt
]
=
null
;
var
attrValueHash
=
{}
,
deletedAtCol
=
self
.
_timestampAttributes
.
deletedAt
,
deletedAtAttribute
=
self
.
rawAttributes
[
deletedAtCol
]
,
deletedAtDefaultValue
=
deletedAtAttribute
.
hasOwnProperty
(
'defaultValue'
)
?
deletedAtAttribute
.
defaultValue
:
null
;
attrValueHash
[
deletedAtCol
]
=
deletedAtDefaultValue
;
options
.
omitNull
=
false
;
return
self
.
QueryInterface
.
bulkUpdate
(
self
.
getTableName
(
options
),
attrValueHash
,
options
.
where
,
options
,
self
.
_timestampAttributes
.
deletedAt
);
}).
tap
(
function
()
{
...
...
test/integration/sequelize.test.js
View file @
538a619
...
...
@@ -1215,4 +1215,55 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
});
});
});
describe
(
'paranoid deletedAt non-null default value'
,
function
()
{
it
(
'should use defaultValue of deletedAt in paranoid clause and restore'
,
function
()
{
var
epochObj
=
new
Date
(
0
)
,
epoch
=
Number
(
epochObj
);
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
username
:
DataTypes
.
STRING
,
deletedAt
:
{
type
:
DataTypes
.
DATE
,
defaultValue
:
epochObj
}
},
{
paranoid
:
true
,
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
User
.
create
({
username
:
'user1'
}).
then
(
function
(
user
)
{
expect
(
Number
(
user
.
deletedAt
)).
to
.
equal
(
epoch
);
return
User
.
findOne
({
where
:
{
username
:
'user1'
}
}).
then
(
function
(
user
)
{
expect
(
user
).
to
.
exist
;
expect
(
Number
(
user
.
deletedAt
)).
to
.
equal
(
epoch
);
return
user
.
destroy
();
}).
then
(
function
(
destroyedUser
)
{
expect
(
Number
(
destroyedUser
.
deletedAt
)).
not
.
to
.
equal
(
epoch
);
return
destroyedUser
.
restore
();
}).
then
(
function
(
restoredUser
)
{
expect
(
Number
(
restoredUser
.
deletedAt
)).
to
.
equal
(
epoch
);
return
User
.
destroy
({
where
:
{
username
:
'user1'
}});
}).
then
(
function
()
{
return
User
.
count
();
}).
then
(
function
(
count
)
{
expect
(
count
).
to
.
equal
(
0
);
return
User
.
restore
();
}).
then
(
function
()
{
return
User
.
findAll
();
}).
then
(
function
(
nonDeletedUsers
)
{
expect
(
nonDeletedUsers
.
length
).
to
.
equal
(
1
);
nonDeletedUsers
.
forEach
(
function
(
u
)
{
expect
(
Number
(
u
.
deletedAt
)).
to
.
equal
(
epoch
);
});
});
});
});
});
});
});
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment