Skip to content
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

Fix: upsert all to not restore soft-deleted record accidentally #569

Open
wants to merge 2 commits into
base: core
Choose a base branch
from

Conversation

mahdiar-naufal-shyftplan
Copy link

@mahdiar-naufal-shyftplan mahdiar-naufal-shyftplan commented Nov 3, 2024

Addressing this issue:

When upsert records with upsert_all method on AR 7.0 above, Paranoia restore any matching deleted record.
The sql from AR 6 returned something like this if we do an upsert from the added test:

INSERT INTO \"employers\" (\"id\",\"name\") VALUES (1, 'new_e1'), (2, 'new_e2') ON CONFLICT (\"id\") DO UPDATE SET \"name\"=excluded.\"name\"

With AR 7, InsertAll includes scope keys automatically, generates sql into something like this:

INSERT INTO \"employers\" (\"id\",\"name\",\"deleted_at\") VALUES (1, 'new_e1', NULL), (2, 'new_e2', NULL) ON CONFLICT (\"id\") DO UPDATE SET \"name\"=excluded.\"name\",\"deleted_at\"=excluded.\"deleted_at\" RETURNING \"id\"

This PR excludes the paranoia_columns on conflict handling so that the paranoia column will not be replaced by NULL on upsert_all, result:

INSERT INTO \"employers\" (\"id\",\"name\") VALUES (1, 'new_e1'), (2, 'new_e2') ON CONFLICT (\"id\") DO UPDATE SET \"name\"=excluded.\"name\" RETURNING \"id\"

I have tested this PR in ruby 3.2.1 in rails 7.1.3.2 and 6.0.6.1.

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.

2 participants