Skip to content

Commit

Permalink
added unique constraint to GuidVersionsThrough
Browse files Browse the repository at this point in the history
  • Loading branch information
bodintsov committed Dec 12, 2024
1 parent c9c85e1 commit cb5a9cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 17 additions & 0 deletions osf/migrations/0026_guidversionsthrough_unique_guid_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.15 on 2024-12-12 12:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('osf', '0025_versioned_guid_for_preprints_doi_versioning'),
]

operations = [
migrations.AddConstraint(
model_name='guidversionsthrough',
constraint=models.UniqueConstraint(fields=('guid', 'version'), name='unique_guid_version'),
),
]
6 changes: 5 additions & 1 deletion osf/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core.exceptions import MultipleObjectsReturned
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import connections, models
from django.db.models import ForeignKey
from django.db.models import ForeignKey, UniqueConstraint
from django.db.models.query import QuerySet
from django.db.models.signals import post_save
from django.dispatch import receiver
Expand Down Expand Up @@ -273,6 +273,10 @@ class GuidVersionsThrough(BaseModel):
version = models.PositiveIntegerField(null=True, blank=True)
is_rejected = models.BooleanField(default=False)

class Meta:
constraints = [
UniqueConstraint(fields=['guid', 'version'], name='unique_guid_version')
]

class BlackListGuid(BaseModel):
id = models.AutoField(primary_key=True)
Expand Down
2 changes: 1 addition & 1 deletion osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def csl(self): # formats node information into CSL format for citation parsing

@property
def is_latest_version(self):
if not self.date_published:
if self.machine_state != 'initial' and not self.date_published:
return False
return self.versioned_guids.first().guid.referent.version == self.version

Expand Down

0 comments on commit cb5a9cd

Please sign in to comment.