|
@ -26,7 +26,7 @@ appconfs = [apps.get_app_config(n) for n in [ |
|
|
'protocoloadm', ]] |
|
|
'protocoloadm', ]] |
|
|
|
|
|
|
|
|
stubs_list = [] |
|
|
stubs_list = [] |
|
|
|
|
|
stub_created = False |
|
|
name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] |
|
|
name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] |
|
|
|
|
|
|
|
|
# apps do not overlap |
|
|
# apps do not overlap |
|
@ -91,6 +91,9 @@ def warn(msg): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_fk_related(field, value, label=None): |
|
|
def get_fk_related(field, value, label=None): |
|
|
|
|
|
has_textfield = False |
|
|
|
|
|
fields_dict = {} |
|
|
|
|
|
global stub_created |
|
|
if value is not None: |
|
|
if value is not None: |
|
|
try: |
|
|
try: |
|
|
value = field.related_model.objects.get(id=value) |
|
|
value = field.related_model.objects.get(id=value) |
|
@ -100,9 +103,28 @@ def get_fk_related(field, value, label=None): |
|
|
field.name, value, |
|
|
field.name, value, |
|
|
field.model.__name__, label or '---') |
|
|
field.model.__name__, label or '---') |
|
|
if value == 0: |
|
|
if value == 0: |
|
|
# we interpret FK == 0 as actually FK == NONE |
|
|
# se FK == 0, criamos um stub e colocamos o valor DESCONHECIDO |
|
|
value = None |
|
|
# para qualquer TextField que possa haver |
|
|
warn(msg + ' => using NONE for zero value') |
|
|
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: |
|
|
else: |
|
|
value = make_stub(field.related_model, value) |
|
|
value = make_stub(field.related_model, value) |
|
|
stubs_list.append((value.id, field)) |
|
|
stubs_list.append((value.id, field)) |
|
@ -163,7 +185,6 @@ class DataMigrator: |
|
|
for field in new._meta.fields: |
|
|
for field in new._meta.fields: |
|
|
old_field_name = renames.get(field.name) |
|
|
old_field_name = renames.get(field.name) |
|
|
field_type = field.get_internal_type() |
|
|
field_type = field.get_internal_type() |
|
|
|
|
|
|
|
|
if old_field_name: |
|
|
if old_field_name: |
|
|
old_value = getattr(old, old_field_name) |
|
|
old_value = getattr(old, old_field_name) |
|
|
if isinstance(field, models.ForeignKey): |
|
|
if isinstance(field, models.ForeignKey): |
|
@ -219,6 +240,7 @@ class DataMigrator: |
|
|
legacy_model_name = self.model_renames.get(model, model.__name__) |
|
|
legacy_model_name = self.model_renames.get(model, model.__name__) |
|
|
legacy_model = legacy_app.get_model(legacy_model_name) |
|
|
legacy_model = legacy_app.get_model(legacy_model_name) |
|
|
legacy_pk_name = legacy_model._meta.pk.name |
|
|
legacy_pk_name = legacy_model._meta.pk.name |
|
|
|
|
|
global stub_created |
|
|
|
|
|
|
|
|
# Clear all model entries |
|
|
# Clear all model entries |
|
|
# They may have been created in a previous migration attempt |
|
|
# They may have been created in a previous migration attempt |
|
@ -229,12 +251,14 @@ class DataMigrator: |
|
|
# There is no pk in the legacy table |
|
|
# There is no pk in the legacy table |
|
|
def save(new, old): |
|
|
def save(new, old): |
|
|
new.save() |
|
|
new.save() |
|
|
|
|
|
stub_created = False |
|
|
|
|
|
|
|
|
old_records = iter_sql_records( |
|
|
old_records = iter_sql_records( |
|
|
'select * from ' + legacy_model._meta.db_table, 'legacy') |
|
|
'select * from ' + legacy_model._meta.db_table, 'legacy') |
|
|
else: |
|
|
else: |
|
|
def save(new, old): |
|
|
def save(new, old): |
|
|
save_with_id(new, getattr(old, legacy_pk_name)) |
|
|
save_with_id(new, getattr(old, legacy_pk_name)) |
|
|
|
|
|
stub_created = False |
|
|
|
|
|
|
|
|
old_records = legacy_model.objects.all().order_by(legacy_pk_name) |
|
|
old_records = legacy_model.objects.all().order_by(legacy_pk_name) |
|
|
|
|
|
|
|
|