Commit e2ada726 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

update migration to create virgo and IGC and link them to genes

parent 220b2f71
# Generated by Django 3.1.7 on 2021-04-12 10:07
from django.db import migrations, models
import django.db.models.deletion
def create_sources(apps, schema_editor):
Source = apps.get_model('catalog', 'Source')
items_to_create = [
{
'name': 'Virgo',
'url': 'https://virgo.igs.umaryland.edu/',
'doi': '10.1038/s41467-020-14677-3'
},
{
'name': 'IGC-9.9M',
'url': 'https://db.cngb.org/microbiome/genecatalog/genecatalog_human/',
'doi': '10.1038/s41467-020-14677-3'
},
]
for item in items_to_create:
new_entry = Source(**item)
new_entry.save()
def link_to_source(apps, schema_editor):
Gene = apps.get_model('catalog', 'Gene')
Source = apps.get_model('catalog', 'Source')
mapping_source = {
'igc': Source.objects.get(name='IGC-9.9M'),
'virgo': Source.objects.get(name='Virgo')
}
for row in Gene.objects.all():
if row.source != 'undef':
row.new_source = mapping_source.get(row.source)
row.save()
class Migration(migrations.Migration):
dependencies = [
......@@ -20,4 +52,20 @@ class Migration(migrations.Migration):
('tools', models.JSONField(blank=True, null=True)),
],
),
migrations.RunPython(create_sources),
migrations.AddField(
model_name='gene',
name='new_source',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='genes', to='catalog.source'),
),
migrations.RunPython(link_to_source),
migrations.RemoveField(
model_name='gene',
name='source',
),
migrations.RenameField(
model_name='gene',
old_name='new_source',
new_name='source',
),
]
# Generated by Django 3.1.7 on 2021-04-12 10:26
from django.db import migrations, models
import django.db.models.deletion
def link_to_source(apps, schema_editor):
Gene = apps.get_model('catalog', 'Gene')
Source = apps.get_models('catalog', 'Source')
mapping_source = {
'igc': Source.objects.get(name='IGC-9.9M'),
'virgo': Source.objects.get(name='Virgo')
}
for row in Gene.objects.all():
print(row.source)
if row.source != 'undef':
row.new_source = mapping_source.get(row.source)
row.save()
class Migration(migrations.Migration):
dependencies = [
('catalog', '0029_source'),
]
operations = [
migrations.AddField(
model_name='gene',
name='new_source',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='genes', to='catalog.source'),
),
migrations.RemoveField(
model_name='gene',
name='source',
),
migrations.RenameField(
model_name='gene',
old_name='new_source',
new_name='source',
),
]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment