From 1488df9cf3bfea6a59eee5838596c00a062c8ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Sconetto?= Date: Thu, 9 Nov 2017 15:47:33 -0200 Subject: [PATCH] Incorpora o migra_autor para o migration --- sapl/legacy/migra_autor.py | 54 ---------------------------------- sapl/legacy/migration.py | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 54 deletions(-) delete mode 100644 sapl/legacy/migra_autor.py diff --git a/sapl/legacy/migra_autor.py b/sapl/legacy/migra_autor.py deleted file mode 100644 index d806f510e..000000000 --- a/sapl/legacy/migra_autor.py +++ /dev/null @@ -1,54 +0,0 @@ -import mysql.connector # dep: mysql-connector-python-rf - -def migra_autor(db, passwd): - connection = mysql.connector.connect(user='root', database=db, passwd=passwd) - cursor = connection.cursor(buffered=True) - query = ("select cod_parlamentar, COUNT(*) \ - from {}.autor where col_username is not null \ - group by col_username, cod_parlamentar \ - having 1 < COUNT(*) \ - order by cod_parlamentar asc;").format(db) - - cursor.execute(query) - - all_authors = [] - for response in cursor: - if response[0] is not None: - all_authors.append(response) - - - for author in all_authors: - query2 = ("select * from {}.autor \ - where cod_parlamentar = {} \ - group by cod_autor;").format(db, str(author[0])) - cursor.execute(query2) - user = [] - - for response in cursor: - user.append(response) - - ativ = [] - inativ = [] - for tupl in user: - # tupl[8] = ind_excluido - if tupl[8] == 1: - inativ.append(tupl) - elif tupl[8] == 0: - ativ.append(tupl) - - - tables = ['autoria', 'documento_administrativo', 'proposicao', 'protocolo'] - for table in tables: - # Para update e delete no MySQL -> SET SQL_SAFE_UPDATES = 0; - query3 = ("update {}.{} set cod_autor = {} where cod_autor in ").format(db, table, ativ[0][0]) - inativIds = [u[0] for u in inativ] - inativIds = (str(inativIds)).replace(']', ')').replace('[', '(') - query3 += inativIds + ';' - cursor.execute(query3) - - - query4 = ("delete from sapl_cm_ere_cpy.autor \ - where cod_autor in ") - query4 += inativIds + ';' - cursor.execute(query4) - diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py index f61f564ff..e72f50e45 100644 --- a/sapl/legacy/migration.py +++ b/sapl/legacy/migration.py @@ -181,6 +181,65 @@ def garante_tabela_no_legado(create_table): assert existe_tabela_no_legado(tabela) +def migra_autor(): + SQL_ENUMERA_REPETIDOS = ''' + select cod_parlamentar, COUNT(*) + from autor where col_username is not null + group by col_username, cod_parlamentar + having 1 < COUNT(*) + order by cod_parlamentar asc; + ''' + + SQL_INFOS_AUTOR = ''' + select * from autor + where cod_parlamentar = {} + group by cod_autor; + ''' + + SQL_UPDATE_TABLES_AUTOR = "update {} set cod_autor = {} where cod_autor in ({});" + + SQL_DELETE_AUTOR_INATIVO = "delete from autor where cod_autor in ({});" + + cursor = exec_legado(SQL_ENUMERA_REPETIDOS) + + all_authors = [] + + for response in cursor: + if response[0] is not None: + all_authors.append(response) + + for author in all_authors: + sql = SQL_INFOS_AUTOR.format(str(author[0])) + cursor = exec_legado(sql) + + user = [] + + for response in cursor: + user.append(response) + + ativ = [] + inativ = [] + for tupl in user: + # tupl[8] = ind_excluido + if tupl[8] == 1: + inativ.append(tupl) + elif tupl[8] == 0:\ + ativ.append(tupl) + + tables = ['autoria', 'documento_administrativo', 'proposicao', 'protocolo'] + for table in tables: + # Para update e delete no MySQL -> SET SQL_SAFE_UPDATES = 0; + ativId = ativ[0][0] + inativIds = [u[0] for u in inativ] + inativIds = (str(inativIds)).replace(']', '').replace('[', '') + sql = SQL_UPDATE_TABLES_AUTOR.format(table, ativId, inativIds) + exec_legado(sql) + + + sql = SQL_DELETE_AUTOR_INATIVO.format(inativIds) + exec_legado(sql) + + def uniformiza_banco(): exec_legado(''' SELECT replace(@@sql_mode,"STRICT_TRANS_TABLES,","ALLOW_INVALID_DATES");