From a6dc54f84cf78f47d82a083991955f13c928b8c9 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Tue, 15 Aug 2017 15:27:00 -0300 Subject: [PATCH] =?UTF-8?q?Reformata=20arquivo=20de=20sqls=20pr=C3=A9-migr?= =?UTF-8?q?a=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/legacy/migration.py | 10 ++- sapl/legacy/scripts/fix_tables.sql | 109 +++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 13 deletions(-) diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py index 47c2c934b..ec6188ebd 100644 --- a/sapl/legacy/migration.py +++ b/sapl/legacy/migration.py @@ -143,12 +143,10 @@ def get_field(model, fieldname): def exec_sql_file(path, db='default'): - cursor = connections[db].cursor() - for line in open(path): - try: - cursor.execute(line) - except (OperationalError, ProgrammingError) as e: - print("Args: '%s'" % (str(e.args))) + with open(path) as arq: + sql = arq.read() + with connections[db].cursor() as cursor: + cursor.execute(sql) def exec_sql(sql, db='default'): diff --git a/sapl/legacy/scripts/fix_tables.sql b/sapl/legacy/scripts/fix_tables.sql index 1f8cdb63b..3d239e1ae 100644 --- a/sapl/legacy/scripts/fix_tables.sql +++ b/sapl/legacy/scripts/fix_tables.sql @@ -1,5 +1,6 @@ -- Apaga as restrições somente para essa sessão -SELECT REPLACE(@@sql_mode,'STRICT_TRANS_TABLES,','ALLOW_INVALID_DATES'); +SELECT replace(@@sql_mode,'STRICT_TRANS_TABLES,','ALLOW_INVALID_DATES'); + -- Exclui procedures caso já existam DROP PROCEDURE IF EXISTS verifica_campos_proposicao; DROP PROCEDURE IF EXISTS verifica_campos_tipo_materia_legislativa; @@ -7,18 +8,111 @@ DROP PROCEDURE IF EXISTS verifica_campos_sessao_plenaria_presenca; DROP PROCEDURE IF EXISTS cria_lexml_registro_provedor_e_publicador; DROP PROCEDURE IF EXISTS cria_tipo_situacao_militar; DROP PROCEDURE IF EXISTS muda_vinculo_norma_juridica_ind_excluido; + -- Procedure para criar campo num_proposicao em proposicao -CREATE PROCEDURE verifica_campos_proposicao() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='proposicao' AND column_name='num_proposicao') THEN UPDATE proposicao SET dat_envio = '1800-01-01' WHERE CAST(dat_envio AS CHAR(20)) = '0000-00-00 00:00:00'; ALTER TABLE proposicao ADD COLUMN num_proposicao INT(11) NULL after txt_justif_devolucao; END IF; END; +CREATE PROCEDURE verifica_campos_proposicao() BEGIN IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='proposicao' + AND COLUMN_NAME='num_proposicao') THEN +UPDATE proposicao +SET dat_envio = '1800-01-01' +WHERE cast(dat_envio AS char(20)) = '0000-00-00 00:00:00'; + ALTER TABLE proposicao ADD COLUMN num_proposicao int(11) NULL AFTER txt_justif_devolucao; +END IF; END; + -- Procedure para criar campo iind_num_automatica em tipo_materia_legislativa -CREATE PROCEDURE verifica_campos_tipo_materia_legislativa() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='tipo_materia_legislativa' AND column_name='ind_num_automatica') THEN ALTER TABLE tipo_materia_legislativa ADD COLUMN ind_num_automatica BOOLEAN NULL DEFAULT FALSE after des_tipo_materia; END IF; IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='tipo_materia_legislativa' AND column_name='quorum_minimo_votacao') THEN ALTER TABLE tipo_materia_legislativa ADD COLUMN quorum_minimo_votacao INT(11) NULL after ind_num_automatica; END IF; END; +CREATE PROCEDURE verifica_campos_tipo_materia_legislativa() +BEGIN IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='tipo_materia_legislativa' + AND COLUMN_NAME='ind_num_automatica') THEN +ALTER TABLE tipo_materia_legislativa ADD COLUMN ind_num_automatica BOOLEAN NULL DEFAULT FALSE AFTER des_tipo_materia; +END IF; +IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='tipo_materia_legislativa' + AND COLUMN_NAME='quorum_minimo_votacao') THEN +ALTER TABLE tipo_materia_legislativa ADD COLUMN quorum_minimo_votacao int(11) NULL AFTER ind_num_automatica; +END IF; END; + -- Procedure para criar campos cod_presenca_sessao (sendo a nova PK da tabela) e dat_sessao em sessao_plenaria_presenca -CREATE PROCEDURE verifica_campos_sessao_plenaria_presenca() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='sessao_plenaria_presenca' AND column_name='cod_presenca_sessao') THEN ALTER TABLE sessao_plenaria_presenca DROP PRIMARY KEY, ADD cod_presenca_sessao INT AUTO_INCREMENT PRIMARY KEY FIRST; END IF; IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='sessao_plenaria_presenca' AND column_name='dat_sessao') THEN ALTER TABLE sessao_plenaria_presenca ADD COLUMN dat_sessao DATE NULL after cod_parlamentar; END IF; END; +CREATE PROCEDURE verifica_campos_sessao_plenaria_presenca() BEGIN IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='sessao_plenaria_presenca' + AND COLUMN_NAME='cod_presenca_sessao') THEN +ALTER TABLE sessao_plenaria_presenca +DROP PRIMARY KEY, + ADD cod_presenca_sessao INT auto_increment PRIMARY KEY FIRST; +END IF; +IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='sessao_plenaria_presenca' + AND COLUMN_NAME='dat_sessao') THEN +ALTER TABLE sessao_plenaria_presenca ADD COLUMN dat_sessao DATE NULL AFTER cod_parlamentar; +END IF; END; + + -- Procedure para criar tabela lexml_registro_provedor e lexml_registro_publicador -CREATE PROCEDURE cria_lexml_registro_provedor_e_publicador() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='lexml_registro_publicador') THEN CREATE TABLE lexml_registro_publicador (cod_publicador INT AUTO_INCREMENT NOT NULL, id_publicador INT, nom_publicador VARCHAR(255), adm_email VARCHAR(50), sigla VARCHAR(255), nom_responsavel VARCHAR(255), tipo VARCHAR(50), id_responsavel INT, PRIMARY KEY (cod_publicador)); END IF; IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='lexml_registro_provedor') THEN CREATE TABLE lexml_registro_provedor (cod_provedor INT AUTO_INCREMENT NOT NULL, id_provedor INT, nom_provedor VARCHAR(255), sgl_provedor VARCHAR(15), adm_email VARCHAR(50), nom_responsavel VARCHAR(255), tipo VARCHAR(50), id_responsavel INT, xml_provedor LONGTEXT, PRIMARY KEY (cod_provedor)); END IF; END; +CREATE PROCEDURE cria_lexml_registro_provedor_e_publicador() +BEGIN IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='lexml_registro_publicador') THEN +CREATE TABLE lexml_registro_publicador ( + cod_publicador INT auto_increment NOT NULL, + id_publicador INT, nom_publicador varchar(255), + adm_email varchar(50), + sigla varchar(255), + nom_responsavel varchar(255), + tipo varchar(50), + id_responsavel INT, PRIMARY KEY (cod_publicador)); +END IF; +IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='lexml_registro_provedor') THEN +CREATE TABLE lexml_registro_provedor ( + cod_provedor INT auto_increment NOT NULL, + id_provedor INT, nom_provedor varchar(255), + sgl_provedor varchar(15), + adm_email varchar(50), + nom_responsavel varchar(255), + tipo varchar(50), + id_responsavel INT, xml_provedor longtext, + PRIMARY KEY (cod_provedor)); +END IF; END; + -- Procedure para criar tabela tipo_situacao_militar -CREATE PROCEDURE cria_tipo_situacao_militar() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='tipo_situacao_militar') THEN CREATE TABLE tipo_situacao_militar (tip_situacao_militar INT AUTO_INCREMENT NOT NULL, des_tipo_situacao VARCHAR(50), ind_excluido INT, PRIMARY KEY (tip_situacao_militar)); END IF; END; +CREATE PROCEDURE cria_tipo_situacao_militar() BEGIN IF NOT EXISTS + (SELECT * + FROM information_schema.columns + WHERE table_schema=database() + AND TABLE_NAME='tipo_situacao_militar') THEN +CREATE TABLE tipo_situacao_militar ( + tip_situacao_militar INT auto_increment NOT NULL, + des_tipo_situacao varchar(50), + ind_excluido INT, PRIMARY KEY (tip_situacao_militar)); +END IF; END; + -- Procedure para mudar valor do campo ind_excluido da tabela vinculo_norma_juridica de 0 para string vazia '' -CREATE PROCEDURE muda_vinculo_norma_juridica_ind_excluido() BEGIN UPDATE vinculo_norma_juridica SET ind_excluido = '' WHERE trim(ind_excluido) = '0'; END; +CREATE PROCEDURE muda_vinculo_norma_juridica_ind_excluido() BEGIN +UPDATE vinculo_norma_juridica +SET ind_excluido = '' +WHERE trim(ind_excluido) = '0'; +END; + -- Executa as procedures criadas acima CALL verifica_campos_proposicao; CALL verifica_campos_tipo_materia_legislativa; @@ -26,3 +120,4 @@ CALL verifica_campos_sessao_plenaria_presenca; CALL cria_lexml_registro_provedor_e_publicador; CALL cria_tipo_situacao_militar; CALL muda_vinculo_norma_juridica_ind_excluido; +