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

Add Update Channels #243

Merged
merged 12 commits into from
Mar 21, 2023
Merged

Conversation

rbino
Copy link
Collaborator

@rbino rbino commented Feb 1, 2023

Add a GraphQL CRUD for UpdateChannels, allowing to manage them an associate them with Target Groups

@coveralls
Copy link

coveralls commented Feb 1, 2023

Pull Request Test Coverage Report for Build 9047d5d5078d8ba8db64f16f4f67cb6498347d84-PR-243

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+81.4%) to 81.434%

Totals Coverage Status
Change from base Build 4053144426: 81.4%
Covered Lines: 943
Relevant Lines: 1158

💛 - Coveralls

backend/lib/edgehog/update_campaigns.ex Outdated Show resolved Hide resolved
backend/test/edgehog/update_campaigns_test.exs Outdated Show resolved Hide resolved
backend/test/edgehog/update_campaigns_test.exs Outdated Show resolved Hide resolved
backend/test/support/fixtures/groups_fixtures.ex Outdated Show resolved Hide resolved
backend/test/support/fixtures/groups_fixtures.ex Outdated Show resolved Hide resolved
Comment on lines +27 to +30
references(:update_channels,
with: [tenant_id: :tenant_id],
on_delete: :nilify_all
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You might want to add a match: full on the foreign key here.

Copy link
Collaborator Author

@rbino rbino Feb 6, 2023

Choose a reason for hiding this comment

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

match: full doesn't allow the id to be NULL (see here), and since a device_group is allowed to have no connected update_channel, this should be supported

backend/test/support/fixtures/update_campaigns_fixtures.ex Outdated Show resolved Hide resolved
backend/test/support/fixtures/update_campaigns_fixtures.ex Outdated Show resolved Hide resolved
backend/lib/edgehog/update_campaigns.ex Outdated Show resolved Hide resolved
Comment on lines +38 to +43
@doc false
def create_changeset(update_channel, attrs) do
update_channel
|> changeset(attrs)
|> validate_required([:target_group_ids])
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am missing the broader context, why a different create_changeset/2 that does not cast or perform all the validations included in changeset/2?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually it does, we usually do separate create/update changesets by duplicating the whole function, but I realized that if you need a changeset that is strictly a superset of another one, you can just create the smaller changeset with its function and then add additional validation/casting, which eliminates duplicate code and the need to keep the two changesets in sync for the common parts.
In this case, target_group_ids is already cast in the changeset function, but when you update an UpdateChannel you don't have to necessarily pass target_group_ids if you don't need to update them, while you do have to pass them when you create it, so I just add an additional validate_required on top of the other changeset

@rbino rbino force-pushed the update-channels branch 4 times, most recently from 0b079ee to 4107493 Compare February 8, 2023 16:30
@rbino rbino marked this pull request as ready for review February 8, 2023 16:37
backend/test/edgehog/update_campaigns_test.exs Outdated Show resolved Hide resolved
backend/lib/edgehog/update_campaigns.ex Outdated Show resolved Hide resolved
|> changeset(attrs)
|> validate_required([:target_group_ids])
end

@doc false
def changeset(update_channel, attrs) do
Copy link
Contributor

Choose a reason for hiding this comment

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

When we have few changesets (see changesets for base_image, ota_operation) usually we prefix their names with verb (like create_changeset, update_changeset). Do we want to rename changeset -> update_changeset?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In this case I left this name like this because it's actually reused in create_changeset (which is something I suspect we can do more when we split the whole update/create stuff). It doesn't make much sense for create_changeset to call update_changeset, hence the naming.

backend/test/support/fixtures/update_campaigns_fixtures.ex Outdated Show resolved Hide resolved
@rbino rbino marked this pull request as draft March 7, 2023 11:24
@rbino rbino force-pushed the update-channels branch 2 times, most recently from 4181537 to 84c7dbd Compare March 7, 2023 17:14
Generated with:

mix phx.gen.context UpdateCampaigns UpdateChannel update_channels \
  tenant_id:references:tenants name:string:unique handle:string:unique

Copyright headers manually added after generation

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
- Change get_update_channel!/1 to fetch_update_channel/1
- Scope unique indexes to tenant
- Validate handle format
- Adapt tests and add some more

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Prerequisite for using this fixtures in the upcoming UpdateChannel fixtures

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Identify which update channel (if any) a device group is associated with

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
The association is explicit only on UpdateChannel since we want the
dependency between context to only go in that direction (i.e.
UpdateCampaigns knows about Groups, not the other way around).

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
@rbino rbino marked this pull request as ready for review March 7, 2023 17:15
Allow assigning and unassigning Device Groups when creating, updating or
deleting an UpdateChannel. Add relevant tests and fixture changes.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Also add relevant tests

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Also add relevant tests

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Also add relevant tests

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Also add relevant tests.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Allow retrieving a device group id -> update channel map for a list of device
group ids using a single database query. Also add relevant tests.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
Allow retrieving the UpdateChannel associated with a specific DeviceGroup. Do
this using batching so we minimize N+1 query problems.
Also add a relevant test and update CHANGELOG since the UpdateChannels
feature is now complete.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
@szakhlypa szakhlypa merged commit e2245a7 into edgehog-device-manager:main Mar 21, 2023
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.

4 participants