Browse Source

Update migration code to use field mappings

pull/6/head
Marcio Mazza 10 years ago
parent
commit
4df82ae2a5
  1. 58
      legacy/scripts/migrate.py
  2. 3
      legacy/scripts/migration_base.py

58
legacy/scripts/migrate.py

@ -1,33 +1,16 @@
from django.db import connection, models from django.db import connection, models
import legacy from field_mappings import field_mappings
from parlamentares.models import * from migration_base import appconfs, legacy_app
mappings = ( def migrate_all():
(Legislatura, 'num_legislatura', [ for app in appconfs:
('dat_inicio', 'data_inicio'), for model in app.models.values():
('dat_fim', 'data_fim'), migrate(model)
('dat_eleicao', 'data_eleicao'), ]
),
(SessaoLegislativa, 'cod_sessao_leg', [ def migrate(model):
('num_legislatura', 'legislatura'),
('num_sessao_leg', 'numero'),
('tip_sessao_leg', 'tipo'),
('dat_inicio', 'data_inicio'),
('dat_fim', 'data_fim'),
('dat_inicio_intervalo', 'data_inicio_intervalo'),
('dat_fim_intervalo', 'data_fim_intervalo'), ]
),
(NivelInstrucao, 'cod_nivel_instrucao', [
('des_nivel_instrucao', 'nivel_instrucao')]
),
)
def migrate():
for model, pk, field_pairs in mappings:
print 'Migrating %s...' % model.__name__ print 'Migrating %s...' % model.__name__
@ -38,23 +21,28 @@ def migrate():
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(sql_reset_seq) cursor.execute(sql_reset_seq)
legacy_model = getattr(legacy.models, model.__name__) legacy_model = legacy_app.get_model(model.__name__)
for old in legacy_model.objects.all().order_by(pk): old_pk_name = legacy_model._meta.pk.name
old_id = getattr(old, pk) if old_pk_name == 'id':
# There is no pk in the legacy table
pass
else:
for old in legacy_model.objects.all().order_by(old_pk_name):
old_pk = getattr(old, old_pk_name)
new = model() new = model()
while not new.id: while not new.id:
for old_field, new_field in field_pairs: for new_field, old_field in field_mappings[model].items():
value = getattr(old, old_field) value = getattr(old, old_field)
# check for a relation # check for a relation
model_field = model._meta.get_field(new_field) model_field = model._meta.get_field(new_field)
# TODO ... test for special transformations first (e.g. Parlamentar.localidade_residencia) # TODO ... test for special transformations first (e.g. Parlamentar.localidade_residencia)
# elfi ... # elfi ...
if isinstance(model_field, models.ForeignKey): if isinstance(model_field, models.ForeignKey):
value = model_field.related_model.objects.get(id=value) value = model_field.related_model.objects.get(id=value)
assert value
setattr(new, new_field, value) setattr(new, new_field, value)
new.save() new.save()
assert new.id <= old_id, 'New id exceeds old one. Be sure your new table was just created!' assert new.id <= old_pk, 'New id exceeds old pk!'
if new.id < old_id: if new.id < old_pk:
new.delete() new.delete()
new = model() new = model()

3
legacy/scripts/migration_base.py

@ -1,12 +1,13 @@
from django.apps import apps from django.apps import apps
# this order is important for the migration
appconfs = [apps.get_app_config(n) for n in [ appconfs = [apps.get_app_config(n) for n in [
'parlamentares', 'parlamentares',
'comissoes', 'comissoes',
'sessao',
'materia', 'materia',
'norma', 'norma',
'sessao',
'lexml', 'lexml',
'protocoloadm', ]] 'protocoloadm', ]]
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]

Loading…
Cancel
Save