#!/usr/bin/env python """ データベース接続状況とold_rogdbデータ確認スクリプト 現在のDB接続状況を確認し、old_rogdbの実際のデータを調査 """ import os import sys import django if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') django.setup() from django.db import connection, connections from django.conf import settings print("=== データベース接続状況確認 ===") try: # 現在のデータベース設定を確認 print("\\n1. Django設定確認:") databases = settings.DATABASES for db_name, config in databases.items(): print(f" {db_name}: {config.get('NAME', 'Unknown')} @ {config.get('HOST', 'localhost')}") with connection.cursor() as cursor: # 現在接続しているデータベース名を確認 cursor.execute("SELECT current_database();") current_db = cursor.fetchone()[0] print(f"\\n2. 現在接続中のDB: {current_db}") # データベース内のテーブル一覧確認 cursor.execute(""" SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE '%rog%' ORDER BY table_name; """) tables = cursor.fetchall() print(f"\\n3. rogaine関連テーブル:") for table in tables: print(f" - {table[0]}") # old_rogdbスキーマまたはテーブルの存在確認 cursor.execute(""" SELECT schemaname, tablename, hasindexes, hasrules, hastriggers FROM pg_tables WHERE tablename LIKE '%rog%' ORDER BY schemaname, tablename; """) all_rog_tables = cursor.fetchall() print(f"\\n4. 全スキーマのrog関連テーブル:") for schema, table, idx, rules, triggers in all_rog_tables: print(f" {schema}.{table}") # データ存在確認 print(f"\\n5. 現在のデータ状況:") # rog_entry データ確認 try: cursor.execute("SELECT COUNT(*) FROM rog_entry;") entry_count = cursor.fetchone()[0] print(f" rog_entry: {entry_count}件") if entry_count > 0: cursor.execute("SELECT * FROM rog_entry LIMIT 3;") sample_entries = cursor.fetchall() print(" サンプルエントリー:") for entry in sample_entries: print(f" ID:{entry[0]}, Team:{entry[5]}, Event:{entry[3]}") except Exception as e: print(f" rog_entry エラー: {e}") # rog_team データ確認 try: cursor.execute("SELECT COUNT(*) FROM rog_team;") team_count = cursor.fetchone()[0] print(f" rog_team: {team_count}件") if team_count > 0: cursor.execute("SELECT id, team_name, zekken_number FROM rog_team WHERE zekken_number IS NOT NULL AND zekken_number != '' LIMIT 5;") sample_teams = cursor.fetchall() print(" ゼッケン付きチーム:") for team in sample_teams: print(f" ID:{team[0]}, Name:{team[1]}, Zekken:{team[2]}") except Exception as e: print(f" rog_team エラー: {e}") # もしold_rogdbが別のスキーマにある場合 print(f"\\n6. 別スキーマのold_rogdbデータ確認:") try: # old_rogdbスキーマが存在するかチェック cursor.execute(""" SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE '%old%' OR schema_name LIKE '%rog%'; """) schemas = cursor.fetchall() print(" 利用可能なスキーマ:") for schema in schemas: print(f" - {schema[0]}") # old_rogdbスキーマがある場合、そのデータを確認 for schema in schemas: schema_name = schema[0] if 'old' in schema_name.lower(): try: cursor.execute(f"SELECT COUNT(*) FROM {schema_name}.rog_entry;") old_entry_count = cursor.fetchone()[0] print(f" {schema_name}.rog_entry: {old_entry_count}件") except Exception as e: print(f" {schema_name}.rog_entry: アクセスエラー - {e}") except Exception as e: print(f" スキーマ確認エラー: {e}") # old_rogdbが別のデータベースの場合の確認 print(f"\\n7. 利用可能なデータベース一覧:") cursor.execute(""" SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname; """) databases = cursor.fetchall() for db in databases: print(f" - {db[0]}") except Exception as e: print(f"❌ エラーが発生しました: {e}") import traceback traceback.print_exc()