Repository navigation
fix(model.js): updateOnDuplicate for unique index - #12516
brianschardt wants to merge 9 commits into
Conversation
|
@eseliger thanks for helping with this awesome package. I have been waiting a while for composite key functionality to be added to v6 and finally tested it. However, it still lacked support for unique indexes. This PR will add the functionality. For me im a little in a rush and would love to get this in the next release. Let me know if there is anything I need to do to get this in there. |
|
Hey! |
Codecov Report
@@ Coverage Diff @@
## master #12516 +/- ##
==========================================
+ Coverage 90.13% 95.83% +5.70%
==========================================
Files 92 92
Lines 9112 9060 -52
Branches 90 90
==========================================
+ Hits 8213 8683 +470
+ Misses 880 360 -520
+ Partials 19 17 -2
Continue to review full report at Codecov.
|
|
@eseliger hey! just updated the PR and added a test case. Let me know if there is anything else I can do to get this merged in sooner. |
| let onConflictKeys = []; | ||
| if (Object.keys(model.uniqueKeys).length > 0) { | ||
| options.upsertKeys = _.chain(model.uniqueKeys) | ||
| onConflictKeys = _.chain(model.uniqueKeys) |
There was a problem hiding this comment.
Unrelated to this PR:
Damn I just realized this whole function is almost 400 lines long. I guess at some point we could do some extraction on some of this logic to make this whole thing more manageable right?
There was a problem hiding this comment.
Yes do this repo have a chore list? I would like the feature merged in before we do that refactor.
There was a problem hiding this comment.
I'm not sure, maybe in issues? I wouldn't be able to say since I'm not a regular contributor.
| indexes: [ | ||
| { | ||
| unique: true, | ||
| fields: ['system_id', 'system'] | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
This fix would also work for when a unique index is defined like this:
const uniqueIndex = {
name: 'user_contest',
msg: 'User is already part of contest.',
};
export class UserContestInfo extends Model {
public contestId!: string;
public prize?: number;
public userId!: string;
public rank?: number;
}
const UserContestInfoFactory = (connection: sequelize.Sequelize) => {
const attributes: ModelAttributes = {
userId: {
type: DataTypes.UUID,
unique: uniqueIndex,
},
confirmed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
contestId: {
type: DataTypes.UUID,
unique: uniqueIndex,
},
prize: {
type: DataTypes.INTEGER,
},
rank: {
type: DataTypes.INTEGER,
},
};
// rest of logic omitted for brevity...
}right?
aecorredor
left a comment
There was a problem hiding this comment.
One question and one small suggestion.
| model._indexes.forEach(index => { | ||
| if (index.unique) { | ||
| index.fields.forEach(uniqueKey => { | ||
| onConflictKeys.push(uniqueKey); | ||
| }); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Since this always happens we can avoid the middle let variable and have it this way
const onConflictKeys = model._indexes.reduce((acc, modelIndex) => {
if (modelIndex.unique) {
modelIndex.fields.forEach(uniqueKey => {
acc.push(uniqueKey);
});
}
return acc;
}, []);
if (Object.keys(model.uniqueKeys).length > 0) {
const restOnConflictKeys = _.chain(model.uniqueKeys)
.values()
.filter((c) => c.fields.length >= 1)
.map((c) => c.fields)
.reduce((c) => c[0])
.value();
onConflictKeys.push(...restOnConflictKeys);
}
if (onConflictKeys.length > 0) {
options.upsertKeys = onConflictKeys;
}There was a problem hiding this comment.
That looks like it would work have you tested it?
There was a problem hiding this comment.
I have not, but the test case you added should still pass with this and should not break anything else right? All we did here was go through the model._indexes first to avoid the middle let onConflictKeys.
There was a problem hiding this comment.
Let me know when you push that change. I'll stamp mine after that.
|
Nice, I think we'll need a PR towards v6 once this is merged right? |
|
Yes I would like to get this in v5 and v6 if possible. |
|
@aecorredor Do you know the best plan of action to get it in? Make a pr to the v6 and v5 branches? |
|
@brianschardt I think just making another PR from the same branch towards v6 would work. Right @eseliger? |
|
@brianschardt question, when you first branched off, did you branch off from v5/master? |
|
i just branched off of master. |
|
Perfect, ignore my last comment. To avoid the conflicts you can make the PR branching off from v6. |
|
Just branched off of master here is the new PR: #12547 |
|
@eseliger hey! who would be responsible for getting this approved and merged? Thanks! |
|
@eseliger could we get an official review on this one some time soon? Or if not, some guidance on who we would have to contact to get this in. Thanks! |
|
hey this would be really great! any idea on when this may be included? |
|
Hello @brianschardt! I see you are a first-time contributor, thank you for taking the time to help Sequelize! I hope to see more PRs from you in the future! I apologize to you and everyone else for taking so long to give some attention here. I reworked this PR into #13345, since this PR is pointing to the old Let's continue the discussion on #13345 |
|
Released on v6.6.4 🎉 |
Currently updateOnDuplicate for unique index and indexes do not work. This PR solves this issue.