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

Feature not deleted / created #556

Closed
atrovato opened this issue Oct 18, 2019 · 6 comments
Closed

Feature not deleted / created #556

atrovato opened this issue Oct 18, 2019 · 6 comments

Comments

@atrovato
Copy link
Contributor

Why is the "update feature" based on feature.external_id instead of feature.id ?

const featureIndex = deviceToReturn.features.findIndex((f) => f.external_id === feature.external_id);

@atrovato
Copy link
Contributor Author

According to this test, if the incoming feature have the same external_id, we update the incoming feature, but it may have a new id.

So, I see 2 solutions :

  1. test on feature.id
  2. keep test on feature.external_id but set old feature id to incoming one

@atrovato atrovato changed the title Feature not updated Feature not deleted / created Oct 19, 2019
@Pierre-Gilles
Copy link
Contributor

The external_id is not meant to change ! It's a unique way to identify a device based on external identifier. (Exemple: Philips Hue Light ID)

We don't use the id, because the service sending the device don't have this id (it's internal).

What's your exact use case/problem ?

@atrovato
Copy link
Contributor Author

When I create a device from MQTT service, I add a light on/off feature and save my device.

Then I remove this feature, by error, and create it again.

It will call the update, but the feature does not exist anymore with this id.

@Pierre-Gilles
Copy link
Contributor

There might be somethings I don't understand, I don't see what's wrong with this case 🤔

If you create a device, remove a feature, then add the feature again, it should work! Based on the external_id of course...

@atrovato
Copy link
Contributor Author

Try it yourself:

  • go to MQTT device creator
  • create one with 1 feature and Feature external ID = mqtt:invalid
  • save it
  • go back and edit this new created device
  • delete feature
  • create a new one with same Feature external ID = mqtt:invalid
  • click on save

==> the device is refreshed but no feature is visible.

What happens ?
Feature is deleted by its id at line 84

if (!features.find((newFeature) => newFeature.id === existingFeature.id)) {

But as we are in the same transaction, when we get plain device at line 97, the feature delete query is not executed, so deleted feature is selected

deviceToReturn = deviceInDb.get({ plain: true });

Then we check for an update not on the id used to delete feature, but on the external_id, feature is always there according to SQL transaction, but at the end, no feature is still present.

Pierre-Gilles added a commit that referenced this issue Oct 22, 2019
…h the same external_id should update existing device feature
@Pierre-Gilles
Copy link
Contributor

The bug was not exactly that, but the fix was simple !

As the external_id is the only source of "truth/unicity", I use it everywhere instead of "id".

Now it works in the case you specified. I added some tests to ensure it stays the same in the future.

R6n0 pushed a commit to R6n0/Gladys that referenced this issue Dec 2, 2020
…ame request with the same external_id should update existing device feature
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