From bafffd661a72c581e7b933123dd13bdc3871bdc3 Mon Sep 17 00:00:00 2001 From: livindra Date: Tue, 28 Apr 2026 21:36:11 +0700 Subject: [PATCH] deploy --- app.py | 30 ++++++++++-------------------- utils/mysql_db.py | 23 +++++++++++++++-------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index e379645..cb3ad07 100644 --- a/app.py +++ b/app.py @@ -51,26 +51,16 @@ try: get_diagnosis_by_prediction_id, get_engine, ) - - try: - engine = get_engine() - # quick connect test - with engine.connect() as conn: - # initialize tables if needed - try: - init_mysql_tables() - except Exception as init_err: - - print(f"⚠️ Warning: Database tables initialization failed: {init_err}") - print("✓ MySQL Database connected successfully") - MYSQL_AVAILABLE = True - except Exception as e: - import traceback - print('❌ MySQL not available at startup:') - print(f" Error: {e}") - print(" Cek: 1) MySQL Server berjalan? 2) .env credentials benar? 3) Database 'deteksi_pmk' ada?") - traceback.print_exc() - MYSQL_AVAILABLE = False + engine = get_engine() + MYSQL_AVAILABLE = engine is not None + if MYSQL_AVAILABLE: + try: + init_mysql_tables() + print("✓ MySQL database initialized") + except Exception as init_err: + print(f"⚠️ Warning: Database tables initialization failed: {init_err}") + else: + print("[APP] MySQL disabled: no valid database configuration found") except Exception as import_err: print(f"❌ Failed to import MySQL utilities: {import_err}") MYSQL_AVAILABLE = False diff --git a/utils/mysql_db.py b/utils/mysql_db.py index 514078b..6c5b06c 100644 --- a/utils/mysql_db.py +++ b/utils/mysql_db.py @@ -40,11 +40,15 @@ def _build_database_uri(): except Exception as e: print(f"[MYSQL] Ignoring invalid DATABASE_URL: {e}") - db_host = os.environ.get('DB_HOST') or os.environ.get('MYSQLHOST', 'localhost') - db_port = _normalize_port(os.environ.get('DB_PORT') or os.environ.get('MYSQLPORT')) - db_user = os.environ.get('DB_USER') or os.environ.get('MYSQLUSER', 'root') + db_host = (os.environ.get('DB_HOST') or os.environ.get('MYSQLHOST') or '').strip() + db_user = (os.environ.get('DB_USER') or os.environ.get('MYSQLUSER') or '').strip() db_password = os.environ.get('DB_PASSWORD') or os.environ.get('MYSQLPASSWORD', '') - db_name = os.environ.get('DB_NAME') or os.environ.get('MYSQLDATABASE', 'deteksi_pmk') + db_name = (os.environ.get('DB_NAME') or os.environ.get('MYSQLDATABASE') or '').strip() + + if not db_host or not db_user or not db_name: + return None + + db_port = _normalize_port(os.environ.get('DB_PORT') or os.environ.get('MYSQLPORT')) if db_password: return f"mysql+pymysql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}" @@ -55,10 +59,13 @@ def _build_database_uri(): DATABASE_URI = _build_database_uri() # Create engine -try: - engine = create_engine(DATABASE_URI, echo=False, pool_pre_ping=True) -except Exception as e: - print(f"[MYSQL] Database engine unavailable: {e}") +if DATABASE_URI: + try: + engine = create_engine(DATABASE_URI, echo=False, pool_pre_ping=True) + except Exception as e: + print(f"[MYSQL] Database engine unavailable: {e}") + engine = None +else: engine = None Base = declarative_base()