From 47591eb787f22ed5c80a27418c0127624efc9e95 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Tue, 7 Jul 2015 10:38:46 -0300 Subject: [PATCH] Move data migration code to one module --- legacy/{scripts => }/migration.py | 76 +++++++++++++++++++++++++++++-- legacy/scripts/field_renames.py | 39 ---------------- legacy/scripts/migration_base.py | 25 ---------- 3 files changed, 72 insertions(+), 68 deletions(-) rename legacy/{scripts => }/migration.py (74%) delete mode 100644 legacy/scripts/field_renames.py delete mode 100644 legacy/scripts/migration_base.py diff --git a/legacy/scripts/migration.py b/legacy/migration.py similarity index 74% rename from legacy/scripts/migration.py rename to legacy/migration.py index 87817be79..523c5a89b 100644 --- a/legacy/scripts/migration.py +++ b/legacy/migration.py @@ -1,15 +1,83 @@ +import re + +import pkg_resources +import yaml +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.base import ModelBase from model_mommy import mommy -from field_renames import field_renames, model_renames -from migration_base import legacy_app, appconfs - -from parlamentares.models import Parlamentar from comissoes.models import Composicao, Participacao +from parlamentares.models import Parlamentar + + +# BASE ###################################################################### + +# this order is important for the migration +appconfs = [apps.get_app_config(n) for n in [ + 'parlamentares', + 'comissoes', + 'materia', + 'norma', + 'sessao', + 'lexml', + 'protocoloadm', ]] +name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] + +# apps do not overlap +for s1 in name_sets: + for s2 in name_sets: + if s1 is not s2: + assert not s1.intersection(s2) +# apps include all legacy models +legacy_app = apps.get_app_config('legacy') +legacy_model_names = set(m.__name__ for m in legacy_app.get_models()) + +model_dict = {m.__name__: m for ac in appconfs for m in ac.get_models()} + + +# RENAMES ################################################################### + +MODEL_RENAME_PATTERN = re.compile('(.+) \((.+)\)') + + +def get_renames(): + field_renames = {} + model_renames = {} + for app in appconfs: + app_rename_data = yaml.load(pkg_resources.resource_string(app.module.__name__, 'legacy.yaml')) + for model_name, renames in app_rename_data.items(): + match = MODEL_RENAME_PATTERN.match(model_name) + if match: + model_name, old_name = match.groups() + else: + old_name = None + model = getattr(app.models_module, model_name) + if old_name: + model_renames[model] = old_name + field_renames[model] = renames + + # collect renames from parent classes + for model, renames in field_renames.items(): + if any(parent in field_renames for parent in model.__mro__[1:]): + renames = {} + for parent in reversed(model.__mro__): + if parent in field_renames: + renames.update(field_renames[parent]) + field_renames[model] = renames + + # remove abstract classes + for model in field_renames: + if model._meta.abstract: + del field_renames[model] + + return field_renames, model_renames + + +# MIGRATION ################################################################# def info(msg): print 'INFO: ' + msg diff --git a/legacy/scripts/field_renames.py b/legacy/scripts/field_renames.py deleted file mode 100644 index f17936864..000000000 --- a/legacy/scripts/field_renames.py +++ /dev/null @@ -1,39 +0,0 @@ -import re - -import yaml -import pkg_resources - -from migration_base import appconfs - - -MODEL_RENAME_PATTERN = re.compile('(.+) \((.+)\)') - - -field_renames = {} -model_renames = {} -for app in appconfs: - app_rename_data = yaml.load(pkg_resources.resource_string(app.module.__name__, 'legacy.yaml')) - for model_name, renames in app_rename_data.items(): - match = MODEL_RENAME_PATTERN.match(model_name) - if match: - model_name, old_name = match.groups() - else: - old_name = None - model = getattr(app.models_module, model_name) - if old_name: - model_renames[model] = old_name - field_renames[model] = renames - -# collect renames from parent classes -for model, renames in field_renames.items(): - if any(parent in field_renames for parent in model.__mro__[1:]): - renames = {} - for parent in reversed(model.__mro__): - if parent in field_renames: - renames.update(field_renames[parent]) - field_renames[model] = renames - -# remove abstract classes -for model in field_renames: - if model._meta.abstract: - del field_renames[model] diff --git a/legacy/scripts/migration_base.py b/legacy/scripts/migration_base.py deleted file mode 100644 index 716d1bc23..000000000 --- a/legacy/scripts/migration_base.py +++ /dev/null @@ -1,25 +0,0 @@ -from django.apps import apps - - -# this order is important for the migration -appconfs = [apps.get_app_config(n) for n in [ - 'parlamentares', - 'comissoes', - 'materia', - 'norma', - 'sessao', - 'lexml', - 'protocoloadm', ]] -name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] - -# apps do not overlap -for s1 in name_sets: - for s2 in name_sets: - if s1 is not s2: - assert not s1.intersection(s2) - -# apps include all legacy models -legacy_app = apps.get_app_config('legacy') -legacy_model_names = set(m.__name__ for m in legacy_app.get_models()) - -model_dict = {m.__name__: m for ac in appconfs for m in ac.get_models()}