hgzero/notification/fix_db_constraint.py

130 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Notification DB 체크 제약 조건 수정 스크립트
실행 전 psycopg2 설치 필요: pip install psycopg2-binary
"""
import psycopg2
import sys
# 데이터베이스 연결 정보
DB_CONFIG = {
'host': '4.230.159.143',
'port': 5432,
'database': 'notificationdb',
'user': 'hgzerouser',
'password': 'Hi5Jessica!'
}
# 실행할 SQL
SQL_DROP_CONSTRAINT = """
ALTER TABLE notifications
DROP CONSTRAINT IF EXISTS notifications_notification_type_check;
"""
SQL_ADD_CONSTRAINT = """
ALTER TABLE notifications
ADD CONSTRAINT notifications_notification_type_check
CHECK (notification_type IN (
'MEETING_INVITATION',
'TODO_ASSIGNED',
'TODO_REMINDER',
'MEETING_REMINDER',
'MINUTES_UPDATED',
'TODO_COMPLETED'
));
"""
SQL_VERIFY = """
SELECT constraint_name, check_clause
FROM information_schema.check_constraints
WHERE constraint_name = 'notifications_notification_type_check';
"""
def main():
print("=" * 70)
print("Notification DB 체크 제약 조건 수정 시작")
print("=" * 70)
conn = None
cursor = None
try:
# 데이터베이스 연결
print(f"\n1. 데이터베이스 연결 중...")
print(f" Host: {DB_CONFIG['host']}")
print(f" Database: {DB_CONFIG['database']}")
conn = psycopg2.connect(**DB_CONFIG)
cursor = conn.cursor()
print(" ✅ 연결 성공")
# 기존 제약 조건 삭제
print("\n2. 기존 체크 제약 조건 삭제 중...")
cursor.execute(SQL_DROP_CONSTRAINT)
print(" ✅ 삭제 완료")
# 새로운 제약 조건 추가
print("\n3. 새로운 체크 제약 조건 추가 중...")
cursor.execute(SQL_ADD_CONSTRAINT)
print(" ✅ 추가 완료")
# 변경 사항 커밋
conn.commit()
print("\n4. 변경 사항 커밋 완료")
# 결과 확인
print("\n5. 제약 조건 확인 중...")
cursor.execute(SQL_VERIFY)
result = cursor.fetchone()
if result:
print(" ✅ 제약 조건이 정상적으로 생성되었습니다")
print(f" - Constraint Name: {result[0]}")
print(f" - Check Clause: {result[1]}")
else:
print(" ⚠️ 제약 조건을 찾을 수 없습니다")
print("\n" + "=" * 70)
print("✅ 모든 작업이 성공적으로 완료되었습니다!")
print("=" * 70)
print("\n다음 단계:")
print("1. notification 서비스를 재시작하세요")
print("2. 로그에서 에러가 사라졌는지 확인하세요")
print(" tail -f notification/logs/notification-service.log")
return 0
except psycopg2.Error as e:
print(f"\n❌ 데이터베이스 오류 발생: {e}")
if conn:
conn.rollback()
return 1
except Exception as e:
print(f"\n❌ 예기치 않은 오류 발생: {e}")
if conn:
conn.rollback()
return 1
finally:
# 연결 종료
if cursor:
cursor.close()
if conn:
conn.close()
print("\n6. 데이터베이스 연결 종료")
if __name__ == "__main__":
try:
import psycopg2
except ImportError:
print("❌ psycopg2 모듈이 설치되지 않았습니다.")
print("\n다음 명령어로 설치하세요:")
print(" pip install psycopg2-binary")
print("\n또는 conda를 사용하는 경우:")
print(" conda install -c conda-forge psycopg2")
sys.exit(1)
sys.exit(main())