Files
iDRAC_Info/update_user_approval.py
2025-11-28 18:27:15 +09:00

68 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
DB 업데이트 스크립트: User 테이블에 승인 관련 필드 추가
- is_approved: 가입 승인 여부
- approval_token: 승인 토큰 (텔레그램 버튼 콜백용)
"""
import sys
import os
# 프로젝트 루트를 sys.path에 추가
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import app
from backend.models.user import db
def update_user_table():
"""User 테이블에 승인 관련 컬럼 추가"""
with app.app_context():
try:
# 컬럼 존재 여부 확인
inspector = db.inspect(db.engine)
columns = [col['name'] for col in inspector.get_columns('user')]
if 'is_approved' not in columns:
print("✅ is_approved 컬럼 추가 중...")
db.session.execute(db.text(
"ALTER TABLE user ADD COLUMN is_approved BOOLEAN DEFAULT 0 NOT NULL"
))
print("✅ is_approved 컬럼 추가 완료")
else:
print(" is_approved 컬럼이 이미 존재합니다.")
if 'approval_token' not in columns:
print("✅ approval_token 컬럼 추가 중...")
# SQLite는 UNIQUE 제약조건을 ALTER TABLE에서 직접 추가할 수 없음
# 먼저 컬럼만 추가하고, 나중에 인덱스로 UNIQUE 처리
db.session.execute(db.text(
"ALTER TABLE user ADD COLUMN approval_token VARCHAR(100)"
))
print("✅ approval_token 컬럼 추가 완료")
# UNIQUE 인덱스 생성
try:
db.session.execute(db.text(
"CREATE UNIQUE INDEX idx_user_approval_token ON user(approval_token)"
))
print("✅ approval_token UNIQUE 인덱스 생성 완료")
except Exception as e:
print(f" UNIQUE 인덱스 생성 스킵 (이미 존재하거나 NULL 값 때문): {e}")
else:
print(" approval_token 컬럼이 이미 존재합니다.")
# 기존 사용자들은 자동 승인 처리
print("✅ 기존 사용자 자동 승인 처리 중...")
db.session.execute(db.text(
"UPDATE user SET is_approved = 1 WHERE is_approved = 0"
))
db.session.commit()
print("✅ 데이터베이스 업데이트 완료!")
except Exception as e:
print(f"❌ 오류 발생: {e}")
db.session.rollback()
raise
if __name__ == "__main__":
update_user_table()