Skip to content

feat(postgres, sqlite): add conflictWhere option to Model.bulkCreate - #13420

Merged
wbourne0 merged 40 commits into
sequelize:mainfrom
wbourne0:add-bulkCreate-conflictWhere
Mar 3, 2023
Merged

wbourne0 merged 40 commits into
sequelize:mainfrom
wbourne0:add-bulkCreate-conflictWhere

Conversation

@wbourne0

@wbourne0 wbourne0 commented Aug 5, 2021 •

Copy link
Copy Markdown
Member

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

  • Added the option conflictWhere to QueryGenerator.prototype.bulkInsertQuery. This is used to generate the ON CONFLICT WHERE part of the query (note: not ON CONFLICT DO UPDATE WHERE).
  • Added an option to Model.bulkCreate, conflictWhere. (rather: documented the option)
  • Updated types
  • Added tests

Note that this is based on #13419 - once that gets merged I'll rebase + remove that diff from this pr.

Example use case:

const Memberships = sequelize.define(
  'memberships',
  {
    user_id: { type: DataTypes.INTEGER, allowNull: false },
    foreign_id: { type: DataTypes.INTEGER, allowNull: false },
    permissions: {
      type: DataTypes.ENUM('admin', 'member', 'guest'),
      allowNull: false,
    },
  },
  {
    deletedAt: 'time_deleted',
    indexes: [
      {
        fields: ['user_id', 'foreign_key'],
        unique: true,
        where: { time_deleted: null },
      },
    ],
  }
);

// In a single query, create (or update) the memberships of many users.
// Only update permissions if existing memberships exist.
Memberships.bulkCreate(
  [
    {
      user_id: 1,
      foreign_id: 1,
      permissions: 'admin',
    },
    {
      user_id: 2,
      foreign_id: 1,
      permissions: 'member',
    },
    {
      user_id: 3,
      foreign_id: 1,
      permissions: 'member',
    },
    {
      user_id: 4,
      foreign_id: 1,
      permissions: 'guest',
    },
  ],
  {
    conflictFields: ['user_id', 'foreign_id'],
    conflictWhere: { time_deleted: null },
    updateOnDuplicate: ['permissions'],
  }
);

The bulkCreate would end up looking similar to this (for postgres):

INSERT INTO
  "memberships" ("id", "user_id", "foreign_id", "permissions")
VALUES
  (DEFAULT, 1, 1, 'admin'),
  (DEFAULT, 2, 1, 'member'),
  (DEFAULT, 3, 1, 'member'),
  (DEFAULT, 4, 1, 'guest') ON CONFLICT ("user_id", "foreign_id")
WHERE
  "time_deleted" IS NULL DO
UPDATE
SET
  "user_id" = EXCLUDED. "user_id",
  "foreign_id" = EXCLUDED. "foreign_id";

Closes #12774.

@wbourne0 wbourne0 added dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). status: awaiting maintainer type: feature DEPRECATED: replace with the "feature" issue type and removed status: awaiting maintainer labels Aug 5, 2021
@wbourne0 wbourne0 self-assigned this Aug 5, 2021
@wbourne0
wbourne0 force-pushed the add-bulkCreate-conflictWhere branch from 96b7c00 to 3438740 Compare August 5, 2021 23:08
@wbourne0 wbourne0 added the status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. label Aug 5, 2021
@wbourne0
wbourne0 force-pushed the add-bulkCreate-conflictWhere branch from 3438740 to 8f7eabe Compare August 5, 2021 23:36
@wbourne0 wbourne0 added status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. and removed status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. labels Aug 5, 2021
lKinderBueno
lKinderBueno previously approved these changes Aug 29, 2021
@ThePletch

Copy link
Copy Markdown

Concerning that typing checks are passing for this, since this appears to be missing a type annotation for the conflictWhere option in the BulkCreateOptions interface.

@wbourne0

wbourne0 commented Dec 2, 2021

Copy link
Copy Markdown
Member Author

Concerning that typing checks are passing for this, since this appears to be missing a type annotation for the conflictWhere option in the BulkCreateOptions interface.

There's no tests on that because I forgot to include it in this pull request. Thanks for the reminder though, I fixed this on another fork of sequelize and forgot to port that change back over.

@github-actions github-actions Bot added the stale label Dec 17, 2021
@wbourne0 wbourne0 removed the stale label Jan 5, 2022
@github-actions github-actions Bot added the stale label Jan 20, 2022
@wbourne0 wbourne0 removed the stale label Feb 10, 2022
@ThePletch

ThePletch commented Feb 24, 2022 •

Copy link
Copy Markdown

any updates on this change? it'd be very valuable to my team to have support for this functionality, since it's required for sequelize to fully support soft deletion.

@ephys
ephys self-requested a review March 11, 2022 10:04
Adds an option to override the `upsertKeys` array passed to the query generator via setting `conflictFields`.
Adds some TS tests for Model.bulkCreate.
@wbourne0
wbourne0 requested a review from ephys February 23, 2023 18:59
@wbourne0

Copy link
Copy Markdown
Member Author

any updates on this change? it'd be very valuable to my team to have support for this functionality, since it's required for sequelize to fully support soft deletion.

late response, hoping to get this merged by end of month.

@wbourne0
wbourne0 merged commit cc4de66 into sequelize:main Mar 3, 2023
@wbourne0
wbourne0 deleted the add-bulkCreate-conflictWhere branch March 3, 2023 19:59
wbourne0 added a commit that referenced this pull request Mar 14, 2023
Adds a conflictWhere option to Model.bulkCreate in postgres & sqlite which controls the clause in the `ON CONFLICT` part of the query.  Backport of #13420.
wbourne0 added a commit that referenced this pull request Mar 14, 2023
Adds a conflictWhere option to Model.bulkCreate in postgres & sqlite which controls the clause in the `ON CONFLICT` part of the query.  Backport of #13420.
wbourne0 added a commit that referenced this pull request Mar 22, 2023
Adds a conflictWhere option to Model.bulkCreate in postgres & sqlite which controls the clause in the `ON CONFLICT` part of the query.  Backport of #13420.
wbourne0 added a commit that referenced this pull request Mar 24, 2023
Adds a conflictWhere option to Model.bulkCreate in postgres & sqlite which controls the clause in the `ON CONFLICT` part of the query.  Backport of #13420.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). type: feature DEPRECATED: replace with the "feature" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bulkCreate with unique partial index in postgres

6 participants