Skip to content

Commit

Permalink
WIP spdatasetattachment migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alesan99 committed Feb 25, 2025
1 parent e422e17 commit ddd1805
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
83 changes: 83 additions & 0 deletions specifyweb/workbench/migrations/0006_spdatasetattachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generated by Django 3.2.15 on 2025-02-25 16:43
'''
This migration creates the spdatasetattachment table and adds it to the schema config.
'''

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import specifyweb.specify.models
from specifyweb.specify.migration_utils.update_schema_config import revert_table_field_schema_config, update_table_field_schema_config_with_defaults

MIGRATION_0006_FIELDS = {
'SpdatasetAttachment': [
('id', 'id', 'id'),
('ordinal', 'ordinal', 'ordinal'),
('remarks', 'remarks', 'remarks'),
('timestampcreated', 'timestampcreated', 'timestampcreated'),
('timestampmodified', 'timestampmodified', 'timestampmodified'),
('version', 'version', 'version'),
('attachment', 'attachment', 'attachment'),
('createdbyagent', 'createdbyagent', 'createdbyagent'),
('modifiedbyagent', 'modifiedbyagent', 'modifiedbyagent'),
('storage', 'storage', 'storage'),
],
}

def create_spdatasetattachment(apps):
migrations.CreateModel(
name='SpdatasetAttachment',
fields=[
('id', models.AutoField(db_column='spdatasetattachmentid', primary_key=True, serialize=False)),
('ordinal', models.IntegerField(db_column='Ordinal')),
('remarks', models.TextField(blank=True, db_column='Remarks', null=True)),
('timestampcreated', models.DateTimeField(db_column='TimestampCreated', default=django.utils.timezone.now)),
('timestampmodified', models.DateTimeField(blank=True, db_column='TimestampModified', default=django.utils.timezone.now, null=True)),
('version', models.IntegerField(blank=True, db_column='Version', default=0, null=True)),
('attachment', models.ForeignKey(db_column='AttachmentID', on_delete=specifyweb.specify.models.protect_with_blockers, related_name='spdatasetattachments', to='specify.attachment')),
('createdbyagent', models.ForeignKey(db_column='CreatedByAgentID', null=True, on_delete=specifyweb.specify.models.protect_with_blockers, related_name='+', to='specify.agent')),
('modifiedbyagent', models.ForeignKey(db_column='ModifiedByAgentID', null=True, on_delete=specifyweb.specify.models.protect_with_blockers, related_name='+', to='specify.agent')),
('spdataset', models.ForeignKey(db_column='id', on_delete=django.db.models.deletion.CASCADE, related_name='spdatasetattachments', to='workbench.spdataset')),
],
options={
'db_table': 'spdatasetattachment',
'ordering': (),
},
),

def reverse_create_spdatasetattachment(apps):
migrations.DeleteModel(
name='SpdatasetAttachment',
),

def create_spdatasetattachment_splocalecontaineritem(apps):
Discipline = apps.get_model('specify', 'Discipline')
for discipline in Discipline.objects.all():
for table, fields in MIGRATION_0006_FIELDS.items():
for field in fields:
update_table_field_schema_config_with_defaults(
table, discipline.id, field, apps)

def reverse_create_spdatasetattachment_splocalecontaineritem(apps):
for table, fields in MIGRATION_0006_FIELDS.items():
for field in fields:
revert_table_field_schema_config(table, field, apps)

class Migration(migrations.Migration):

dependencies = [
('specify', '0023_update_schema_config_text'),
('workbench', '0005_auto_20210428_1634'),
]

def apply_migration(apps, schema_editor):
create_spdatasetattachment(apps)
create_spdatasetattachment_splocalecontaineritem(apps)

def revert_migration(apps, schema_editor):
reverse_create_spdatasetattachment(apps)
reverse_create_spdatasetattachment_splocalecontaineritem(apps)

operations = [
migrations.RunPython(apply_migration, revert_migration, atomic=True)
]
13 changes: 6 additions & 7 deletions specifyweb/workbench/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.http import Http404
from django.utils import timezone

from specifyweb.specify.models import Collection, Specifyuser, Agent, datamodel, custom_save, protect_with_blockers
from specifyweb.specify.models import Collection, Specifyuser, Agent, Attachment, datamodel, custom_save, protect_with_blockers
from specifyweb.specify.api import uri_for_model

class Dataset(models.Model):
Expand Down Expand Up @@ -44,7 +44,7 @@ def get_meta_fields(cls, request, extra_meta_fields=None, extra_filters=None):
collection=request.specify_collection,
**(extra_filters if extra_filters is not None else {})
).only(*attrs)
return []
return [{'id': ds.id, **{attr: getattr(ds, attr) for attr in attrs}, 'uploadplan': json.loads(ds.uploadplan) if ds.uploadplan else None} for ds in dss]

# raise_404: Whether to raise 404 or return http 404.
# lock_object: Whether to run a "select for update" or "select"
Expand Down Expand Up @@ -119,7 +119,6 @@ class SpdatasetAttachment(models.Model):
# ID Field
id = models.AutoField(primary_key=True, db_column='spdatasetattachmentid')

# TODO: Dont't use lazy references
# Fields
ordinal = models.IntegerField(blank=False, null=False, unique=False, db_column='Ordinal', db_index=False)
remarks = models.TextField(blank=True, null=True, unique=False, db_column='Remarks', db_index=False)
Expand All @@ -128,10 +127,10 @@ class SpdatasetAttachment(models.Model):
version = models.IntegerField(blank=True, null=True, unique=False, db_column='Version', db_index=False, default=0)

# Relationships: Many-to-One
spdataset = models.ForeignKey('Spdataset', db_column='id', related_name='spdatasetattachments', null=False, on_delete=models.CASCADE)
attachment = models.ForeignKey('Attachment', db_column='AttachmentID', related_name='spdatasetattachments', null=False, on_delete=protect_with_blockers)
createdbyagent = models.ForeignKey('Agent', db_column='CreatedByAgentID', related_name='+', null=True, on_delete=protect_with_blockers)
modifiedbyagent = models.ForeignKey('Agent', db_column='ModifiedByAgentID', related_name='+', null=True, on_delete=protect_with_blockers)
spdataset = models.ForeignKey(Spdataset, db_column='id', related_name='spdatasetattachments', null=False, on_delete=models.CASCADE)
attachment = models.ForeignKey(Attachment, db_column='AttachmentID', related_name='spdatasetattachments', null=False, on_delete=protect_with_blockers)
createdbyagent = models.ForeignKey(Agent, db_column='CreatedByAgentID', related_name='+', null=True, on_delete=protect_with_blockers)
modifiedbyagent = models.ForeignKey(Agent, db_column='ModifiedByAgentID', related_name='+', null=True, on_delete=protect_with_blockers)

class Meta:
db_table = 'spdatasetattachment'
Expand Down

0 comments on commit ddd1805

Please sign in to comment.