def _migrate_chemical_analyses(self): print("Migrating chemical analyses...") User = get_user_model() all_references = [] old_chemical_analyses = queryset_iterator( LegacyChemicalAnalyses .objects .all() .select_related('subsample', 'reference', 'user') ) ChemicalAnalysis.objects.all().delete() for record in old_chemical_analyses: print("Migrating chemical analyses: #{}".format(record.pk)) if record.large_rock == 'Y': mineral = Mineral.objects.get(name='Bulk Rock') else: try: mineral = Mineral.objects.get(name=record.mineral.name) except AttributeError: mineral = None if record.reference: all_references.append(record.reference.name) subsample = Subsample.objects.get( name=record.subsample.name, sample=Sample.objects.get( pk=SampleMapping.objects.get( old_sample_id=record.subsample.sample.pk ).new_sample_id) ) try: reference=record.reference.name except AttributeError: reference = None try: reference_image = Image.get(subsample=subsample) except Image.DoesNotExist: reference_image = None chem_analysis = ChemicalAnalysis.objects.create( subsample=subsample, public_data=True if record.public_data == 'Y' else False, reference_x=record.reference_x, reference_y=record.reference_y, stage_x = record.stage_x, stage_y = record.stage_y, analysis_method = record.analysis_method, where_done = record.where_done, analyst = record.analyst, analysis_date = record.analysis_date, date_precision = record.date_precision, description = record.description, total = record.total, spot_id = record.spot_id, mineral=mineral, owner=User.objects.get(email=record.user.email), reference=reference, reference_image=reference_image ) legacy_cae = LegacyChemicalAnalysisElements.objects.filter( chemical_analysis=record ).select_related('element') for cae in legacy_cae: ChemicalAnalysisElement.objects.create( chemical_analysis=chem_analysis, element=Element.objects.get(name=cae.element.name), amount=cae.amount, precision = cae.precision, precision_type = cae.precision_type, measurement_unit = cae.measurement_unit, min_amount = cae.min_amount, max_amount = cae.max_amount, ) legacy_cao = LegacyChemicalAnalysisOxides.objects.filter( chemical_analysis=record ).select_related('oxide') for cao in legacy_cao: ChemicalAnalysisOxide.objects.create( chemical_analysis=chem_analysis, oxide=Oxide.objects.get(species=cao.oxide.species), amount=cao.amount, precision = cao.precision, precision_type = cao.precision_type, measurement_unit = cao.measurement_unit, min_amount = cao.min_amount, max_amount = cao.max_amount, ) if all_references: for ref in all_references: Reference.objects.get_or_create(name=ref)
def _migrate_samples(self): print("Migrating samples...") User = get_user_model() all_collectors = [] all_regions = [] old_samples = queryset_iterator(LegacySample.objects.all(), chunksize=1000) Sample.objects.all().delete() for old_sample in old_samples: print("Migrating old sample #{0}: {1}" .format(old_sample.pk, old_sample.number)) rock_type = (RockType .objects .filter(name=old_sample.rock_type.rock_type)[0]) old_user = LegacyUser.objects.get(pk=old_sample.user_id) new_user = User.objects.get(email=old_user.email) if old_sample.collector: all_collectors.extend([old_sample.collector]) regions = [lsr.region.name for lsr in LegacySampleRegion .objects .filter(sample=old_sample)] if regions: all_regions.extend(regions) references = [lsr.reference.name for lsr in LegacySampleReference .objects .filter(sample=old_sample)] new_georeferences = GeoReference.objects.filter(name__in=references) old_metamorphic_regions = (lsa.metamorphic_region.name for lsa in LegacySampleMetamorphicRegion .objects .filter(sample=old_sample)) new_metamorphic_regions = ( MetamorphicRegion .objects .filter(name__in=old_metamorphic_regions) ) old_metamorphic_grades = (lsa.metamorphic_grade.name for lsa in LegacySampleMetamorphicGrades .objects .filter(sample=old_sample)) new_metamorphic_grades = (MetamorphicGrade .objects .filter(name__in=old_metamorphic_grades)) aliases = [lsa.alias for lsa in LegacySampleAlias .objects .filter(sample=old_sample)] new_sample = Sample.objects.create( public_data=True if old_sample.public_data == 'Y' else False, number=old_sample.number, owner=new_user, aliases=aliases, collection_date=old_sample.collection_date, date_precision=old_sample.date_precision, country=old_sample.country, description=old_sample.description, location_name=old_sample.location_text, location_coords=old_sample.location, location_error=old_sample.location_error, rock_type=rock_type, regions=regions, collector_name=old_sample.collector ) SampleMapping.objects.create(old_sample_id=old_sample.pk, new_sample_id=new_sample.pk) self._migrate_subsamples(old_sample, new_sample) new_sample.metamorphic_regions.add(*new_metamorphic_regions) new_sample.metamorphic_grades.add(*new_metamorphic_grades) new_sample.references.add(*new_georeferences) old_minerals = (LegacySampleMineral .objects .filter(sample=old_sample)) old_mineral_names = [om.mineral.name for om in old_minerals] new_minerals = Mineral.objects.filter(name__in=old_mineral_names) for mineral in new_minerals: old_sample_mineral = (LegacySampleMineral .objects .get(mineral__name=mineral.name, sample=old_sample)) SampleMineral.objects.create(sample=new_sample, mineral=mineral, amount=old_sample_mineral.amount) Region.objects.all().delete() Region.objects.bulk_create([Region(name=region) for region in set(all_regions)]) Collector.objects.all().delete() Collector.objects.bulk_create([Collector(name=collector) for collector in set(all_collectors)])
def _migrate_samples(self): print("Migrating samples...") User = get_user_model() all_collectors = set() all_regions = set() old_samples = queryset_iterator(LegacySample.objects.all(), chunksize=1000) Sample.objects.all().delete() for old_sample in old_samples: print("Migrating old sample #{0}: {1}".format( old_sample.pk, old_sample.number)) rock_type = (RockType.objects.filter( name=old_sample.rock_type.rock_type)[0]) old_user = LegacyUser.objects.get(pk=old_sample.user_id) new_user = User.objects.get(email=old_user.email) if old_sample.collector: all_collectors.add(old_sample.collector) regions = [ lsr.region.name for lsr in LegacySampleRegion.objects.filter(sample=old_sample) ] if regions: all_regions.update(set(regions)) new_georeferences = self._get_georeferences(old_sample) new_metamorphic_regions = self._get_metamorphic_regions(old_sample) new_metamorphic_grades = self._get_metamorphic_grades(old_sample) aliases = [ lsa.alias for lsa in LegacySampleAlias.objects.filter(sample=old_sample) ] new_sample = Sample.objects.create( public_data=old_sample.public_data == 'Y', number=old_sample.number, owner=new_user, aliases=aliases, collection_date=old_sample.collection_date, date_precision=old_sample.date_precision, country=old_sample.country, description=old_sample.description, location_name=old_sample.location_text, location_coords=old_sample.location, location_error=old_sample.location_error, rock_type=rock_type, regions=regions, collector_name=old_sample.collector) SampleMapping.objects.create(old_sample_id=old_sample.pk, new_sample_id=new_sample.pk) self._migrate_subsamples(old_sample, new_sample=new_sample) old_images = LegacyImages.objects.filter(sample=old_sample) self._migrate_images(old_images, new_sample) new_sample.metamorphic_regions.add(*new_metamorphic_regions) new_sample.metamorphic_grades.add(*new_metamorphic_grades) new_sample.references.add(*new_georeferences) self._migrate_sample_minerals(old_sample, new_sample) Region.objects.all().delete() Region.objects.bulk_create( [Region(name=region) for region in all_regions]) Collector.objects.all().delete() Collector.objects.bulk_create( [Collector(name=collector) for collector in all_collectors])
def _migrate_samples(self): print("Migrating samples...") User = get_user_model() all_collectors = [] all_regions = [] old_samples = queryset_iterator(LegacySample.objects.all(), chunksize=1000) Sample.objects.all().delete() for old_sample in old_samples: print("Migrating old sample #{0}: {1}".format( old_sample.pk, old_sample.number)) rock_type = (RockType.objects.filter( name=old_sample.rock_type.rock_type)[0]) old_user = LegacyUser.objects.get(pk=old_sample.user_id) new_user = User.objects.get(email=old_user.email) if old_sample.collector: all_collectors.extend([old_sample.collector]) regions = [ lsr.region.name for lsr in LegacySampleRegion.objects.filter(sample=old_sample) ] if regions: all_regions.extend(regions) references = [ lsr.reference.name for lsr in LegacySampleReference.objects.filter( sample=old_sample) ] new_georeferences = GeoReference.objects.filter( name__in=references) old_metamorphic_regions = ( lsa.metamorphic_region.name for lsa in LegacySampleMetamorphicRegion.objects.filter( sample=old_sample)) new_metamorphic_regions = (MetamorphicRegion.objects.filter( name__in=old_metamorphic_regions)) old_metamorphic_grades = ( lsa.metamorphic_grade.name for lsa in LegacySampleMetamorphicGrades.objects.filter( sample=old_sample)) new_metamorphic_grades = (MetamorphicGrade.objects.filter( name__in=old_metamorphic_grades)) aliases = [ lsa.alias for lsa in LegacySampleAlias.objects.filter(sample=old_sample) ] new_sample = Sample.objects.create( public_data=True if old_sample.public_data == 'Y' else False, number=old_sample.number, owner=new_user, aliases=aliases, collection_date=old_sample.collection_date, date_precision=old_sample.date_precision, country=old_sample.country, description=old_sample.description, location_name=old_sample.location_text, location_coords=old_sample.location, location_error=old_sample.location_error, rock_type=rock_type, regions=regions, collector_name=old_sample.collector) SampleMapping.objects.create(old_sample_id=old_sample.pk, new_sample_id=new_sample.pk) self._migrate_subsamples(old_sample, new_sample) new_sample.metamorphic_regions.add(*new_metamorphic_regions) new_sample.metamorphic_grades.add(*new_metamorphic_grades) new_sample.references.add(*new_georeferences) old_minerals = (LegacySampleMineral.objects.filter( sample=old_sample)) old_mineral_names = [om.mineral.name for om in old_minerals] new_minerals = Mineral.objects.filter(name__in=old_mineral_names) for mineral in new_minerals: old_sample_mineral = (LegacySampleMineral.objects.get( mineral__name=mineral.name, sample=old_sample)) SampleMineral.objects.create(sample=new_sample, mineral=mineral, amount=old_sample_mineral.amount) Region.objects.all().delete() Region.objects.bulk_create( [Region(name=region) for region in set(all_regions)]) Collector.objects.all().delete() Collector.objects.bulk_create( [Collector(name=collector) for collector in set(all_collectors)])
def _migrate_chemical_analyses(self): print("Migrating chemical analyses...") User = get_user_model() all_references = [] old_chemical_analyses = queryset_iterator( LegacyChemicalAnalyses .objects .all() .select_related('subsample', 'reference', 'user') ) ChemicalAnalysis.objects.all().delete() for record in old_chemical_analyses: print("Migrating chemical analyses: #{}".format(record.pk)) if record.large_rock == 'Y': mineral = Mineral.objects.get(name='Bulk Rock') else: try: mineral = Mineral.objects.get(name=record.mineral.name) except AttributeError: mineral = None if record.reference: all_references.append(record.reference.name) subsample = Subsample.objects.get( name=record.subsample.name, sample=Sample.objects.get( pk=SampleMapping.objects.get( old_sample_id=record.subsample.sample.pk ).new_sample_id) ) try: reference=record.reference.name except AttributeError: reference = None chem_analysis = ChemicalAnalysis.objects.create( subsample=subsample, public_data=True if record.public_data == 'Y' else False, reference_x=record.reference_x, reference_y=record.reference_y, stage_x = record.stage_x, stage_y = record.stage_y, analysis_method = record.analysis_method, where_done = record.where_done, analyst = record.analyst, analysis_date = record.analysis_date, date_precision = record.date_precision, description = record.description, total = record.total, spot_id = record.spot_id, mineral=mineral, owner=User.objects.get(email=record.user.email), reference=reference, ) legacy_cae = LegacyChemicalAnalysisElements.objects.filter( chemical_analysis=record ).select_related('element') for cae in legacy_cae: ChemicalAnalysisElement.objects.create( chemical_analysis=chem_analysis, element=Element.objects.get(name=cae.element.name), amount=cae.amount, precision = cae.precision, precision_type = cae.precision_type, measurement_unit = cae.measurement_unit, min_amount = cae.min_amount, max_amount = cae.max_amount, ) legacy_cao = LegacyChemicalAnalysisOxides.objects.filter( chemical_analysis=record ).select_related('oxide') for cao in legacy_cao: ChemicalAnalysisOxide.objects.create( chemical_analysis=chem_analysis, oxide=Oxide.objects.get(species=cao.oxide.species), amount=cao.amount, precision = cao.precision, precision_type = cao.precision_type, measurement_unit = cao.measurement_unit, min_amount = cao.min_amount, max_amount = cao.max_amount, ) if all_references: for ref in all_references: Reference.objects.get_or_create(name=ref)