Browse Source

Add a special migration for comissoes app

pull/6/head
Marcio Mazza 10 years ago
parent
commit
68a4f917da
  1. 20
      comissoes/models.py
  2. 13
      legacy/scripts/field_renames.py
  3. 48
      legacy/scripts/migration.py

20
comissoes/models.py

@ -53,7 +53,7 @@ class Comissao(models.Model):
return self.nome return self.nome
class PeriodoCompComissao(models.Model): class Periodo(models.Model): # PeriodoCompComissao
data_inicio = models.DateField(verbose_name=_(u'Data Início')) # dat_inicio_periodo data_inicio = models.DateField(verbose_name=_(u'Data Início')) # dat_inicio_periodo
data_fim = models.DateField(blank=True, null=True, verbose_name=_(u'Data Fim')) # dat_fim_periodo data_fim = models.DateField(blank=True, null=True, verbose_name=_(u'Data Fim')) # dat_fim_periodo
@ -77,10 +77,18 @@ class CargoComissao(models.Model):
return self.nome return self.nome
class ComposicaoComissao(models.Model): class Composicao(models.Model): # IGNORE
comissao = models.ForeignKey(Comissao, verbose_name=_(u'Comissão')) # cod_comissao
periodo = models.ForeignKey(Periodo, verbose_name=_(u'Período')) # cod_periodo_comp
class Meta:
verbose_name = _(u'Composição de Comissão')
verbose_name_plural = _(u'Composições de Comissão')
class Participacao(models.Model): # ComposicaoComissao
composicao = models.ForeignKey(Composicao) # cod_comissao
parlamentar = models.ForeignKey(Parlamentar) # cod_parlamentar parlamentar = models.ForeignKey(Parlamentar) # cod_parlamentar
comissao = models.ForeignKey(Comissao) # cod_comissao
periodo_comp = models.ForeignKey(PeriodoCompComissao) # cod_periodo_comp
cargo = models.ForeignKey(CargoComissao) # cod_cargo cargo = models.ForeignKey(CargoComissao) # cod_cargo
titular = models.BooleanField(verbose_name=_(u'Titular')) # ind_titular titular = models.BooleanField(verbose_name=_(u'Titular')) # ind_titular
data_designacao = models.DateField(verbose_name=_(u'Data Designação')) # dat_designacao data_designacao = models.DateField(verbose_name=_(u'Data Designação')) # dat_designacao
@ -89,8 +97,8 @@ class ComposicaoComissao(models.Model):
observacao = models.CharField(max_length=150, blank=True, null=True, verbose_name=_(u'Observação')) # obs_composicao observacao = models.CharField(max_length=150, blank=True, null=True, verbose_name=_(u'Observação')) # obs_composicao
class Meta: class Meta:
verbose_name = _(u'Composição de Comissão') verbose_name = _(u'Participação em Comissão')
verbose_name_plural = _(u'Composições de Comissão') verbose_name_plural = _(u'Participações em Comissão')
def __unicode__(self): def __unicode__(self):
return 'TODO...' return 'TODO...'

13
legacy/scripts/field_renames.py

@ -1,7 +1,7 @@
from collections import defaultdict import re
from collections import defaultdict, OrderedDict
from difflib import SequenceMatcher from difflib import SequenceMatcher
from inspect import getsourcelines from inspect import getsourcelines
from collections import OrderedDict
from migration_base import appconfs, legacy_app from migration_base import appconfs, legacy_app
@ -34,10 +34,19 @@ def print_commented_source(model):
field_renames = OrderedDict() field_renames = OrderedDict()
model_renames = {}
for app in appconfs: for app in appconfs:
for model in app.models.values(): for model in app.models.values():
new_to_old = OrderedDict() new_to_old = OrderedDict()
lines = getsourcelines(model)[0] lines = getsourcelines(model)[0]
class_line = lines[0].strip()
match = re.match('class (.+)\(models\.Model\): *\# *(.*)', class_line)
if match:
model_name, rename = match.groups()
assert model_name == model.__name__
model_renames[model] = rename
for line in lines: for line in lines:
if is_field_line(line): if is_field_line(line):
new = get_field(line) new = get_field(line)

48
legacy/scripts/migration.py

@ -4,9 +4,11 @@ from django.db import connections, models
from django.db.models.base import ModelBase from django.db.models.base import ModelBase
from model_mommy import mommy from model_mommy import mommy
from field_renames import field_renames from field_renames import field_renames, model_renames
from migration_base import legacy_app, appconfs from migration_base import legacy_app, appconfs
from parlamentares.models import Parlamentar from parlamentares.models import Parlamentar
from comissoes.models import Composicao, Participacao
def info(msg): def info(msg):
@ -27,7 +29,7 @@ def special(model, fieldname):
@special(Parlamentar, 'unid_deliberativa') @special(Parlamentar, 'unid_deliberativa')
def none_to_false(value): def none_to_false(obj, value):
# Field is defined as not null in legacy db, but data includes null values # Field is defined as not null in legacy db, but data includes null values
# => transform None to False # => transform None to False
if value is None: if value is None:
@ -35,6 +37,24 @@ def none_to_false(value):
return bool(value) return bool(value)
@special(Participacao, 'composicao')
def get_participacao_composicao(obj, value):
# value parameter is ignored
new = Composicao()
for new_field, value in [('comissao', obj.cod_comissao),
('periodo', obj.cod_periodo_comp)]:
model_field = Composicao._meta.get_field(new_field)
value = get_related_if_foreignkey(model_field, '???', value)
setattr(new, new_field, value)
previous = Composicao.objects.filter(comissao=new.comissao, periodo=new.periodo)
if previous:
assert len(previous) == 1
return previous[0]
else:
new.save()
return new
def migrate(obj=appconfs, count_limit=None): def migrate(obj=appconfs, count_limit=None):
# warning: model/app migration order is of utmost importance # warning: model/app migration order is of utmost importance
@ -96,12 +116,17 @@ def make_stub(model, id):
def migrate_model(model, to_delete, count_limit=None): def migrate_model(model, to_delete, count_limit=None):
legacy_model_name = model_renames.get(model, model.__name__)
if legacy_model_name.upper() == 'IGNORE':
print 'Model ignored: %s' % model.__name__
return
print 'Migrating %s...' % model.__name__ print 'Migrating %s...' % model.__name__
# clear all model entries # clear all model entries
model.objects.all().delete() model.objects.all().delete()
legacy_model = legacy_app.get_model(model.__name__) legacy_model = legacy_app.get_model(legacy_model_name)
old_pk_name = legacy_model._meta.pk.name old_pk_name = legacy_model._meta.pk.name
# setup migration strategy for tables with or without a pk # setup migration strategy for tables with or without a pk
@ -131,15 +156,23 @@ def migrate_model(model, to_delete, count_limit=None):
model_field = model._meta.get_field(new_field) model_field = model._meta.get_field(new_field)
transform = special_transforms.get(model_field) transform = special_transforms.get(model_field)
if transform: if transform:
value = transform(value) value = transform(old, value)
else: else:
# check for a relation # check for a relation
value = get_related_if_foreignkey(model_field, old_pk, value)
setattr(new, new_field, value)
save(new, old_pk)
if getattr(old, 'ind_excluido', False):
to_delete.append(new)
def get_related_if_foreignkey(model_field, old_pk, value):
if isinstance(model_field, models.ForeignKey) and value is not None: if isinstance(model_field, models.ForeignKey) and value is not None:
try: try:
value = model_field.related_model.objects.get(id=value) value = model_field.related_model.objects.get(id=value)
except ObjectDoesNotExist: except ObjectDoesNotExist:
msg = 'FK [%s (%s) : %s] not found for value %s' % ( msg = 'FK [%s (%s) : %s] not found for value %s' % (
model.__name__, old_pk, model_field.name, value) model_field.model.__name__, old_pk, model_field.name, value)
if value == 0: if value == 0:
# we interpret FK == 0 as actually FK == NONE # we interpret FK == 0 as actually FK == NONE
value = None value = None
@ -149,10 +182,7 @@ def migrate_model(model, to_delete, count_limit=None):
warn(msg + ' => STUB CREATED') warn(msg + ' => STUB CREATED')
else: else:
assert value assert value
setattr(new, new_field, value) return value
save(new, old_pk)
if getattr(old, 'ind_excluido', False):
to_delete.append(new)
def get_ind_excluido(obj): def get_ind_excluido(obj):

Loading…
Cancel
Save