294 lines
15 KiB
Python
294 lines
15 KiB
Python
|
|
#!/usr/bin/env python
|
|||
|
|
"""
|
|||
|
|
old_rogdb から rogdb へのFC岐阜エントリー移行スクリプト
|
|||
|
|
old_rogdbのFC岐阜イベント(event_id=10)のゼッケン番号付きエントリーを移行
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
import django
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
|||
|
|
django.setup()
|
|||
|
|
|
|||
|
|
from django.db import connections, transaction
|
|||
|
|
from rog.models import NewEvent2, Team, Entry, NewCategory, CustomUser
|
|||
|
|
|
|||
|
|
print("=== old_rogdb から FC岐阜エントリー移行 ===")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# データベース接続を取得
|
|||
|
|
default_db = connections['default'] # rogdb
|
|||
|
|
old_db = connections.databases.get('old_rogdb')
|
|||
|
|
|
|||
|
|
if not old_db:
|
|||
|
|
print("❌ old_rogdb接続設定が見つかりません。別DB接続を試行します。")
|
|||
|
|
|
|||
|
|
# old_rogdbに直接接続してデータを取得
|
|||
|
|
import psycopg2
|
|||
|
|
|
|||
|
|
# old_rogdbへの直接接続
|
|||
|
|
old_conn = psycopg2.connect(
|
|||
|
|
host='postgres-db',
|
|||
|
|
database='old_rogdb',
|
|||
|
|
user='admin',
|
|||
|
|
password='admin123456'
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print("✅ old_rogdbに接続成功")
|
|||
|
|
|
|||
|
|
with old_conn.cursor() as old_cursor:
|
|||
|
|
# old_rogdbのFC岐阜エントリーデータを取得
|
|||
|
|
old_cursor.execute("""
|
|||
|
|
SELECT re.id, re.team_id, re.zekken_number, re.zekken_label,
|
|||
|
|
rt.team_name, re.category_id, re.date, re.owner_id,
|
|||
|
|
rc.category_name
|
|||
|
|
FROM rog_entry re
|
|||
|
|
JOIN rog_team rt ON re.team_id = rt.id
|
|||
|
|
LEFT JOIN rog_newcategory rc ON re.category_id = rc.id
|
|||
|
|
WHERE re.event_id = 10
|
|||
|
|
ORDER BY re.zekken_number;
|
|||
|
|
""")
|
|||
|
|
|
|||
|
|
old_fc_data = old_cursor.fetchall()
|
|||
|
|
print(f"\\n✅ old_rogdb FC岐阜エントリー: {len(old_fc_data)}件")
|
|||
|
|
|
|||
|
|
if old_fc_data:
|
|||
|
|
print("\\nold_rogdb FC岐阜データサンプル(最初の5件):")
|
|||
|
|
for i, (entry_id, team_id, zekken, label, team_name, cat_id, date, owner_id, cat_name) in enumerate(old_fc_data[:5]):
|
|||
|
|
print(f" {i+1}. Entry {entry_id}: Team '{team_name}' - ゼッケン{zekken} ({cat_name})")
|
|||
|
|
|
|||
|
|
# FC岐阜イベントを確認
|
|||
|
|
fc_event = NewEvent2.objects.filter(id=10).first()
|
|||
|
|
if not fc_event:
|
|||
|
|
print("❌ FC岐阜イベント(ID:10)が見つかりません")
|
|||
|
|
old_conn.close()
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
print(f"\\n✅ FC岐阜イベント: {fc_event.event_name}")
|
|||
|
|
|
|||
|
|
# データ移行開始
|
|||
|
|
print("\\n=== old_rogdb から rogdb へデータ移行開始 ===")
|
|||
|
|
migrated_count = 0
|
|||
|
|
error_count = 0
|
|||
|
|
|
|||
|
|
for entry_id, team_id, zekken, label, team_name, cat_id, date, owner_id, cat_name in old_fc_data:
|
|||
|
|
try:
|
|||
|
|
with transaction.atomic():
|
|||
|
|
# カテゴリを取得または作成
|
|||
|
|
if cat_id and cat_name:
|
|||
|
|
category, cat_created = NewCategory.objects.get_or_create(
|
|||
|
|
id=cat_id,
|
|||
|
|
defaults={
|
|||
|
|
'category_name': cat_name,
|
|||
|
|
'category_number': cat_id
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
if cat_created:
|
|||
|
|
print(f" カテゴリ作成: {cat_name}")
|
|||
|
|
else:
|
|||
|
|
category = None
|
|||
|
|
|
|||
|
|
# チームを取得または作成
|
|||
|
|
team, team_created = Team.objects.get_or_create(
|
|||
|
|
id=team_id,
|
|||
|
|
defaults={
|
|||
|
|
'team_name': team_name,
|
|||
|
|
'owner_id': owner_id or 1,
|
|||
|
|
'category': category,
|
|||
|
|
'event_id': fc_event.id
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if team_created:
|
|||
|
|
print(f" チーム作成: {team_name} (ID: {team_id})")
|
|||
|
|
|
|||
|
|
# エントリーを作成
|
|||
|
|
entry, entry_created = Entry.objects.get_or_create(
|
|||
|
|
team=team,
|
|||
|
|
event=fc_event,
|
|||
|
|
defaults={
|
|||
|
|
'category': category,
|
|||
|
|
'date': date or fc_event.start_datetime,
|
|||
|
|
'owner_id': owner_id or 1,
|
|||
|
|
'zekken_number': int(zekken) if zekken else 0,
|
|||
|
|
'zekken_label': label or f"FC岐阜-{zekken}",
|
|||
|
|
'is_active': True,
|
|||
|
|
'hasParticipated': False,
|
|||
|
|
'hasGoaled': False
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if entry_created:
|
|||
|
|
print(f" ✅ エントリー作成: {team_name} - ゼッケン{zekken}")
|
|||
|
|
migrated_count += 1
|
|||
|
|
else:
|
|||
|
|
print(f" 🔄 既存エントリー: {team_name} - ゼッケン{zekken}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
error_count += 1
|
|||
|
|
print(f" ❌ エラー: {team_name} - {e}")
|
|||
|
|
|
|||
|
|
old_conn.close()
|
|||
|
|
|
|||
|
|
print(f"\\n=== 移行完了 ===")
|
|||
|
|
print(f"移行成功: {migrated_count}件")
|
|||
|
|
print(f"エラー: {error_count}件")
|
|||
|
|
|
|||
|
|
# 最終確認
|
|||
|
|
fc_entries = Entry.objects.filter(event=fc_event).order_by('zekken_number')
|
|||
|
|
print(f"\\n🎉 FC岐阜イベント総エントリー: {fc_entries.count()}件")
|
|||
|
|
|
|||
|
|
if fc_entries.exists():
|
|||
|
|
print("\\nゼッケン番号一覧(最初の10件):")
|
|||
|
|
for entry in fc_entries[:10]:
|
|||
|
|
print(f" ゼッケン{entry.zekken_number}: {entry.team.team_name}")
|
|||
|
|
print("\\n🎉 FC岐阜イベントのゼッケン番号表示問題が解決されました!")
|
|||
|
|
print("\\n🎯 通過審査管理画面でFC岐阜を選択すると、ゼッケン番号が表示されるようになります。")
|
|||
|
|
else:
|
|||
|
|
print("❌ old_rogdbにもFC岐阜エントリーデータがありません")
|
|||
|
|
old_conn.close()
|
|||
|
|
|
|||
|
|
else:
|
|||
|
|
# 通常のDjango接続設定がある場合の処理
|
|||
|
|
with default_db.cursor() as cursor:
|
|||
|
|
# まず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(f"利用可能なスキーマ: {schemas}")
|
|||
|
|
|
|||
|
|
# old_rogdbデータベースに直接接続を試行
|
|||
|
|
cursor.execute("SELECT current_database();")
|
|||
|
|
current_db = cursor.fetchone()[0]
|
|||
|
|
print(f"現在のDB: {current_db}")
|
|||
|
|
|
|||
|
|
# データベース一覧を確認
|
|||
|
|
cursor.execute("""
|
|||
|
|
SELECT datname FROM pg_database
|
|||
|
|
WHERE datistemplate = false AND datname != 'postgres';
|
|||
|
|
""")
|
|||
|
|
databases = cursor.fetchall()
|
|||
|
|
print(f"利用可能なDB: {[db[0] for db in databases]}")
|
|||
|
|
|
|||
|
|
# old_rogdbのrog_entryデータを確認
|
|||
|
|
try:
|
|||
|
|
# 別データベースのテーブルにアクセスする方法を試行
|
|||
|
|
cursor.execute("""
|
|||
|
|
SELECT table_name FROM information_schema.tables
|
|||
|
|
WHERE table_schema = 'public'
|
|||
|
|
AND table_name LIKE '%entry%';
|
|||
|
|
""")
|
|||
|
|
entry_tables = cursor.fetchall()
|
|||
|
|
print(f"エントリー関連テーブル: {entry_tables}")
|
|||
|
|
|
|||
|
|
# FC岐阜関連のエントリーデータを確認
|
|||
|
|
# まず現在のDBで状況確認
|
|||
|
|
cursor.execute("""
|
|||
|
|
SELECT COUNT(*) FROM rog_entry WHERE event_id = 10;
|
|||
|
|
""")
|
|||
|
|
current_fc_entries = cursor.fetchone()[0]
|
|||
|
|
print(f"現在のDB FC岐阜エントリー: {current_fc_entries}件")
|
|||
|
|
|
|||
|
|
if current_fc_entries > 0:
|
|||
|
|
cursor.execute("""
|
|||
|
|
SELECT re.id, re.team_id, re.zekken_number, re.zekken_label,
|
|||
|
|
rt.team_name, re.category_id
|
|||
|
|
FROM rog_entry re
|
|||
|
|
JOIN rog_team rt ON re.team_id = rt.id
|
|||
|
|
WHERE re.event_id = 10
|
|||
|
|
AND re.zekken_number IS NOT NULL
|
|||
|
|
ORDER BY re.zekken_number
|
|||
|
|
LIMIT 10;
|
|||
|
|
""")
|
|||
|
|
fc_data = cursor.fetchall()
|
|||
|
|
|
|||
|
|
print(f"\\n✅ FC岐阜エントリーデータ(最初の10件):")
|
|||
|
|
for entry_id, team_id, zekken, label, team_name, cat_id in fc_data:
|
|||
|
|
print(f" Entry {entry_id}: Team {team_id} '{team_name}' - ゼッケン{zekken}")
|
|||
|
|
|
|||
|
|
# FC岐阜イベントを取得
|
|||
|
|
fc_event = NewEvent2.objects.filter(id=10).first()
|
|||
|
|
if not fc_event:
|
|||
|
|
print("❌ FC岐阜イベント(ID:10)が見つかりません")
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
print(f"\\n✅ FC岐阜イベント: {fc_event.event_name}")
|
|||
|
|
|
|||
|
|
# エントリーデータを新しいEntry modelに同期
|
|||
|
|
print("\\n=== エントリーデータ同期開始 ===")
|
|||
|
|
updated_count = 0
|
|||
|
|
|
|||
|
|
# 全FC岐阜エントリーを取得
|
|||
|
|
cursor.execute("""
|
|||
|
|
SELECT re.id, re.team_id, re.zekken_number, re.zekken_label,
|
|||
|
|
rt.team_name, re.category_id, re.date, re.owner_id
|
|||
|
|
FROM rog_entry re
|
|||
|
|
JOIN rog_team rt ON re.team_id = rt.id
|
|||
|
|
WHERE re.event_id = 10
|
|||
|
|
ORDER BY re.zekken_number;
|
|||
|
|
""")
|
|||
|
|
all_fc_data = cursor.fetchall()
|
|||
|
|
|
|||
|
|
for entry_id, team_id, zekken, label, team_name, cat_id, date, owner_id in all_fc_data:
|
|||
|
|
try:
|
|||
|
|
# チームを取得
|
|||
|
|
team = Team.objects.get(id=team_id)
|
|||
|
|
|
|||
|
|
# カテゴリを取得
|
|||
|
|
category = NewCategory.objects.get(id=cat_id) if cat_id else None
|
|||
|
|
|
|||
|
|
# エントリーを更新または作成
|
|||
|
|
entry, created = Entry.objects.update_or_create(
|
|||
|
|
team=team,
|
|||
|
|
event=fc_event,
|
|||
|
|
defaults={
|
|||
|
|
'category': category,
|
|||
|
|
'date': date or fc_event.start_datetime,
|
|||
|
|
'owner_id': owner_id,
|
|||
|
|
'zekken_number': int(zekken) if zekken else 0,
|
|||
|
|
'zekken_label': label or f"FC岐阜-{zekken}",
|
|||
|
|
'is_active': True,
|
|||
|
|
'hasParticipated': False,
|
|||
|
|
'hasGoaled': False
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if created:
|
|||
|
|
print(f" ✅ エントリー作成: {team_name} - ゼッケン{zekken}")
|
|||
|
|
else:
|
|||
|
|
print(f" 🔄 エントリー更新: {team_name} - ゼッケン{zekken}")
|
|||
|
|
updated_count += 1
|
|||
|
|
|
|||
|
|
except Team.DoesNotExist:
|
|||
|
|
print(f" ⚠️ チーム{team_id}が見つかりません: {team_name}")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ エラー: {e}")
|
|||
|
|
|
|||
|
|
print(f"\\n✅ 処理完了: {updated_count}件のエントリーを処理")
|
|||
|
|
|
|||
|
|
# 最終確認
|
|||
|
|
fc_entries = Entry.objects.filter(event=fc_event).order_by('zekken_number')
|
|||
|
|
print(f"\\n🎉 FC岐阜イベント総エントリー: {fc_entries.count()}件")
|
|||
|
|
|
|||
|
|
if fc_entries.exists():
|
|||
|
|
print("\\nゼッケン番号一覧(最初の10件):")
|
|||
|
|
for entry in fc_entries[:10]:
|
|||
|
|
print(f" ゼッケン{entry.zekken_number}: {entry.team.team_name}")
|
|||
|
|
print("\\n🎉 FC岐阜イベントのゼッケン番号表示問題が解決されました!")
|
|||
|
|
else:
|
|||
|
|
print("❌ 現在のDBにFC岐阜エントリーデータがありません")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ データ確認エラー: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ エラーが発生しました: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|