Update database.py
parent
59805cff64
commit
7aa4373be2
24
database.py
24
database.py
|
|
@ -11,15 +11,8 @@ load_dotenv()
|
|||
|
||||
DB_PATH = Path(__file__).parent / "quiz_app.db"
|
||||
|
||||
load_dotenv()
|
||||
|
||||
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}"
|
||||
# SQLite 데이터베이스 URL 설정
|
||||
DATABASE_URL = f"sqlite:///{DB_PATH}"
|
||||
|
||||
engine = create_engine(DATABASE_URL, echo=True)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
|
@ -303,8 +296,8 @@ def migrate_database():
|
|||
# 기존 데이터 마이그레이션
|
||||
cursor.execute("""
|
||||
INSERT INTO learning_history
|
||||
(user_id, group_code, score, total_questions, timestamp)
|
||||
SELECT 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, question_content, feedback, user_choice, correct_answer, timestamp
|
||||
FROM learning_history_old
|
||||
""")
|
||||
|
||||
|
|
@ -320,6 +313,15 @@ def migrate_database():
|
|||
if conn:
|
||||
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
|
||||
init_db()
|
||||
# Migrate database to new schema
|
||||
|
|
|
|||
Loading…
Reference in New Issue