Skip to content
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
4 changes: 0 additions & 4 deletions tom_base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,6 @@
'PAGE_SIZE': 100
}

# Silence system checks that are not applicable to how the TOM Toolkit is designed.
# These are likely temporary and can be removed in downstream TOMs if desired.
SILENCED_SYSTEM_CHECKS = ['models.W047']

try:
from local_settings import * # noqa
except ImportError:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 5.2.13 on 2026-09-23 22:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tom_dataproducts', '0019_alter_astrometryreduceddatum_options_and_more'),
('tom_targets', '0031_basetarget_shared_by_basetarget_shared_from'),
]

operations = [
migrations.RemoveConstraint(
model_name='astrometryreduceddatum',
name='unique_astrometry',
),
migrations.RemoveConstraint(
model_name='photometryreduceddatum',
name='unique_photometry',
),
migrations.RemoveConstraint(
model_name='spectroscopyreduceddatum',
name='unique_spectroscopy',
),
migrations.AddConstraint(
model_name='astrometryreduceddatum',
constraint=models.UniqueConstraint(fields=('target', 'timestamp', 'telescope', 'instrument', 'ra', 'dec', 'reduction_version'), name='unique_astrometry'),
),
migrations.AddConstraint(
model_name='photometryreduceddatum',
constraint=models.UniqueConstraint(fields=('target', 'bandpass', 'timestamp', 'brightness', 'instrument', 'reduction_version'), name='unique_photometry_brightness'),
),
migrations.AddConstraint(
model_name='photometryreduceddatum',
constraint=models.UniqueConstraint(fields=('target', 'bandpass', 'timestamp', 'limit', 'instrument', 'reduction_version'), name='unique_photometry_limit'),
),
migrations.AddConstraint(
model_name='spectroscopyreduceddatum',
constraint=models.UniqueConstraint(fields=('target', 'timestamp', 'telescope', 'instrument', 'flux', 'reduction_version'), name='unique_spectroscopy'),
),
]
11 changes: 6 additions & 5 deletions tom_dataproducts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,12 @@ class PhotometryReducedDatum(ReducedDatumCommon):
class Meta(ReducedDatumCommon.Meta):
constraints = [
models.UniqueConstraint(
fields=["target", "bandpass", "timestamp", "limit", "brightness", "instrument", "reduction_version"],
name="unique_photometry",
nulls_distinct=False
fields=["target", "bandpass", "timestamp", "brightness", "instrument", "reduction_version"],
name="unique_photometry_brightness",
),
models.UniqueConstraint(
fields=["target", "bandpass", "timestamp", "limit", "instrument", "reduction_version"],
name="unique_photometry_limit",
Comment on lines 458 to +464

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would this handle the situation where we have two photometry points with different brightnesses but the same limit? Whenever a limiting magnitude is available, we have been storing that in our database, even if the photometry point is a detection (i.e., it has a brightness). I think we would want two PhotometryReducedDatums to be allowed as long as the brightness OR the limit (or both) were different.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Speaking to @griffin-h I think we determined that this would not cause problems in realistic scenarios.

)
]

Expand All @@ -481,7 +484,6 @@ class Meta(ReducedDatumCommon.Meta):
models.UniqueConstraint(
fields=["target", "timestamp", "telescope", "instrument", "flux", "reduction_version"],
name="unique_spectroscopy",
nulls_distinct=False
)
]

Expand All @@ -503,7 +505,6 @@ class Meta(ReducedDatumCommon.Meta):
models.UniqueConstraint(
fields=["target", "timestamp", "telescope", "instrument", "ra", "dec", "reduction_version"],
name="unique_astrometry",
nulls_distinct=False
)
]

Expand Down
112 changes: 112 additions & 0 deletions tom_dataproducts/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import IntegrityError
from tom_targets.base_models import get_target_model_app_label
from http import HTTPStatus
import os
Expand Down Expand Up @@ -635,6 +636,17 @@ def test_create_reduced_datum(self):

self.assertEqual(2, ReducedDatum.objects.count())

def test_create_reduced_datum_without_legacy_value(self):
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=None,
bandpass="r",
limit=2.0,
)

self.assertEqual(1, ReducedDatum.objects.count())

def test_create_reduced_datum_duplicate(self):
"""Test that we cannot add a second PhotometryReducedDatum with the same target,
timestamp, brightness, and bandpass"""
Expand All @@ -653,6 +665,106 @@ def test_create_reduced_datum_duplicate(self):
bandpass="r"
)

def test_create_reduced_datum_duplicate_limit_none(self):
"""Test that we cannot add a duplicate ReducedDataum, even when limit is None.
exposure_time is null in both cases, but we still expect a ValidationError."""
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=1.0,
bandpass="r",
limit=None,
)

# This raises a ValidationError because by using create the uniqueness constraint
# is enforced in code, and never reaches the database level constraint.
with self.assertRaises(ValidationError):
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=1.0,
bandpass="r",
limit=None,
)

def test_create_reduced_datum_duplicate_brightness_none(self):
"""Test that we cannot add a duplicate ReducedDataum, even when brightness is None.
exposure_time is null in both cases, but we still expect a ValidationError."""
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=None,
bandpass="r",
limit=2.0,
)

# This raises a ValidationError because by using create the uniqueness constraint
# is enforced in code, and never reaches the database level constraint.
with self.assertRaises(ValidationError):
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=None,
bandpass="r",
limit=2.0,
)

def test_create_reduced_datum_duplicate_none_limit_bulk(self):
"""Test that we cannot add duplicate ReducedDatums, even when limit is None.
Tests that this applies to bulk operations.
"""
# Ensure we can create one first
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=1.0,
bandpass="r",
limit=None,
)
self.assertEqual(1, PhotometryReducedDatum.objects.count())
datums = [
PhotometryReducedDatum(
target=self.target,
timestamp=self.timestamp,
brightness=1.0,
bandpass="r",
limit=None,
)
for _ in range(3)
]
# This raises an IntegrityError because Python level validation does not occur in
# bulk operations, here we rely on the database level constraint to prevent duplicates
with self.assertRaises(IntegrityError):
PhotometryReducedDatum.objects.bulk_create(datums)

def test_create_reduced_datum_duplicate_none_brightness_bulk(self):
"""Test that we cannot add duplicate ReducedDatums, even when brightness is None.
Tests that this applies to bulk operations.
"""
# Ensure we can create one first
PhotometryReducedDatum.objects.create(
target=self.target,
timestamp=self.timestamp,
brightness=None,
bandpass="r",
limit=2.0,
)
self.assertEqual(1, PhotometryReducedDatum.objects.count())
datums = [
PhotometryReducedDatum(
target=self.target,
timestamp=self.timestamp,
brightness=None,
bandpass="r",
limit=2.0,
)
for _ in range(3)
]
# This raises an IntegrityError because Python level validation does not occur in
# bulk operations, here we rely on the database level constraint to prevent duplicates
with self.assertRaises(IntegrityError):
PhotometryReducedDatum.objects.bulk_create(datums)


@override_settings(TOM_FACILITY_CLASSES=['tom_observations.tests.utils.FakeRoboticFacility'],
TARGET_PERMISSIONS_ONLY=True,
Expand Down
4 changes: 0 additions & 4 deletions tom_setup/templates/tom_setup/settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,6 @@ PLOTLY_THEME = 'plotly_white'
# Set this to False if you have a particularly large DB and paginated views are slow.
SHOW_PAGINATION_INFO = True

# Silence system checks that are not applicable to how the TOM Toolkit is designed.
# These are likely temporary and can be removed in downstream TOMs if desired.
SILENCED_SYSTEM_CHECKS = ['models.W047']

try:
from local_settings import * # noqa
except ImportError:
Expand Down
Loading