#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymysql
import sys

# UTF-8 출력 설정
if sys.platform == 'win32':
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

# 데이터베이스 연결
conn = pymysql.connect(
    host='officialsite.kr',
    user='admin',
    password='dlgks~123',
    database='app_master',
    port=23306,
    charset='utf8mb4'
)

try:
    cursor = conn.cursor()

    # link_url 컬럼 추가
    print('link_url 컬럼 추가 중...')
    cursor.execute('''
        ALTER TABLE babynote_news_gallery
        ADD COLUMN link_url TEXT NULL AFTER image_url
    ''')

    conn.commit()
    print('✅ link_url 컬럼이 추가되었습니다.')

    # 테이블 구조 확인
    print()
    print('===== 업데이트된 테이블 구조 =====')
    cursor.execute('DESCRIBE babynote_news_gallery')

    columns = cursor.fetchall()
    for col in columns:
        print(f'{col[0]:<20} {col[1]:<20} {col[2]:<10} {col[3]:<10}')

except pymysql.err.OperationalError as e:
    if '1060' in str(e):
        print('⚠️  link_url 컬럼이 이미 존재합니다.')
    else:
        print(f'오류 발생: {e}')
except Exception as e:
    print(f'오류 발생: {e}')
    conn.rollback()
finally:
    cursor.close()
    conn.close()
