#!/usr/bin/env python3 """ PostgreSQL 테이블 확인 스크립트 distribution-service의 테이블 생성 여부를 확인합니다. """ import psycopg2 import sys # DB 연결 정보 DB_CONFIG = { 'host': '4.217.133.59', 'port': 5432, 'database': 'distributiondb', 'user': 'eventuser', 'password': 'Hi5Jessica!' } def main(): try: # DB 연결 print(f"Connecting to database: {DB_CONFIG['host']}:{DB_CONFIG['port']}/{DB_CONFIG['database']}") conn = psycopg2.connect(**DB_CONFIG) cursor = conn.cursor() # 테이블 목록 조회 print("\n=== Tables in distributiondb ===") cursor.execute(""" SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name """) tables = cursor.fetchall() if not tables: print("No tables found.") else: for table in tables: print(f" - {table[0]}") # distribution_status 테이블 구조 확인 if any('distribution_status' in table for table in tables): print("\n=== distribution_status table structure ===") cursor.execute(""" SELECT column_name, data_type, character_maximum_length, is_nullable FROM information_schema.columns WHERE table_name = 'distribution_status' ORDER BY ordinal_position """) columns = cursor.fetchall() for col in columns: nullable = "NULL" if col[3] == 'YES' else "NOT NULL" max_len = f"({col[2]})" if col[2] else "" print(f" - {col[0]}: {col[1]}{max_len} {nullable}") # channel_status 테이블 구조 확인 if any('channel_status' in table for table in tables): print("\n=== channel_status table structure ===") cursor.execute(""" SELECT column_name, data_type, character_maximum_length, is_nullable FROM information_schema.columns WHERE table_name = 'channel_status' ORDER BY ordinal_position """) columns = cursor.fetchall() for col in columns: nullable = "NULL" if col[3] == 'YES' else "NOT NULL" max_len = f"({col[2]})" if col[2] else "" print(f" - {col[0]}: {col[1]}{max_len} {nullable}") # 인덱스 확인 print("\n=== Indexes ===") cursor.execute(""" SELECT tablename, indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' AND tablename IN ('distribution_status', 'channel_status') ORDER BY tablename, indexname """) indexes = cursor.fetchall() for idx in indexes: print(f" - {idx[0]}.{idx[1]}") print(f" {idx[2]}") # 외래 키 확인 print("\n=== Foreign Keys ===") cursor.execute(""" SELECT tc.table_name, kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name IN ('distribution_status', 'channel_status') """) fks = cursor.fetchall() for fk in fks: print(f" - {fk[0]}.{fk[1]} -> {fk[2]}.{fk[3]}") # 연결 종료 cursor.close() conn.close() print("\n✅ Database connection successful!") return 0 except Exception as e: print(f"\n❌ Error: {e}", file=sys.stderr) return 1 if __name__ == "__main__": sys.exit(main())