From 3578b9ffbf11bec35ad079df896a56e3a4b7b3e5 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Mon, 21 Mar 2016 15:06:48 -0300 Subject: [PATCH 1/2] Cria stub para PK igual a zero Signed-off-by: Luciano Almeida --- legacy/migration.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index 5e8cf3ed6..31bfc1576 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -26,7 +26,7 @@ appconfs = [apps.get_app_config(n) for n in [ 'protocoloadm', ]] stubs_list = [] - +stub_created = False name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] # apps do not overlap @@ -91,6 +91,9 @@ def warn(msg): def get_fk_related(field, value, label=None): + has_textfield = False + fields_dict = {} + global stub_created if value is not None: try: value = field.related_model.objects.get(id=value) @@ -100,9 +103,28 @@ def get_fk_related(field, value, label=None): field.name, value, field.model.__name__, label or '---') if value == 0: - # we interpret FK == 0 as actually FK == NONE - value = None - warn(msg + ' => using NONE for zero value') + # se FK == 0, criamos um stub e colocamos o valor DESCONHECIDO + # para qualquer TextField que possa haver + all_fields = field.related_model._meta.get_fields() + for related_field in all_fields: + if related_field.get_internal_type() == 'TextField': + fields_dict[related_field.name] = 'DESCONHECIDO' + has_textfield = True + elif related_field.get_internal_type() == 'CharField': + fields_dict[related_field.name] = 'DESC' + has_textfield = True + if has_textfield and field.null is False: + if not stub_created: + stub_created = mommy.make(field.related_model, + **fields_dict) + warn(msg + ' => STUB CREATED FOR NOT NULL FIELD') + value = stub_created + else: + value = stub_created + warn(msg + ' => USING STUB ALREADY CREATED') + else: + value = None + warn(msg + ' => using NONE for zero value') else: value = make_stub(field.related_model, value) stubs_list.append((value.id, field)) @@ -163,7 +185,6 @@ class DataMigrator: for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() - if old_field_name: old_value = getattr(old, old_field_name) if isinstance(field, models.ForeignKey): @@ -219,6 +240,7 @@ class DataMigrator: legacy_model_name = self.model_renames.get(model, model.__name__) legacy_model = legacy_app.get_model(legacy_model_name) legacy_pk_name = legacy_model._meta.pk.name + global stub_created # Clear all model entries # They may have been created in a previous migration attempt @@ -229,12 +251,14 @@ class DataMigrator: # There is no pk in the legacy table def save(new, old): new.save() + stub_created = False old_records = iter_sql_records( 'select * from ' + legacy_model._meta.db_table, 'legacy') else: def save(new, old): save_with_id(new, getattr(old, legacy_pk_name)) + stub_created = False old_records = legacy_model.objects.all().order_by(legacy_pk_name) From 0ab1f3bc4a33d6d28601264cafc662b0b9a637fe Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Mon, 4 Apr 2016 13:34:29 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Arruma=20sugest=C3=B5es=20do=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- legacy/migration.py | 51 +++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index 31bfc1576..d98b17b19 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -6,6 +6,7 @@ from django.apps import apps from django.apps.config import AppConfig from django.core.exceptions import ObjectDoesNotExist from django.db import connections, models +from django.db.models import CharField, TextField from django.db.models.base import ModelBase from model_mommy import mommy @@ -26,7 +27,7 @@ appconfs = [apps.get_app_config(n) for n in [ 'protocoloadm', ]] stubs_list = [] -stub_created = False + name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] # apps do not overlap @@ -87,48 +88,41 @@ def info(msg): def warn(msg): - print('WARNING! ' + msg) + print('CUIDADO! ' + msg) def get_fk_related(field, value, label=None): - has_textfield = False fields_dict = {} - global stub_created + + if value is None and field.null is False: + value = 0 if value is not None: try: value = field.related_model.objects.get(id=value) except ObjectDoesNotExist: - msg = 'FK [%s] not found for value %s ' \ - '(in %s %s)' % ( + msg = 'FK [%s] não encontrada para valor %s ' \ + '(em %s %s)' % ( field.name, value, field.model.__name__, label or '---') if value == 0: - # se FK == 0, criamos um stub e colocamos o valor DESCONHECIDO - # para qualquer TextField que possa haver - all_fields = field.related_model._meta.get_fields() - for related_field in all_fields: - if related_field.get_internal_type() == 'TextField': - fields_dict[related_field.name] = 'DESCONHECIDO' - has_textfield = True - elif related_field.get_internal_type() == 'CharField': - fields_dict[related_field.name] = 'DESC' - has_textfield = True - if has_textfield and field.null is False: - if not stub_created: - stub_created = mommy.make(field.related_model, - **fields_dict) - warn(msg + ' => STUB CREATED FOR NOT NULL FIELD') - value = stub_created - else: - value = stub_created - warn(msg + ' => USING STUB ALREADY CREATED') + # se FK == 0, criamos um stub e colocamos o valor '???????? + # para qualquer CharField ou TextField que possa haver + if not field.null: + all_fields = field.related_model._meta.get_fields() + fields_dict = {f.name: '????????????'[:f.max_length] + for f in all_fields + if isinstance(f, (CharField, TextField)) and + not f.choices and not f.blank} + value = mommy.make(field.related_model, + **fields_dict) + warn(msg + ' => STUB criada para campos não nuláveis!') else: value = None - warn(msg + ' => using NONE for zero value') + warn(msg + ' => usando None para valores iguais a zero!') else: value = make_stub(field.related_model, value) stubs_list.append((value.id, field)) - warn(msg + ' => STUB CREATED') + warn(msg + ' => STUB criada!') else: assert value return value @@ -240,7 +234,6 @@ class DataMigrator: legacy_model_name = self.model_renames.get(model, model.__name__) legacy_model = legacy_app.get_model(legacy_model_name) legacy_pk_name = legacy_model._meta.pk.name - global stub_created # Clear all model entries # They may have been created in a previous migration attempt @@ -251,14 +244,12 @@ class DataMigrator: # There is no pk in the legacy table def save(new, old): new.save() - stub_created = False old_records = iter_sql_records( 'select * from ' + legacy_model._meta.db_table, 'legacy') else: def save(new, old): save_with_id(new, getattr(old, legacy_pk_name)) - stub_created = False old_records = legacy_model.objects.all().order_by(legacy_pk_name)