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

Fix metadata indexes #93

Merged
merged 3 commits into from
Feb 9, 2021
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
40 changes: 40 additions & 0 deletions dandiapi/api/migrations/0006_metadata_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 3.0.9 on 2021-02-09 15:45

import django.contrib.postgres.indexes
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0005_default_oauth_application'),
]

operations = [
migrations.RemoveIndex(
model_name='assetblob',
name='api_assetbl_sha256_828fb9_idx',
),
migrations.AlterUniqueTogether(
name='versionmetadata',
unique_together=set(),
),
migrations.AddIndex(
model_name='assetblob',
index=django.contrib.postgres.indexes.HashIndex(
fields=['sha256'], name='api_assetbl_sha256_79e05b_hash'
),
),
migrations.AddIndex(
model_name='versionmetadata',
index=django.contrib.postgres.indexes.HashIndex(
fields=['metadata'], name='api_version_metadat_1146a6_hash'
),
),
migrations.AddIndex(
model_name='versionmetadata',
index=django.contrib.postgres.indexes.HashIndex(
fields=['name'], name='api_version_name_ed2a83_hash'
),
),
]
3 changes: 2 additions & 1 deletion dandiapi/api/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.indexes import HashIndex
from django.core.files.storage import Storage
from django.core.validators import RegexValidator
from django.db import models
Expand Down Expand Up @@ -34,7 +35,7 @@ class AssetBlob(TimeStampedModel):
sha256 = models.CharField(max_length=64, validators=[RegexValidator(f'^{SHA256_REGEX}$')])

class Meta:
indexes = [models.Index(fields=['sha256'])]
indexes = [HashIndex(fields=['sha256'])]

@property
def size(self):
Expand Down
6 changes: 5 additions & 1 deletion dandiapi/api/models/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime

from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.indexes import HashIndex
from django.core.validators import RegexValidator
from django.db import models
from django_extensions.db.models import TimeStampedModel
Expand All @@ -15,7 +16,10 @@ class VersionMetadata(TimeStampedModel):
name = models.CharField(max_length=300)

class Meta:
unique_together = ['metadata', 'name']
indexes = [
HashIndex(fields=['metadata']),
HashIndex(fields=['name']),
]

@property
def references(self) -> int:
Expand Down
42 changes: 42 additions & 0 deletions dandiapi/api/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ def test_version_rest_update(api_client, user, version):
assert version.metadata.name == new_name


@pytest.mark.django_db
def test_version_rest_update_large(api_client, user, version):
assign_perm('owner', user, version.dandiset)
api_client.force_authenticate(user=user)

new_name = 'A unique and special name!'
new_metadata = {
'foo': 'bar',
'num': 123,
'list': ['a', 'b', 'c'],
'very_large': 'words' * 10000,
}
saved_metadata = {
**new_metadata,
'name': new_name,
'identifier': f'DANDI:{version.dandiset.identifier}',
}

assert api_client.put(
f'/api/dandisets/{version.dandiset.identifier}/versions/{version.version}/',
{'metadata': new_metadata, 'name': new_name},
format='json',
).data == {
'dandiset': {
'identifier': version.dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
},
'version': version.version,
'name': new_name,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'asset_count': version.asset_count,
'metadata': saved_metadata,
'size': version.size,
}

version.refresh_from_db()
assert version.metadata.metadata == saved_metadata
assert version.metadata.name == new_name


@pytest.mark.django_db
def test_version_rest_publish(api_client, user, version, asset_factory):
assign_perm('owner', user, version.dandiset)
Expand Down