Files
test-mcp/migrate_db_history.py

44 lines
1.8 KiB
Python

import pymysql
import os
def get_db():
return pymysql.connect(
host='localhost', user='root', password='45278434',
database=os.getenv('DB_NAME', 'PM_proto'), charset='utf8mb4'
)
def migrate_to_timeseries():
conn = get_db()
try:
with conn.cursor() as cursor:
# 1. 기존 고유 제약 조건 제거 (project_id 중복 허용을 위함)
try:
cursor.execute("ALTER TABLE overseas_projects DROP INDEX project_id")
print(">>> 기존 project_id 고유 제약 제거")
except: pass
# 2. crawl_date 컬럼 추가 (날짜별 데이터 구분을 위함)
cursor.execute("DESCRIBE overseas_projects")
cols = [row[0] for row in cursor.fetchall()]
if 'crawl_date' not in cols:
cursor.execute("ALTER TABLE overseas_projects ADD COLUMN crawl_date DATE AFTER project_id")
print(">>> crawl_date 컬럼 추가")
# 3. 기존 데이터의 crawl_date를 오늘로 채움
cursor.execute("UPDATE overseas_projects SET crawl_date = DATE(updated_at) WHERE crawl_date IS NULL")
# 4. 새로운 복합 고유 제약 추가 (ID + 날짜 조합으로 중복 방지)
# 같은 날짜에 다시 크롤링하면 덮어쓰고, 날짜가 다르면 새로 생성됨
try:
cursor.execute("ALTER TABLE overseas_projects ADD UNIQUE INDEX idx_project_date (project_id, crawl_date)")
print(">>> 복합 고유 제약(project_id + crawl_date) 추가 완료")
except: pass
conn.commit()
print(">>> DB 시계열 마이그레이션 성공!")
finally:
conn.close()
if __name__ == "__main__":
migrate_to_timeseries()