Browse Source

Corrige alguns problemas

pull/1596/head
João Pedro Sconetto 7 years ago
committed by Marcio Mazza
parent
commit
911c3e10f2
  1. 90
      sapl/legacy/migra_autor.py

90
sapl/legacy/migra_autor.py

@ -1,46 +1,52 @@
import mysql.connector # dep: mysql-connector-python-rf import mysql.connector # dep: mysql-connector-python-rf
def migra_autor(db, passwd): def migra_autor(db, passwd):
connection = mysql.connector.connect(user='root', database=db, passwd=passwd) connection = mysql.connector.connect(user='root', database=db, passwd=passwd)
cursor = connection.cursor(buffered=True) cursor = connection.cursor(buffered=True)
query = ("select cod_parlamentar, COUNT(*) \ query = ("select cod_parlamentar, COUNT(*) \
from {}.autor where col_username is not null \ from {}.autor where col_username is not null \
group by col_username, cod_parlamentar \ group by col_username, cod_parlamentar \
having 1 < COUNT(*) \ having 1 < COUNT(*) \
order by cod_parlamentar asc;").format(db) order by cod_parlamentar asc;").format(db)
cursor.execute(query) cursor.execute(query)
all_authors = [] 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 = " + str(author[0]) + " \
group by cod_autor;").format(db)
cursor.execute(query2)
user = []
for response in cursor: for response in cursor:
user.append(response) if response[0] is not None:
all_authors.append(response)
ativ = []
inativ = []
for tupl in user: for author in all_authors:
if tupl[8] == 1: query2 = ("select * from {}.autor \
inativ.append(tupl) where cod_parlamentar = " + str(author[0]) + " \
elif tupl[8] == 0: group by cod_autor;").format(db)
ativ.append(tupl) cursor.execute(query2)
user = []
tables = ['autoria', 'documento_administrativo', 'proposicao', 'protocolo'] for response in cursor:
for table in tables: user.append(response)
query3 = ("UPDATE {}.{} SET cod_autor = {} WHERE cod_autor in ").format(db, table, ativ[0][0])
inativIds = [u[0] for u in inativ] ativ = []
inativIds = (str(inativIds)).replace(']', ')').replace('[', '(') inativ = []
query3 += inativIds for tupl in user:
if tupl[8] == 1:
cursor.execute(query3) 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 + ';'

Loading…
Cancel
Save