Repository: interlegis/sapl

Issue:
Migração em paralelo
Adaptar migração de dados para rodar várias casas em paralelo


Discussion:

Resolving patch:
diff --git a/sapl/legacy/management/commands/migracao_25_31.py b/sapl/legacy/management/commands/migracao_25_31.py
--- a/sapl/legacy/management/commands/migracao_25_31.py
+++ b/sapl/legacy/management/commands/migracao_25_31.py
@@ -4,7 +4,17 @@
 
 
 class Command(BaseCommand):
-    help = u'Faz a migração de dados do SAPL 2.5 para o SAPL 3.1'
+
+    help = u'Migração de dados do SAPL 2.5 para o SAPL 3.1'
+
+    def add_arguments(self, parser):
+        parser.add_argument(
+            '-f',
+            action='store_true',
+            default=False,
+            dest='force',
+            help='Não interativa: pula confirmação de exclusão dos dados',
+        )
 
     def handle(self, *args, **options):
-        migration.migrate()
+        migration.migrate(interativo=not options['force'])
diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py
--- a/sapl/legacy/migration.py
+++ b/sapl/legacy/migration.py
@@ -11,11 +11,10 @@
 from django.contrib.contenttypes.models import ContentType
 from django.core.exceptions import ObjectDoesNotExist
 from django.db import OperationalError, ProgrammingError, connections, models
-from django.db.models import CharField, Max, ProtectedError, TextField, signals
+from django.db.models import CharField, Max, ProtectedError, TextField
 from django.db.models.base import ModelBase
 from model_mommy import mommy
 from model_mommy.mommy import foreign_key_required, make
-from reversion.models import Revision, Version
 
 from sapl.base.models import Autor, ProblemaMigracao
 from sapl.comissoes.models import Comissao, Composicao, Participacao
@@ -28,6 +27,7 @@
 from sapl.parlamentares.models import Parlamentar
 from sapl.protocoloadm.models import Protocolo, StatusTramitacaoAdministrativo
 from sapl.sessao.models import ExpedienteMateria, OrdemDia, SessaoPlenaria
+from sapl.settings import PROJECT_DIR
 from sapl.utils import normalize
 
 # BASE ######################################################################
@@ -316,9 +316,9 @@ def fill_vinculo_norma_juridica():
              ('L', 'Suspende integralmente a norma'),
              ('N', 'Julgada integralmente inconstitucional'),
              ('O', 'Julgada parcialmente inconstitucional')]
-    lista_objs = [VinculoNormaJuridica(sigla=item[0], descricao=item[1])
+    lista_objs = [TipoVinculoNormaJuridica(sigla=item[0], descricao=item[1])
                   for item in lista]
-    VinculoNormaJuridica.objects.bulk_create(lista_objs)
+    TipoVinculoNormaJuridica.objects.bulk_create(lista_objs)
 
 
 class DataMigrator:
@@ -386,32 +386,37 @@ def populate_renamed_fields(self, new, old):
                 setattr(new, field.name, value)
             elif field.model.__name__ == 'TipoAutor' and \
                     field.name == 'content_type':
-                try:
-                    value = field.related_model.objects.get(
-                        model=normalize(new.descricao.lower()).replace(' ',
-                                                                       ''))
-                except ObjectDoesNotExist:
-                    value = None
+
+                model = normalize(new.descricao.lower()).replace(' ', '')
+                content_types = field.related_model.objects.filter(
+                    model=model).exclude(app_label='legacy')
+                assert len(content_types) <= 1
+
+                value = content_types[0] if content_types else None
                 setattr(new, field.name, value)
 
-    def migrate(self, obj=appconfs):
+    def migrate(self, obj=appconfs, interativo=True):
         # warning: model/app migration order is of utmost importance
-        exec_sql_file('sapl/legacy/scripts/fix_tables.sql', 'legacy')
+        exec_sql_file(PROJECT_DIR.child(
+            'sapl', 'legacy', 'scripts', 'fix_tables.sql'), 'legacy')
         self.to_delete = []
 
         # excluindo database antigo.
-        info('Todos os dados do banco serão excluidos. '
-             'Recomendamos que faça backup do banco sapl antes de continuar.')
-        info('Deseja continuar? [s/n]')
-        resposta = input()
-        if resposta.lower() in ['s', 'sim', 'y', 'yes']:
-            pass
-        else:
-            info('Migração cancelada.')
-            return 0
+        if interativo:
+            info('Todos os dados do banco serão excluidos. '
+                 'Recomendamos que faça backup do banco sapl '
+                 'antes de continuar.')
+            info('Deseja continuar? [s/n]')
+            resposta = input()
+            if resposta.lower() in ['s', 'sim', 'y', 'yes']:
+                pass
+            else:
+                info('Migração cancelada.')
+                return 0
         info('Excluindo entradas antigas do banco.')
-        call(['./manage.py', 'flush', '--settings=sapl.settings',
-              '--database=default', '--no-input'], stdout=PIPE)
+        call([PROJECT_DIR.child('manage.py'), 'flush',
+              '--settings=sapl.settings', '--database=default', '--no-input'],
+             stdout=PIPE)
 
         info('Começando migração: %s...' % obj)
         self._do_migrate(obj)
@@ -528,9 +533,9 @@ def delete_stubs(self):
         return excluidos
 
 
-def migrate(obj=appconfs):
+def migrate(obj=appconfs, interativo=True):
     dm = DataMigrator()
-    dm.migrate(obj)
+    dm.migrate(obj, interativo)
 
 
 # MIGRATION_ADJUSTMENTS #####################################################
diff --git a/sapl/legacy_migration_settings.py b/sapl/legacy_migration_settings.py
--- a/sapl/legacy_migration_settings.py
+++ b/sapl/legacy_migration_settings.py
@@ -1,12 +1,11 @@
 import os
 
-from decouple import AutoConfig, Config, RepositoryEnv
+from decouple import Config, RepositoryEnv
 from dj_database_url import parse as db_url
 
 from .settings import *  # flake8: noqa
 
-config = AutoConfig()
-config.config = Config(RepositoryEnv(os.path.abspath('sapl/legacy/.env')))
+config = Config(RepositoryEnv(BASE_DIR.child('legacy', '.env')))
 
 
 INSTALLED_APPS += (
@@ -15,6 +14,13 @@
 
 DATABASES['legacy'] = config('DATABASE_URL', cast=db_url,)
 
+# Sobrescreve o nome dos bancos caso a variável de ambiente seja definida
+# Útil para migração em lote de vários bancos
+DATABASE_NAME_OVERRIDE = os.environ.get('DATABASE_NAME')
+if DATABASE_NAME_OVERRIDE:
+    for db in DATABASES.values():
+        db['NAME'] = DATABASE_NAME_OVERRIDE
+
 DATABASE_ROUTERS = ['sapl.legacy.router.LegacyRouter', ]
 
 DEBUG = False


Test patch: