Skip to content

fix(model.js): updateOnDuplicate for unique index - #12516

Closed
brianschardt wants to merge 9 commits into
sequelize:masterfrom
prototype-two:unique-index-upsert
Closed

brianschardt wants to merge 9 commits into
sequelize:masterfrom
prototype-two:unique-index-upsert

Conversation

@brianschardt

Copy link
Copy Markdown
Contributor

Currently updateOnDuplicate for unique index and indexes do not work. This PR solves this issue.

@brianschardt

Copy link
Copy Markdown
Contributor Author

@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.

@eseliger

Copy link
Copy Markdown
Member

Hey!
Can you please add a test case for the added functionality? Also, this seems to break existing tests.
Also, after this is merged, a separate PR to the v6 branch would be needed, if you need it released quickly :)

@codecov

codecov Bot commented Jul 22, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #12516 into master will increase coverage by 5.70%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/model.js 96.59% <100.00%> (+0.08%) ⬆️
src/dialects/mariadb/query-generator.js 18.75% <0.00%> (-81.25%) ⬇️
src/dialects/mariadb/data-types.js 47.36% <0.00%> (-52.64%) ⬇️
src/dialects/abstract/query-interface.js 92.28% <0.00%> (-1.86%) ⬇️
src/sequelize.js 94.90% <0.00%> (ø)
src/dialects/mariadb/index.js
src/dialects/mariadb/query.js
src/dialects/mariadb/connection-manager.js
src/dialects/mssql/index.js 100.00% <0.00%> (ø)
src/dialects/mssql/query-interface.js 100.00% <0.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d06aa03...bed8a0b. Read the comment docs.

@brianschardt

Copy link
Copy Markdown
Contributor Author

@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.

Comment thread src/model.js Outdated
let onConflictKeys = [];
if (Object.keys(model.uniqueKeys).length > 0) {
options.upsertKeys = _.chain(model.uniqueKeys)
onConflictKeys = _.chain(model.uniqueKeys)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@brianschardt brianschardt Jul 23, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes do this repo have a chore list? I would like the feature merged in before we do that refactor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, maybe in issues? I wouldn't be able to say since I'm not a regular contributor.

Comment on lines +737 to +743
indexes: [
{
unique: true,
fields: ['system_id', 'system']
}
]
}

@aecorredor aecorredor Jul 23, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@aecorredor aecorredor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question and one small suggestion.

Comment thread src/model.js Outdated
Comment on lines +2821 to +2827
model._indexes.forEach(index => {
if (index.unique) {
index.fields.forEach(uniqueKey => {
onConflictKeys.push(uniqueKey);
});
}
});

@aecorredor aecorredor Jul 23, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like it would work have you tested it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep correct.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know when you push that change. I'll stamp mine after that.

@aecorredor

Copy link
Copy Markdown
Contributor

Nice, I think we'll need a PR towards v6 once this is merged right?

@brianschardt

Copy link
Copy Markdown
Contributor Author

Yes I would like to get this in v5 and v6 if possible.

@brianschardt

Copy link
Copy Markdown
Contributor Author

@aecorredor Do you know the best plan of action to get it in? Make a pr to the v6 and v5 branches?

@aecorredor

Copy link
Copy Markdown
Contributor

@brianschardt I think just making another PR from the same branch towards v6 would work. Right @eseliger?

@brianschardt

Copy link
Copy Markdown
Contributor Author

Just made a PR: #12543 to v6 but there were a lot of merge conflicts. Should I branch of the v6 and make the change there? @eseliger

@aecorredor

aecorredor commented Jul 24, 2020 •

Copy link
Copy Markdown
Contributor

@brianschardt question, when you first branched off, did you branch off from v5/master? If so, that might be an issue since some fixes were supposedly introduced related to this same bug in v6. EDIT: there shouldn't be issues since you are fixing the bug going from what you saw not working in v6, sorry.

@brianschardt

Copy link
Copy Markdown
Contributor Author

i just branched off of master.

@aecorredor

Copy link
Copy Markdown
Contributor

Perfect, ignore my last comment. To avoid the conflicts you can make the PR branching off from v6.

@brianschardt

brianschardt commented Jul 24, 2020 •

Copy link
Copy Markdown
Contributor Author

Just branched off of master here is the new PR: #12547
It is showing a code diff that gives it a bad check.
@aecorredor @eseliger

@aecorredor

Copy link
Copy Markdown
Contributor

@eseliger hey! who would be responsible for getting this approved and merged? Thanks!

@aecorredor

Copy link
Copy Markdown
Contributor

@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!

@aecorredor

Copy link
Copy Markdown
Contributor

@papb what do we need to get this guy and also #12547 in?

Thanks.

@michaelfarrell76

Copy link
Copy Markdown

hey this would be really great! any idea on when this may be included?

@papb

papb commented Jun 24, 2021

Copy link
Copy Markdown
Member

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 master branch. Also, it took me some extra time to understand what is going on with this PR because unfortunately the test you added already passes without your changes. I slightly modified the test in order to make your changes necessary to make it pass.

Let's continue the discussion on #13345

@papb papb closed this Jun 24, 2021
@papb

papb commented Jun 26, 2021

Copy link
Copy Markdown
Member

Released on v6.6.4 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants