From 91a98e4fffdbed8386259490955e1d866449ffd5 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Mon, 28 Mar 2016 16:51:23 -0300 Subject: [PATCH 1/4] =?UTF-8?q?Ajusta=20migra=C3=A7=C3=A3o=20para=20novas?= =?UTF-8?q?=20mudan=C3=A7as=20das=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- legacy/migration.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index d98b17b19..34c6d2a34 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -93,7 +93,6 @@ def warn(msg): def get_fk_related(field, value, label=None): fields_dict = {} - if value is None and field.null is False: value = 0 if value is not None: @@ -169,7 +168,6 @@ def make_stub(model, id): class DataMigrator: - def __init__(self): self.field_renames, self.model_renames = get_renames() @@ -179,6 +177,8 @@ class DataMigrator: for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() + msg = ("Field %s (%s) from model %s " % + (field.name, field_type, field.model.__name__)) if old_field_name: old_value = getattr(old, old_field_name) if isinstance(field, models.ForeignKey): @@ -191,13 +191,23 @@ class DataMigrator: value = get_fk_related(field, old_value, label) else: value = getattr(old, old_field_name) + if (field_type == 'DateField' and + field.null is False and value is None): + names = [old_fields.name for old_fields + in old._meta.get_fields()] + combined_names = "(" + ")|(".join(names) + ")" + matches = re.search('(ano_\w+)', combined_names) + if not matches: + warn(msg + '=> setting 0000-01-01 value to DateField') + value = '0001-01-01' + else: + value = '%d-01-01' % getattr(old, matches.group(0)) + warn(msg + "=> settig %s for not null DateField" % + (value)) if field_type == 'CharField' or field_type == 'TextField': if value is None: - warn( - "Field %s (%s) from model %s" - " => settig empty string '' for %s value" % - (field.name, field_type, field.model.__name__, - value)) + warn(msg + "=> settig empty string '' for %s value" % + (value)) value = '' setattr(new, field.name, value) From dd3f37940b38ab3716258f4f07e06969ea238411 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Tue, 5 Apr 2016 10:30:39 -0300 Subject: [PATCH 2/4] Arruma conflito com master e traduz textos de info Signed-off-by: Luciano Almeida --- legacy/migration.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index 34c6d2a34..685d9dcba 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -104,7 +104,7 @@ def get_fk_related(field, value, label=None): field.name, value, field.model.__name__, label or '---') if value == 0: - # se FK == 0, criamos um stub e colocamos o valor '???????? + # 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() @@ -177,7 +177,7 @@ class DataMigrator: for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() - msg = ("Field %s (%s) from model %s " % + msg = ("Campo %s (%s) da model %s " % (field.name, field_type, field.model.__name__)) if old_field_name: old_value = getattr(old, old_field_name) @@ -187,7 +187,7 @@ class DataMigrator: old_type._meta.pk.name != 'id': label = old.pk else: - label = '-- WITHOUT PK --' + label = '-- SEM PK --' value = get_fk_related(field, old_value, label) else: value = getattr(old, old_field_name) @@ -198,15 +198,17 @@ class DataMigrator: combined_names = "(" + ")|(".join(names) + ")" matches = re.search('(ano_\w+)', combined_names) if not matches: - warn(msg + '=> setting 0000-01-01 value to DateField') + warn(msg + + '=> colocando valor 0000-01-01 para DateField') value = '0001-01-01' else: value = '%d-01-01' % getattr(old, matches.group(0)) - warn(msg + "=> settig %s for not null DateField" % + warn(msg + + "=> colocando %s para DateField não nulável" % (value)) if field_type == 'CharField' or field_type == 'TextField': if value is None: - warn(msg + "=> settig empty string '' for %s value" % + warn(msg + "=> colocando string vazia para valor %s" % (value)) value = '' setattr(new, field.name, value) @@ -215,13 +217,13 @@ class DataMigrator: # warning: model/app migration order is of utmost importance self.to_delete = [] - info('Starting %s migration...' % obj) + info('Começando migração: %s...' % obj) self._do_migrate(obj) # exclude logically deleted in legacy base - info('Deleting models with ind_excluido...') + info('Deletando models com ind_excluido...') for obj in self.to_delete: obj.delete() - info('Deleting unnecessary stubs...') + info('Deletando stubs desnecessários...') self.delete_stubs() def _do_migrate(self, obj): @@ -239,7 +241,7 @@ class DataMigrator: 'Parameter must be a Model, AppConfig or a sequence of them') def migrate_model(self, model): - print('Migrating %s...' % model.__name__) + print('Migrando %s...' % model.__name__) legacy_model_name = self.model_renames.get(model, model.__name__) legacy_model = legacy_app.get_model(legacy_model_name) @@ -314,7 +316,7 @@ def adjust_parlamentar(new_parlamentar, old): # but data includes null values # => transform None to False if value is None: - warn('null converted to False') + warn('nulo convertido para falso') new_parlamentar.unidade_deliberativa = False From a340d18ca47ed0fea681c9d835f710beb6dd2032 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 31 Mar 2016 11:40:00 -0300 Subject: [PATCH 3/4] Concerta problema de duplicidade Signed-off-by: Luciano Almeida --- legacy/migration.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/legacy/migration.py b/legacy/migration.py index 685d9dcba..0bd1f008a 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -28,6 +28,8 @@ appconfs = [apps.get_app_config(n) for n in [ stubs_list = [] +unique_constraints = [] + name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] # apps do not overlap @@ -148,6 +150,39 @@ def iter_sql_records(sql, db): yield record +def delete_constraints(model): + global unique_constraints + # pega nome da unique constraint dado o nome da tabela + table = model._meta.db_table + cursor = exec_sql("SELECT conname FROM pg_constraint WHERE conrelid = " + "(SELECT oid FROM pg_class WHERE relname LIKE " + "'%s') and contype = 'u';" % (table)) + result = cursor.fetchone() + # if theres a result then delete + if result: + args = model._meta.unique_together[0] + args_list = list(args) + + unique_constraints.append([table, result[0], args_list, model]) + exec_sql("ALTER TABLE %s DROP CONSTRAINT %s;" % + (table, result[0])) + + +def recreate_constraints(): + global unique_constraints + if unique_constraints: + for constraint in unique_constraints: + table, name, args, model = constraint + for i in range(len(args)): + if isinstance(model._meta.get_field(args[i]), + models.ForeignKey): + args[i] = args[i]+'_id' + args_string = '' + args_string += "(" + ', '.join(map(str, args)) + ")" + exec_sql("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE %s;" % + (table, name, args_string)) + + def save_with_id(new, id): sequence_name = '%s_id_seq' % type(new)._meta.db_table cursor = exec_sql('SELECT last_value from %s;' % sequence_name) @@ -225,6 +260,8 @@ class DataMigrator: obj.delete() info('Deletando stubs desnecessários...') self.delete_stubs() + info('Recreating unique constraints...') + recreate_constraints() def _do_migrate(self, obj): if isinstance(obj, AppConfig): @@ -250,6 +287,7 @@ class DataMigrator: # Clear all model entries # They may have been created in a previous migration attempt model.objects.all().delete() + delete_constraints(model) # setup migration strategy for tables with or without a pk if legacy_pk_name == 'id': From 9a68b8e7ed20b2be26524283b65426fa39ea0a85 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Tue, 5 Apr 2016 13:59:10 -0300 Subject: [PATCH 4/4] Remove global e corrige alguns textos Signed-off-by: Luciano Almeida --- legacy/migration.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index 0bd1f008a..f1291ca16 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -27,7 +27,6 @@ appconfs = [apps.get_app_config(n) for n in [ 'protocoloadm', ]] stubs_list = [] - unique_constraints = [] name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] @@ -151,25 +150,23 @@ def iter_sql_records(sql, db): def delete_constraints(model): - global unique_constraints # pega nome da unique constraint dado o nome da tabela table = model._meta.db_table cursor = exec_sql("SELECT conname FROM pg_constraint WHERE conrelid = " "(SELECT oid FROM pg_class WHERE relname LIKE " "'%s') and contype = 'u';" % (table)) result = cursor.fetchone() - # if theres a result then delete + # se existir um resultado, unique constraint será deletado if result: + warn('Excluindo unique constraint de nome %s' % result) args = model._meta.unique_together[0] args_list = list(args) - unique_constraints.append([table, result[0], args_list, model]) exec_sql("ALTER TABLE %s DROP CONSTRAINT %s;" % (table, result[0])) def recreate_constraints(): - global unique_constraints if unique_constraints: for constraint in unique_constraints: table, name, args, model = constraint @@ -260,7 +257,7 @@ class DataMigrator: obj.delete() info('Deletando stubs desnecessários...') self.delete_stubs() - info('Recreating unique constraints...') + info('Recriando unique constraints...') recreate_constraints() def _do_migrate(self, obj):