Skip to content
Snippets Groups Projects
Commit d31a21c6 authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

migrate proteins and choose migrated models with arguments

	# Please enter the commit message for your changes. Lines starting
parent 16ed6a33
No related branches found
No related tags found
No related merge requests found
from django.core.management import BaseCommand from django.core.management import BaseCommand
import mysql.connector import mysql.connector
from ippidb.models import Bibliography from ippidb.models import Bibliography, Protein
class Command(BaseCommand): class Command(BaseCommand):
help = "Import data from the local v1 database" help = "Import data from the local v1 database"
def add_arguments(self, parser):
parser.add_argument(
'--bibliographies',
action='store_true',
dest='bibliographies',
default=False,
help='Flush and migrate bibliographies',
)
parser.add_argument(
'--proteins',
action='store_true',
dest='proteins',
default=False,
help='Flush and migrate proteins',
)
def handle(self, *args, **options): def handle(self, *args, **options):
conn = mysql.connector.connect(host="localhost",user="root",password="ippidb", database="ippidb") conn = mysql.connector.connect(host="localhost",user="root",password="ippidb", database="ippidb")
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("""SELECT * FROM biblio""") if options['bibliographies']:
rows = cursor.fetchall() cursor.execute("""SELECT * FROM biblio""")
Bibliography.objects.all().delete() rows = cursor.fetchall()
self.stdout.write(self.style.SUCCESS('Successfully flushed bibliography table')) Bibliography.objects.all().delete()
for row in rows: self.stdout.write(self.style.SUCCESS('Successfully flushed bibliography table'))
try: for row in rows:
b = Bibliography() try:
if row[1]=='article': b = Bibliography()
b.source = 'PM' if row[1]=='article':
b.source = 'PM'
else:
b.source = 'PT'
b.id_source = row[2]
b.save()
except Exception as e:
self.stdout.write(self.style.ERROR('Failed inserting {}'.format(row[2])))
else:
self.stdout.write(self.style.SUCCESS('Successfully inserted {}'.format(row[2])))
if options['proteins']:
cursor.execute("""SELECT * FROM protein""")
rows = cursor.fetchall()
Protein.objects.all().delete()
self.stdout.write(self.style.SUCCESS('Successfully flushed protein table'))
for row in rows:
try:
p = Protein()
p.uniprot_id = row[1]
p.save()
except Exception as e:
self.stdout.write(self.style.ERROR('Failed inserting {} {}'.format(row[1], row[2])))
else: else:
b.source = 'PT' self.stdout.write(self.style.SUCCESS('Successfully inserted {} {}'.format(row[1], row[2])))
b.id_source = row[2]
b.save()
except Exception as e:
self.stdout.write(self.style.ERROR('Failed inserting {}'.format(row[2])))
else:
self.stdout.write(self.style.SUCCESS('Successfully inserted {}'.format(row[2])))
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