Skip to content

Commit

Permalink
Fix DJ001 Disallow null CharField on BaseZarrArchive.checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba committed Nov 9, 2023
1 parent b38aa93 commit b8ed49d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 4.1.13 on 2023-11-09 00:56

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
'zarr',
'0004_remove_embargoedzarrarchive_zarr-embargoedzarrarchive-consistent-checksum-status_and_more',
),
]

operations = [
migrations.RemoveConstraint(
model_name='embargoedzarrarchive',
name='zarr-embargoedzarrarchive-consistent-checksum-status',
),
migrations.RemoveConstraint(
model_name='zarrarchive',
name='zarr-zarrarchive-consistent-checksum-status',
),
migrations.AlterField(
model_name='embargoedzarrarchive',
name='checksum',
field=models.CharField(default='', max_length=512),
preserve_default=False,
),
migrations.AlterField(
model_name='zarrarchive',
name='checksum',
field=models.CharField(default='', max_length=512),
preserve_default=False,
),
migrations.AddConstraint(
model_name='embargoedzarrarchive',
constraint=models.CheckConstraint(
check=models.Q(
models.Q(
('checksum', ''), ('status__in', ['Pending', 'Uploaded', 'Ingesting'])
),
models.Q(models.Q(('checksum', ''), _negated=True), ('status', 'Complete')),
_connector='OR',
),
name='zarr-embargoedzarrarchive-consistent-checksum-status',
),
),
migrations.AddConstraint(
model_name='zarrarchive',
constraint=models.CheckConstraint(
check=models.Q(
models.Q(
('checksum', ''), ('status__in', ['Pending', 'Uploaded', 'Ingesting'])
),
models.Q(models.Q(('checksum', ''), _negated=True), ('status', 'Complete')),
_connector='OR',
),
name='zarr-zarrarchive-consistent-checksum-status',
),
),
]
8 changes: 4 additions & 4 deletions dandiapi/zarr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ class Meta:
models.CheckConstraint(
name='%(app_label)s-%(class)s-consistent-checksum-status',
check=models.Q(
checksum__isnull=True,
checksum='',
status__in=[
ZarrArchiveStatus.PENDING,
ZarrArchiveStatus.UPLOADED,
ZarrArchiveStatus.INGESTING,
],
)
| models.Q(checksum__isnull=False, status=ZarrArchiveStatus.COMPLETE),
| (~models.Q(checksum='') & models.Q(status=ZarrArchiveStatus.COMPLETE)),
),
]

zarr_id = models.UUIDField(unique=True, default=uuid4, db_index=True)
name = models.CharField(max_length=512)
file_count = models.BigIntegerField(default=0)
size = models.BigIntegerField(default=0)
checksum = models.CharField(max_length=512, null=True, default=None)
checksum = models.CharField(max_length=512)
status = models.CharField(
max_length=max(len(choice[0]) for choice in ZarrArchiveStatus.choices),
choices=ZarrArchiveStatus.choices,
Expand All @@ -80,7 +80,7 @@ def generate_upload_urls(self, path_md5s: list[dict]):
]

def mark_pending(self):
self.checksum = None
self.checksum = ''
self.status = ZarrArchiveStatus.PENDING
self.file_count = 0
self.size = 0
Expand Down
2 changes: 1 addition & 1 deletion dandiapi/zarr/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def ingest_zarr_archive(zarr_id: str, force: bool = False):

# Set as ingesting
zarr.status = ZarrArchiveStatus.INGESTING
zarr.checksum = None
zarr.checksum = ''
zarr.save(update_fields=['status', 'checksum'])

# Zarr is in correct state, lock until ingestion finishes
Expand Down
8 changes: 4 additions & 4 deletions dandiapi/zarr/tests/test_ingest_zarr_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_ingest_zarr_archive(zarr_archive_factory, zarr_file_factory):
total_size = sum([f.size for f in files])

# Assert pre-ingest properties
assert zarr.checksum is None
assert zarr.checksum == ''
assert zarr.size == 0
assert zarr.file_count == 0
assert zarr.status == ZarrArchiveStatus.UPLOADED
Expand All @@ -32,7 +32,7 @@ def test_ingest_zarr_archive(zarr_archive_factory, zarr_file_factory):

# Assert checksum properly computed
zarr.refresh_from_db()
assert zarr.checksum is not None
assert zarr.checksum != ''
assert zarr.size == total_size
assert zarr.file_count == 2
assert zarr.status == ZarrArchiveStatus.COMPLETE
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_ingest_zarr_archive_force(zarr_archive_factory, zarr_file_factory):
# Perform ingest with force flag, assert updated
ingest_zarr_archive(str(zarr.zarr_id), force=True)
zarr.refresh_from_db()
assert zarr.checksum is not None and zarr.checksum != first_checksum
assert zarr.checksum != '' and zarr.checksum != first_checksum


@pytest.mark.django_db(transaction=True)
Expand Down Expand Up @@ -158,4 +158,4 @@ def test_ingest_dandiset_zarrs(dandiset_factory, zarr_archive_factory, zarr_file
for zarr in dandiset.zarr_archives.all():
assert zarr.size != 0
assert zarr.file_count != 0
assert zarr.checksum is not None
assert zarr.checksum != ''
2 changes: 1 addition & 1 deletion dandiapi/zarr/tests/test_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_zarr_rest_create(authenticated_api_client, user, dandiset):
'zarr_id': UUID_RE,
'dandiset': dandiset.identifier,
'status': ZarrArchiveStatus.PENDING,
'checksum': None,
'checksum': '',
'file_count': 0,
'size': 0,
}
Expand Down
4 changes: 2 additions & 2 deletions dandiapi/zarr/tests/test_zarr_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_zarr_rest_upload_start(
# Assert fields updated
zarr_archive.refresh_from_db()
assert zarr_archive.status == ZarrArchiveStatus.PENDING
assert zarr_archive.checksum is None
assert zarr_archive.checksum == ''
assert zarr_archive.file_count == 0
assert zarr_archive.size == 0

Expand Down Expand Up @@ -80,7 +80,7 @@ def test_zarr_rest_finalize(

# Check that zarr ingestion occurred
zarr_archive.refresh_from_db()
assert zarr_archive.checksum is not None
assert zarr_archive.checksum != ''
assert zarr_archive.checksum != EMPTY_CHECKSUM
assert zarr_archive.status == ZarrArchiveStatus.COMPLETE

Expand Down

0 comments on commit b8ed49d

Please sign in to comment.