This commit is contained in:
livindra 2026-04-28 21:36:11 +07:00
parent ff92f07645
commit bafffd661a
2 changed files with 25 additions and 28 deletions

30
app.py
View File

@ -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

View File

@ -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()