SOY NV AI
commited on
Commit
·
47726c5
1
Parent(s):
4d2da15
Add database connection status logging and PostgreSQL connection verification
Browse files- app/__init__.py +34 -1
- app/core/config.py +6 -2
app/__init__.py
CHANGED
|
@@ -7,6 +7,7 @@ from flask_login import LoginManager
|
|
| 7 |
import sqlite3
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Optional
|
|
|
|
| 10 |
|
| 11 |
from app.database import db, User
|
| 12 |
from app.core.config import Config, get_config
|
|
@@ -154,8 +155,40 @@ def create_app() -> Flask:
|
|
| 154 |
|
| 155 |
# 데이터베이스 초기화 및 마이그레이션
|
| 156 |
with app.app_context():
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
migrate_database(app)
|
|
|
|
|
|
|
| 159 |
create_admin_user()
|
| 160 |
|
| 161 |
logger.info("Flask 애플리케이션이 초기화되었습니다.")
|
|
|
|
| 7 |
import sqlite3
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Optional
|
| 10 |
+
from sqlalchemy import create_engine, text
|
| 11 |
|
| 12 |
from app.database import db, User
|
| 13 |
from app.core.config import Config, get_config
|
|
|
|
| 155 |
|
| 156 |
# 데이터베이스 초기화 및 마이그레이션
|
| 157 |
with app.app_context():
|
| 158 |
+
# 데이터베이스 연결 정보 로깅
|
| 159 |
+
db_uri = app.config['SQLALCHEMY_DATABASE_URI']
|
| 160 |
+
if db_uri.startswith('postgresql://') or db_uri.startswith('postgres://'):
|
| 161 |
+
# PostgreSQL 연결 정보 (보안을 위해 비밀번호는 마스킹)
|
| 162 |
+
masked_uri = db_uri.split('@')[0].split('://')[0] + '://***@' + '@'.join(db_uri.split('@')[1:]) if '@' in db_uri else db_uri
|
| 163 |
+
logger.info(f"[데이터베이스] PostgreSQL 연결 시도: {masked_uri}")
|
| 164 |
+
# 연결 테스트
|
| 165 |
+
try:
|
| 166 |
+
engine = create_engine(db_uri)
|
| 167 |
+
with engine.connect() as conn:
|
| 168 |
+
result = conn.execute(text("SELECT version()"))
|
| 169 |
+
version = result.fetchone()[0]
|
| 170 |
+
logger.info(f"[데이터베이스] PostgreSQL 연결 성공! 버전: {version[:50]}...")
|
| 171 |
+
except Exception as e:
|
| 172 |
+
logger.error(f"[데이터베이스] PostgreSQL 연결 실패: {str(e)}")
|
| 173 |
+
logger.warning("[데이터베이스] SQLite로 폴백합니다.")
|
| 174 |
+
# SQLite로 폴백
|
| 175 |
+
db_uri = f'sqlite:///{config.INSTANCE_FOLDER / "finance_analysis.db"}'
|
| 176 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
|
| 177 |
+
else:
|
| 178 |
+
logger.info(f"[데이터베이스] SQLite 사용: {db_uri}")
|
| 179 |
+
|
| 180 |
+
# 데이터베이스 테이블 생성
|
| 181 |
+
try:
|
| 182 |
+
db.create_all()
|
| 183 |
+
logger.info("[데이터베이스] 테이블 생성 완료")
|
| 184 |
+
except Exception as e:
|
| 185 |
+
logger.error(f"[데이터베이스] 테이블 생성 실패: {str(e)}")
|
| 186 |
+
raise
|
| 187 |
+
|
| 188 |
+
# 마이그레이션 실행 (SQLite만 지원)
|
| 189 |
migrate_database(app)
|
| 190 |
+
|
| 191 |
+
# 관리자 계정 생성
|
| 192 |
create_admin_user()
|
| 193 |
|
| 194 |
logger.info("Flask 애플리케이션이 초기화되었습니다.")
|
app/core/config.py
CHANGED
|
@@ -24,11 +24,15 @@ class Config:
|
|
| 24 |
# PostgreSQL: postgresql://user:password@host:port/database
|
| 25 |
# SQLite (기본값): sqlite:///instance/finance_analysis.db
|
| 26 |
_database_url = os.getenv('DATABASE_URL', '')
|
| 27 |
-
if _database_url:
|
| 28 |
-
#
|
| 29 |
SQLALCHEMY_DATABASE_URI: str = _database_url
|
| 30 |
else:
|
| 31 |
# 기본값: SQLite 사용
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
SQLALCHEMY_DATABASE_URI: str = f'sqlite:///{PROJECT_ROOT / "instance" / "finance_analysis.db"}'
|
| 33 |
SQLALCHEMY_TRACK_MODIFICATIONS: bool = False
|
| 34 |
MAX_CONTENT_LENGTH: int = 100 * 1024 * 1024 # 100MB
|
|
|
|
| 24 |
# PostgreSQL: postgresql://user:password@host:port/database
|
| 25 |
# SQLite (기본값): sqlite:///instance/finance_analysis.db
|
| 26 |
_database_url = os.getenv('DATABASE_URL', '')
|
| 27 |
+
if _database_url and (_database_url.startswith('postgresql://') or _database_url.startswith('postgres://')):
|
| 28 |
+
# PostgreSQL URL이 제공되면 사용
|
| 29 |
SQLALCHEMY_DATABASE_URI: str = _database_url
|
| 30 |
else:
|
| 31 |
# 기본값: SQLite 사용
|
| 32 |
+
if _database_url:
|
| 33 |
+
# DATABASE_URL이 설정되었지만 PostgreSQL 형식이 아닌 경우 경고
|
| 34 |
+
import warnings
|
| 35 |
+
warnings.warn(f"DATABASE_URL이 설정되었지만 PostgreSQL 형식이 아닙니다. SQLite를 사용합니다: {_database_url[:50]}...")
|
| 36 |
SQLALCHEMY_DATABASE_URI: str = f'sqlite:///{PROJECT_ROOT / "instance" / "finance_analysis.db"}'
|
| 37 |
SQLALCHEMY_TRACK_MODIFICATIONS: bool = False
|
| 38 |
MAX_CONTENT_LENGTH: int = 100 * 1024 * 1024 # 100MB
|