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

cache.rollbackRelationships does not rollback the 2nd time after adding a record and rollback again #9207

Closed
eodb opened this issue Jan 23, 2024 · 3 comments · Fixed by #9221

Comments

@eodb
Copy link

eodb commented Jan 23, 2024

Description

I've tried using the "most welcome" feature to be able to rollback a relationship in the cache after a record was added to a sync hasMany relationship (using store.cache.rollbackRelationships). This works fine the first time but the 2nd time the record is added and a rollback of the relation is attempted, the record is not removed from the collection.

I know it is a brand new feature, so I'm not sure if we can use it already or that I'm missing something here...

Reproduction

The last assert in following qunit test fails, after a 2nd attempt to rollback the relationship.

import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';

let store;

module('integration/relationship/rollback relationship twice', function (hooks) {
  setupTest(hooks);

  hooks.beforeEach(function () {
    class Job extends Model {
      @attr name;
      @hasMany('message', { async: false, inverse: null }) messages;
    }

    class Message extends Model {
      @attr msg;
    }

    store = this.owner.lookup('service:store');
    this.owner.register('model:job', Job);
    this.owner.register('model:message', Message);
  });

  /**  test to verify that Ember Data issue https://github.com/emberjs/data/issues/8318 is fixed */
  test('rollback relationship twice', async function (assert) {
    let job;
    let msg1;

    let jobs = await store.push({
      data: [
        {
          id: '1',
          type: 'job',
          attributes: {
            name: 'First Job',
          },
        },
      ],
    });

    let msgs = await store.push({
      data: [
        {
          id: '1',
          type: 'message',
          attributes: {
            msg: 'First Message',
          },
        },
      ],
    });

    assert.equal(jobs.length, 1);
    job = jobs[0];
    assert.equal(msgs.length, 1);
    msg1 = msgs[0];

    job.messages.push(msg1);
    assert.equal(job.messages.length, 1, 'job has 1 message');

    const stableRecordIdentifier = store.getReference('job', job.id).identifier();

    let rollbackRelations = store.cache.rollbackRelationships(stableRecordIdentifier);
    assert.equal(rollbackRelations.length, 1, '1 rollbackRelations');
    assert.equal(job.messages.length, 0, 'job has no message');

    // repeat the scenario to add a message and rollback
    job.messages.push(msg1);
    assert.equal(job.messages.length, 1, 'job has 1 message');
    rollbackRelations = store.cache.rollbackRelationships(stableRecordIdentifier);
    assert.equal(rollbackRelations.length, 1, '1 rollbackRelations');

    // 2nd time the relationship does not seem to be reverted
    assert.equal(job.messages.length, 0, 'job has no message');
  });
});

Versions

@runspired
Copy link
Contributor

@eodb did you try your test again main? or just in your app against 5.3?

@eodb
Copy link
Author

eodb commented Jan 24, 2024

@runspired Hi, at first I only tried the 5.3 but I've just redone the qunit test with the latest available version (5.5.0-alpha.11) and the result seems to be same (i.e. the last assert in the test fails)

@runspired
Copy link
Contributor

@eodb our publish script has been borked for a while so we haven't actually published an alpha in some time. The fix I think affects this is only on main and in 4.12 (where rollback relationships wasn't available yet). We've also ported it to the 5.3 release branch but haven't published that fix yet in 5.3. Any chance you could PR the test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
2 participants