From a2b3b03a399d87e7795d7eed97ec508881fde287 Mon Sep 17 00:00:00 2001 From: nettoluis Date: Tue, 19 May 2026 14:23:36 -0300 Subject: [PATCH 1/5] add: database connection handler --- utils/database.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 utils/database.py diff --git a/utils/database.py b/utils/database.py new file mode 100644 index 0000000..4aeb550 --- /dev/null +++ b/utils/database.py @@ -0,0 +1,24 @@ +import psycopg2 + +try: + conexao = psycopg2.connect( + host="db.oca-portal.com", + database="oca_server", + user="oca-user", + password="oca2026!", + port="5432" + ) + + cursor = conexao.cursor() + cursor.execute("SELECT version();") + versao_db = cursor.fetchone() + print("Conexão bem-sucedida!") + print(f"Versão do banco de dados: {versao_db[0]}") + + cursor.close() + conexao.close() + +except psycopg2.OperationalError as e: + print(f"Ocorreu um erro operacional (provavelmente rede ou credenciais): {e}") +except Exception as e: + print(f"Erro: {e}") From 5d3d7bf796da35fa18eb86eb095710c9b12a34ad Mon Sep 17 00:00:00 2001 From: nettoluis Date: Tue, 19 May 2026 15:18:59 -0300 Subject: [PATCH 2/5] add: .env.example database related new entries --- .env.example | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.env.example b/.env.example index faff2e1..179a4bd 100644 --- a/.env.example +++ b/.env.example @@ -10,3 +10,9 @@ SAUDE_DOCS_URL= ECONOMIA_RENDA_DOCS_URL= SANEAMENTO_DOCS_URL= HIDRAULICA_DOCS_URL= +DB_HOST= +DB_DATABASE= +DB_USER= +DB_PASSWORD= +DB_PORT= + From 588784991f3058459687532708cd4ee51741a5bf Mon Sep 17 00:00:00 2001 From: nettoluis Date: Tue, 19 May 2026 15:20:40 -0300 Subject: [PATCH 3/5] add: new environment variables related to the database integration --- config.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index 82bf05e..6e23319 100644 --- a/config.py +++ b/config.py @@ -34,6 +34,11 @@ def get_config_value(name: str) -> str | None: ECONOMIA_RENDA_DOCS_URL = get_config_value("ECONOMIA_RENDA_DOCS_URL") SANEAMENTO_DOCS_URL = get_config_value("SANEAMENTO_DOCS_URL") HIDRAULICA_DOCS_URL = get_config_value("HIDRAULICA_DOCS_URL") +DB_HOST = get_config_value("DB_HOST") +DB_DATABASE = get_config_value("DB_DATABASE") +DB_USER = get_config_value("DB_USER") +DB_PASSWORD = get_config_value("DB_PASSWORD") +DB_PORT = get_config_value("DB_PORT") MACROTEMAS = { "demografia": { @@ -148,4 +153,4 @@ def require_config_value(value: str | None, env_name: str) -> str: status_code=500, detail=f"{env_name} não configurado. Defina no arquivo .config, .env ou nas variáveis de ambiente.", ) - return value \ No newline at end of file + return value From cb05e4fc4e1e9767889eda5f6d6622ec3192c836 Mon Sep 17 00:00:00 2001 From: nettoluis Date: Tue, 19 May 2026 15:21:19 -0300 Subject: [PATCH 4/5] add: postgreSQL connection handler library to requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 47e8ddb..1e622e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ fastapi matplotlib weasyprint dotenv +psycopg2-binary From 4a79d87d9415c7b7ab179bcb967d2d470130f6a6 Mon Sep 17 00:00:00 2001 From: nettoluis Date: Tue, 19 May 2026 15:25:00 -0300 Subject: [PATCH 5/5] feat: connection and test connection to the OCA database --- utils/database.py | 69 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/utils/database.py b/utils/database.py index 4aeb550..50a1803 100644 --- a/utils/database.py +++ b/utils/database.py @@ -1,24 +1,49 @@ import psycopg2 +from config import DB_HOST +from config import DB_DATABASE +from config import DB_USER +from config import DB_PASSWORD +from config import DB_PORT -try: - conexao = psycopg2.connect( - host="db.oca-portal.com", - database="oca_server", - user="oca-user", - password="oca2026!", - port="5432" - ) - - cursor = conexao.cursor() - cursor.execute("SELECT version();") - versao_db = cursor.fetchone() - print("Conexão bem-sucedida!") - print(f"Versão do banco de dados: {versao_db[0]}") - - cursor.close() - conexao.close() - -except psycopg2.OperationalError as e: - print(f"Ocorreu um erro operacional (provavelmente rede ou credenciais): {e}") -except Exception as e: - print(f"Erro: {e}") +def get_connection(): + try: + conexao = psycopg2.connect( + host= DB_HOST, + database=DB_DATABASE, + user=DB_USER, + password=DB_PASSWORD, + port=DB_PORT, + connect_timeout=5 + ) + + cursor = conexao.cursor() + cursor.execute("SELECT version();") + versao_db = cursor.fetchone() + print("Conexão bem-sucedida!") + print(f"Versão do banco de dados: {versao_db[0]}") + + cursor.close() + conexao.close() + + except psycopg2.OperationalError as e: + print(f"Ocorreu um erro operacional (provavelmente rede ou credenciais): {e}") + except Exception as e: + print(f"Erro: {e}") + +def test_connection(): + try: + conn = get_connection() + cursor = conn.cursor() + cursor.execute("SELECT version();") + version = cursor.fetchone() + cursor.close() + conn.close() + return {"status": "connected", "version": version[0]} + except Exception as e: + return {"status": "error", "message": str(e)} + +def main(): + test_connection() + +if __name__ == "__main__": + main()