-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2010 from dandi/asset-access-metadata
Derive asset `access` field from asset blob
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Generated by Django 4.1.13 on 2024-08-20 17:21 | ||
from __future__ import annotations | ||
|
||
from django.db import migrations, models | ||
from django.db.models.expressions import RawSQL | ||
|
||
|
||
def remove_access_fields(apps, _): | ||
Asset = apps.get_model('api.Asset') | ||
AuditRecord = apps.get_model('api.AuditRecord') | ||
|
||
# Use the postgres jsonb '-' operator to delete the 'access' field from metadata | ||
Asset.objects.filter(published=False, metadata__access__isnull=False).update( | ||
metadata=RawSQL("metadata - 'access'", []) | ||
) | ||
|
||
# Delete access field from existing audit records | ||
# https://www.postgresql.org/docs/current/functions-json.html#:~:text=jsonb%20%23%2D%20text%5B%5D%20%E2%86%92%20jsonb | ||
AuditRecord.objects.filter(record_type__in=['add_asset', 'update_asset']).update( | ||
details=RawSQL("details #- '{metadata, access}'", []) | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('api', '0010_auditrecord'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(remove_access_fields), | ||
migrations.RemoveConstraint( | ||
model_name='asset', | ||
name='asset_metadata_no_computed_keys_or_published', | ||
), | ||
migrations.AddConstraint( | ||
model_name='asset', | ||
constraint=models.CheckConstraint( | ||
check=models.Q( | ||
models.Q( | ||
('published', False), | ||
models.Q( | ||
( | ||
'metadata__has_any_keys', | ||
[ | ||
'id', | ||
'access', | ||
'path', | ||
'identifier', | ||
'contentUrl', | ||
'contentSize', | ||
'digest', | ||
'datePublished', | ||
'publishedBy', | ||
], | ||
), | ||
_negated=True, | ||
), | ||
), | ||
models.Q( | ||
('published', True), | ||
( | ||
'metadata__has_keys', | ||
[ | ||
'id', | ||
'access', | ||
'path', | ||
'identifier', | ||
'contentUrl', | ||
'contentSize', | ||
'digest', | ||
'datePublished', | ||
'publishedBy', | ||
], | ||
), | ||
), | ||
_connector='OR', | ||
), | ||
name='asset_metadata_no_computed_keys_or_published', | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters