Skip to content

Commit 6c09501

Browse files
Merge pull request #648 from geoadmin/PB-1403-asset
PB-1403: Only update asset timestamp when the underlying file has changed.
2 parents 071f2f6 + 0e4d3f5 commit 6c09501

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Generated by Django 5.2.11 on 2026-06-03 10:52
2+
3+
import pgtrigger.compiler
4+
import pgtrigger.migrations
5+
6+
from django.db import migrations
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('stac_api', '0072_remove_collectionasset_upd_dec_col_asset_proj_epsg_trigger_and_more'),
13+
]
14+
15+
operations = [
16+
pgtrigger.migrations.RemoveTrigger(
17+
model_name='asset',
18+
name='update_asset_auto_variables_trigger',
19+
),
20+
pgtrigger.migrations.AddTrigger(
21+
model_name='asset',
22+
trigger=pgtrigger.compiler.Trigger(
23+
name='update_asset_auto_variables_trigger',
24+
sql=pgtrigger.compiler.UpsertTriggerSql(
25+
condition=
26+
'WHEN (OLD."checksum_multihash" IS DISTINCT FROM (NEW."checksum_multihash") OR OLD."file" IS DISTINCT FROM (NEW."file"))',
27+
func=
28+
"\n -- update auto variables\n NEW.etag = gen_random_uuid();\n NEW.updated = now();\n\n RAISE INFO 'Updated auto fields of %.id=% due to table updates.', TG_TABLE_NAME, NEW.id;\n\n RETURN NEW;\n ",
29+
hash='ec15994d80ec852d8e7d8aa3b50a915b49a32a2e',
30+
operation='UPDATE',
31+
pgid='pgtrigger_update_asset_auto_variables_trigger_003e1',
32+
table='stac_api_asset',
33+
when='BEFORE'
34+
)
35+
),
36+
),
37+
]

app/stac_api/pgtriggers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class ItemFileSizeTrigger(pgtrigger.Trigger):
212212
'''
213213

214214
return [
215-
*auto_variables_triggers('asset'),
215+
*auto_variables_triggers('asset', 'file', 'checksum_multihash'),
216216
*child_triggers('item', 'Asset'),
217217
*asset_counter_trigger('gsdcount', 'eo_gsd'),
218218
*asset_counter_trigger('geoadminlangcount', 'geoadmin_lang'),

app/tests/tests_10/test_pgtriggers.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ def setUp(self):
8989
).model
9090

9191
@parameterized.expand([
92-
('asset', 'asset', 'checksum_multihash'),
93-
('asset', 'item', 'checksum_multihash'),
94-
('item', 'collection', 'name'),
95-
('collection_asset', 'collection', 'checksum_multihash'),
92+
('asset', 'asset', 'checksum_multihash', True),
93+
('asset', 'asset', 'description', False),
94+
('asset', 'item', 'checksum_multihash', True),
95+
('item', 'collection', 'name', True),
96+
('collection_asset', 'collection', 'checksum_multihash', True),
9697
])
97-
def test_timestamp_updated(self, source_name, destination_name, field_name):
98+
def test_timestamp_updated(self, source_name, destination_name, field_name, expect_update):
9899
destination = getattr(self, destination_name)
99100
source = getattr(self, source_name)
100101

@@ -104,8 +105,12 @@ def test_timestamp_updated(self, source_name, destination_name, field_name):
104105
source.save()
105106
destination.refresh_from_db()
106107

107-
self.assertGreater(destination.updated, prev_mtime)
108-
self.assertNotEqual(destination.etag, prev_etag)
108+
if expect_update:
109+
self.assertGreater(destination.updated, prev_mtime)
110+
self.assertNotEqual(destination.etag, prev_etag)
111+
else:
112+
self.assertEqual(destination.updated, prev_mtime)
113+
self.assertEqual(destination.etag, prev_etag)
109114

110115

111116
class PgTriggerAssetUploads(MockS3PerTestMixin, StacBaseTransactionTestCase):

0 commit comments

Comments
 (0)