Update database.py

main
no 2025-05-26 08:26:13 +00:00
parent 59805cff64
commit 7aa4373be2
1 changed files with 13 additions and 11 deletions

View File

@ -11,15 +11,8 @@ load_dotenv()
DB_PATH = Path(__file__).parent / "quiz_app.db" DB_PATH = Path(__file__).parent / "quiz_app.db"
load_dotenv() # SQLite 데이터베이스 URL 설정
DATABASE_URL = f"sqlite:///{DB_PATH}"
db_name = os.getenv("DB_NAME")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_host = os.getenv("DB_HOST", "localhost")
db_port = os.getenv("DB_PORT", "5432")
DATABASE_URL = f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
engine = create_engine(DATABASE_URL, echo=True) engine = create_engine(DATABASE_URL, echo=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@ -303,8 +296,8 @@ def migrate_database():
# 기존 데이터 마이그레이션 # 기존 데이터 마이그레이션
cursor.execute(""" cursor.execute("""
INSERT INTO learning_history INSERT INTO learning_history
(user_id, group_code, score, total_questions, timestamp) (user_id, group_code, score, total_questions, question_content, feedback, user_choice, correct_answer, timestamp)
SELECT user_id, group_code, score, total_questions, timestamp SELECT user_id, group_code, score, total_questions, question_content, feedback, user_choice, correct_answer, timestamp
FROM learning_history_old FROM learning_history_old
""") """)
@ -320,6 +313,15 @@ def migrate_database():
if conn: if conn:
conn.close() conn.close()
# Initialize database when module is imported
init_db()
# Migrate database to new schema
migrate_database()
return False
finally:
if conn:
conn.close()
# Initialize database when module is imported # Initialize database when module is imported
init_db() init_db()
# Migrate database to new schema # Migrate database to new schema