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

🧯 Update contact modified_on after populate dynamic group task #464

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/models/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func PopulateDynamicGroup(ctx context.Context, db *sqlx.DB, es *elastic.Client,
return 0, errors.Wrapf(err, "error performing query: %s for group: %d", query, groupID)
}

// find which need to be added or removed
// find which contacts need to be added or removed
adds := make([]ContactID, 0, 100)
for _, id := range new {
if !present[id] {
Expand Down Expand Up @@ -409,5 +409,15 @@ func PopulateDynamicGroup(ctx context.Context, db *sqlx.DB, es *elastic.Client,
return 0, errors.Wrapf(err, "error marking dynamic group as ready")
}

// finally update modified_on for all affected contacts to ensure these changes are seen by rp-indexer
changed := make([]ContactID, 0, len(adds))
changed = append(changed, adds...)
changed = append(changed, removals...)

err = UpdateContactModifiedOn(ctx, db, changed)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this all belongs on AddContactsToGroups and DeleteContactsFromGroups instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so as those methods don't even hit the contacts_contact table and the other places we use them are when we're processing events/handlers/hooks and we've optimized updating modified_on for each contact.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. Ya, just feels likely we are going to make this bug again elsewhere when we change contact group membership elsewhere by calling this method, but I suppose we can just try to be extra careful.

if err != nil {
return 0, errors.Wrapf(err, "error updating contact modified_on after group population")
}

return len(new), nil
}
4 changes: 4 additions & 0 deletions core/tasks/contacts/populate_dynamic_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/mailroom/core/tasks/contacts"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
Expand Down Expand Up @@ -52,6 +53,7 @@ func TestPopulateTask(t *testing.T) {
}`, testdata.Cathy.ID)

group := testdata.InsertContactGroup(db, testdata.Org1, "e52fee05-2f95-4445-aef6-2fe7dac2fd56", "Women", "gender = F")
start := dates.Now()

task := &contacts.PopulateDynamicGroupTask{
GroupID: group.ID,
Expand All @@ -61,4 +63,6 @@ func TestPopulateTask(t *testing.T) {
require.NoError(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contactgroup_contacts WHERE contactgroup_id = $1`, group.ID).Returns(1)
testsuite.AssertQuery(t, db, `SELECT contact_id FROM contacts_contactgroup_contacts WHERE contactgroup_id = $1`, group.ID).Returns(int64(testdata.Cathy.ID))
testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND modified_on > $2`, testdata.Cathy.ID, start).Returns(1)
}