add location migrate
This commit is contained in:
64
migrate_sub_fields_to_location2025.py
Normal file
64
migrate_sub_fields_to_location2025.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
LocationからLocation2025へsub_loc_idとsubcategoryを移行するスクリプト
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
|
||||
# Djangoの設定
|
||||
sys.path.append('/app')
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
||||
django.setup()
|
||||
|
||||
from rog.models import Location, Location2025
|
||||
|
||||
def migrate_sub_fields():
|
||||
"""LocationからLocation2025にsub_loc_idとsubcategoryを移行"""
|
||||
|
||||
print("LocationからLocation2025への移行を開始します...")
|
||||
|
||||
# Locationデータを取得
|
||||
locations = Location.objects.all()
|
||||
print(f"移行対象のLocationレコード数: {locations.count()}")
|
||||
|
||||
# Location2025データとマッチングして更新
|
||||
updated_count = 0
|
||||
not_found_count = 0
|
||||
|
||||
for location in locations:
|
||||
# cp_numberとcp_nameでLocation2025を検索
|
||||
try:
|
||||
# location_idをcp_numberとして検索
|
||||
location2025_records = Location2025.objects.filter(
|
||||
cp_number=location.location_id,
|
||||
cp_name__icontains=location.location_name[:50] # 名前の部分一致
|
||||
)
|
||||
|
||||
if location2025_records.exists():
|
||||
for location2025 in location2025_records:
|
||||
# フィールドが空の場合のみ更新
|
||||
if not location2025.sub_loc_id and location.sub_loc_id:
|
||||
location2025.sub_loc_id = location.sub_loc_id
|
||||
|
||||
if not location2025.subcategory and location.subcategory:
|
||||
location2025.subcategory = location.subcategory
|
||||
|
||||
location2025.save()
|
||||
updated_count += 1
|
||||
print(f"✓ 更新: CP{location.location_id} - {location.location_name[:30]}...")
|
||||
else:
|
||||
not_found_count += 1
|
||||
print(f"✗ 未発見: CP{location.location_id} - {location.location_name[:30]}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"エラー (CP{location.location_id}): {str(e)}")
|
||||
|
||||
print(f"\n移行完了:")
|
||||
print(f" 更新レコード数: {updated_count}")
|
||||
print(f" 未発見レコード数: {not_found_count}")
|
||||
print(f" 元レコード数: {locations.count()}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate_sub_fields()
|
||||
Reference in New Issue
Block a user