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

Upsert all restore soft-deleted record accidentally #570

Open
mahdiar-naufal-shyftplan opened this issue Nov 3, 2024 · 1 comment
Open

Comments

@mahdiar-naufal-shyftplan

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\"

How to reproduce:
use this test, it will fail on AR 7.0+

def test_upsert_all_on_soft_deleted_record
    e1 = Employer.create(name: "e1")
    e2 = Employer.create(name: "e2", deleted_at: Time.current)
    assert_nil e1.deleted_at
    assert e2.deleted_at != nil

    Employer.upsert_all([
      { id: e1.id, name: "new_e1" },
      { id: e2.id, name: "new_e2" }
    ])

    assert e1.reload.name == "new_e1"
    assert e2.reload.name == "new_e2"

    assert_nil e1.reload.deleted_at
    assert e2.reload.deleted_at != nil
  end
@JohnnysRibeiro
Copy link

I also had this issue! the fix on the PR solved for me

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

No branches or pull requests

2 participants