From 77f0f8c7bf23d2c9b51b92605ea71f2bad4bf2d3 Mon Sep 17 00:00:00 2001 From: Luciano Henrique Nunes de Almeida Date: Fri, 15 Sep 2017 14:34:42 -0300 Subject: [PATCH] =?UTF-8?q?Arruma=20scripts=20de=20migra=C3=A7=C3=A3o=20(#?= =?UTF-8?q?1479)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Retira senhas hardcoded Signed-off-by: Luciano Almeida * Arruma ajuste de proposição Signed-off-by: Luciano Almeida --- sapl/legacy/migration.py | 18 +++++++--- sapl/legacy/scripts/migra_dbs.sh | 8 ++++- sapl/legacy/scripts/migra_um_db.sh | 37 +++++++++++++------- sapl/legacy/scripts/recria_dbs_postgres.sh | 8 ++++- sapl/legacy/scripts/recria_um_db_postgres.sh | 1 + 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py index 2d586c326..bc7483ea9 100644 --- a/sapl/legacy/migration.py +++ b/sapl/legacy/migration.py @@ -424,9 +424,12 @@ def adjust_mandato(new, old): if old.dat_fim_mandato: new.data_fim_mandato = old.dat_fim_mandato if not new.data_fim_mandato: - new.data_fim_mandato = new.legislatura.data_fim + legislatura = Legislatura.objects.latest('data_fim') + new.data_fim_mandato = legislatura.data_fim + new.data_expedicao_diploma = legislatura.data_inicio if not new.data_inicio_mandato: new.data_inicio_mandato = new.legislatura.data_inicio + new.data_fim_mandato = new.legislatura.data_fim def adjust_ordemdia_antes_salvar(new, old): @@ -556,11 +559,18 @@ def adjust_tipoafastamento(new, old): def adjust_tipoproposicao(new, old): if old.ind_mat_ou_doc == 'M': - new.tipo_conteudo_related = TipoMateriaLegislativa.objects.get( + tipo_materia = TipoMateriaLegislativa.objects.filter( pk=old.tip_mat_ou_doc) + if tipo_materia: + new.tipo_conteudo_related = tipo_materia[0] + else: + raise ForeignKeyFaltando elif old.ind_mat_ou_doc == 'D': - new.tipo_conteudo_related = TipoDocumento.objects.get( - pk=old.tip_mat_ou_doc) + tipo_documento = TipoDocumento.objects.filter(pk=old.tip_mat_ou_doc) + if tipo_documento: + new.tipo_conteudo_related = tipo_documento[0] + else: + raise ForeignKeyFaltando def adjust_statustramitacao(new, old): diff --git a/sapl/legacy/scripts/migra_dbs.sh b/sapl/legacy/scripts/migra_dbs.sh index c7529e015..0e52a9483 100755 --- a/sapl/legacy/scripts/migra_dbs.sh +++ b/sapl/legacy/scripts/migra_dbs.sh @@ -2,4 +2,10 @@ # rodar esse script na raiz do projeto -parallel --verbose ./sapl/legacy/scripts/migra_um_db.sh :::: <(mysql -u root -padmin -e 'show databases;' | grep '^sapl_') +if [ $# -eq 2 ]; then + parallel -eta --verbose ./sapl/legacy/scripts/migra_um_db.sh :::: <(mysql -u $1 -p$2 -e 'show databases;' | grep '^sapl_' | grep -v '_copy$') ::: $1 ::: $2 +else + echo "USO:" + echo " ./sapl/legacy/scripts/migra_dbs.sh [usuário mysql] [senha mysql]" +fi; + diff --git a/sapl/legacy/scripts/migra_um_db.sh b/sapl/legacy/scripts/migra_um_db.sh index 18f233414..7e76c60c4 100755 --- a/sapl/legacy/scripts/migra_um_db.sh +++ b/sapl/legacy/scripts/migra_um_db.sh @@ -1,20 +1,33 @@ #!/bin/bash # rodar esse script na raiz do projeto +if [ $# -eq 3 ]; then + DIR=~/logs_migracao + mkdir -p $DIR -DIR=~/logs_migracao -mkdir -p $DIR + LOG="$DIR/$1.migracao.log" + rm -f $LOG -LOG="$DIR/$1.migracao.log" -rm -f $LOG + echo "########################################" | tee -a $LOG + echo "MIGRANDO BANCO $1" | tee -a $LOG + echo "########################################" | tee -a $LOG + echo >> $LOG -echo "########################################" | tee -a $LOG -echo "MIGRANDO BANCO $1" | tee -a $LOG -echo "########################################" | tee -a $LOG -echo >> $LOG + echo "--- CRIANDO BACKUP ---" | tee -a $LOG + echo >> $LOG + mysql -u $2 -p$3 -e "create database if not exists $1_copy;" && mysqldump -u $2 -p$3 $1 | mysql -u $2 -p$3 $1_copy; + echo >> $LOG + echo "--- DJANGO MIGRATE ---" | tee -a $LOG + echo >> $LOG + DATABASE_NAME=$1 ./manage.py migrate --settings sapl.legacy_migration_settings + echo >> $LOG -echo "--- MIGRACAO DE DADOS ---" | tee -a $LOG -echo >> $LOG -DATABASE_NAME=$1 ./manage.py migracao_25_31 -f --settings sapl.legacy_migration_settings |& tee -a $LOG -echo >> $LOG + echo "--- MIGRACAO DE DADOS ---" | tee -a $LOG + echo >> $LOG + DATABASE_NAME=$1 ./manage.py migracao_25_31 -f --settings sapl.legacy_migration_settings |& tee -a $LOG + echo >> $LOG +else + echo "USO:" + echo " ./sapl/legacy/scripts/migra_um_db.sh [nome_database] [usuário mysql] [senha mysql]" +fi; diff --git a/sapl/legacy/scripts/recria_dbs_postgres.sh b/sapl/legacy/scripts/recria_dbs_postgres.sh index 6fbf18794..d84fa4c4d 100755 --- a/sapl/legacy/scripts/recria_dbs_postgres.sh +++ b/sapl/legacy/scripts/recria_dbs_postgres.sh @@ -1,5 +1,11 @@ # (Re)cria todos os bancos postgres para migração # cria um banco postgres (de mesmo nome) para cada banco mysql cujo nome começa com "sapl_" -mysql -u root -padmin -e 'show databases;' | grep '^sapl_' | xargs -I{} ./recria_um_db_postgres.sh {} + +if [ $# -eq 2 ]; then + parallel --verbose ./recria_um_db_postgres.sh :::: <(mysql -u $1 -p$2 -e 'show databases;' | grep '^sapl_' | grep -v '_copy$') +else + echo "USO:" + echo " ./recria_dbs_postgres.sh [usuário mysql] [senha mysql]" +fi; diff --git a/sapl/legacy/scripts/recria_um_db_postgres.sh b/sapl/legacy/scripts/recria_um_db_postgres.sh index fd56a95ee..3ff66e8f3 100755 --- a/sapl/legacy/scripts/recria_um_db_postgres.sh +++ b/sapl/legacy/scripts/recria_um_db_postgres.sh @@ -1,5 +1,6 @@ # (Re)cria um db postgres # uso: recria_um_db_postgres +echo "Database $1" sudo -u postgres psql -c "drop DATABASE if exists $1" sudo -u postgres psql -c "CREATE DATABASE $1 WITH OWNER = sapl ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'pt_BR.UTF-8' LC_CTYPE = 'pt_BR.UTF-8' CONNECTION LIMIT = -1 TEMPLATE template0;"