tethysplatform / tethys

The Tethys Platform main Django website project repository.
http://tethysplatform.org/
BSD 2-Clause "Simplified" License
92 stars 49 forks source link

Fix how settings are queried when installing apps #887

Closed sdc50 closed 1 year ago

sdc50 commented 1 year ago

Currently app settings are not namespaces. This opens up the potential to have conflicting setting names from different apps.

If two apps with the same setting name are installed the two entries are added to the settings table with the same name. This will cause an error when the app tries to retrieve the settings (something to the effect of "query returned two objects when only one was expected").

I propose that we add an "app" column to the settings tables to namespace the settings and ensure that apps are only querying settings that are associated with itself.

swainn commented 1 year ago

There already is an "app" column in the form of the foreign key that establishes the relationship with the TethysApp model:

https://github.com/tethysplatform/tethys/blob/2cfc82f2299bc41920096c28a4c3278ca9a9353e/tethys_apps/models.py#L178-L192

This sounds like an issue with the query. Where is the failing query?

sdc50 commented 1 year ago

It looks like the query is failing when installing apps:

Traceback (most recent call last):
  File "/conda/envs/tethys/bin/tethys", line 10, in <module>
    sys.exit(tethys_command())
  File "/conda/envs/tethys/lib/python3.10/site-packages/tethys_cli/__init__.py", line 67, in tethys_command
    args.func(args)
  File "/conda/envs/tethys/lib/python3.10/site-packages/tethys_cli/install_commands.py", line 703, in install_command
    portal_result = run_portal_install(app_name)
  File "/conda/envs/tethys/lib/python3.10/site-packages/tethys_cli/install_commands.py", line 500, in run_portal_install
    configure_services_from_file(services, app_name)
  File "/conda/envs/tethys/lib/python3.10/site-packages/tethys_cli/install_commands.py", line 417, in configure_services_from_file
    custom_setting = CustomSetting.objects.get(name=setting_name)
  File "/conda/envs/tethys/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/conda/envs/tethys/lib/python3.10/site-packages/django/db/models/query.py", line 439, in get
    raise self.model.MultipleObjectsReturned(
tethys_apps.models.CustomSetting.MultipleObjectsReturned: get() returned more than one CustomSetting -- it returned 2!

When calling the get_custom_setting method on the app it first queries the app and then gets its settings objects and queries that (already doing what I proposed).

However, in the install command it just queries the CustomSettings directly and doesn't filter by the app.