104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
"""
|
|
Database migration to add MapRoute table for efficient map loading
|
|
Run this script to create the new table structure
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
# Add the project root to Python path
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from app import create_app, db
|
|
|
|
def create_map_route_table():
|
|
"""Create the MapRoute table"""
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
try:
|
|
# Create the MapRoute table
|
|
db.create_all()
|
|
print("✅ MapRoute table created successfully!")
|
|
|
|
# Show current tables
|
|
inspector = db.inspect(db.engine)
|
|
tables = inspector.get_table_names()
|
|
print(f"Current tables: {', '.join(tables)}")
|
|
|
|
if 'map_routes' in tables:
|
|
print("✅ map_routes table confirmed in database")
|
|
else:
|
|
print("❌ map_routes table not found")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error creating MapRoute table: {e}")
|
|
return False
|
|
|
|
return True
|
|
|
|
def populate_existing_routes():
|
|
"""Process existing published posts with GPX files to create map routes"""
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
try:
|
|
from app.models import Post, GPXFile, MapRoute
|
|
from app.utils.gpx_processor import create_map_route_from_gpx
|
|
|
|
# Find published posts with GPX files that don't have map routes yet
|
|
posts_with_gpx = db.session.query(Post).join(GPXFile).filter(
|
|
Post.published == True
|
|
).distinct().all()
|
|
|
|
print(f"Found {len(posts_with_gpx)} published posts with GPX files")
|
|
|
|
processed = 0
|
|
errors = 0
|
|
|
|
for post in posts_with_gpx:
|
|
# Check if map route already exists
|
|
existing_route = MapRoute.query.filter_by(post_id=post.id).first()
|
|
if existing_route:
|
|
print(f"⏭️ Post {post.id} already has a map route, skipping")
|
|
continue
|
|
|
|
# Get the first GPX file for this post
|
|
gpx_file = GPXFile.query.filter_by(post_id=post.id).first()
|
|
if gpx_file:
|
|
print(f"🔄 Processing post {post.id}: {post.title}")
|
|
success = create_map_route_from_gpx(gpx_file.id)
|
|
if success:
|
|
processed += 1
|
|
print(f"✅ Created map route for post {post.id}")
|
|
else:
|
|
errors += 1
|
|
print(f"❌ Failed to create map route for post {post.id}")
|
|
else:
|
|
print(f"⚠️ No GPX file found for post {post.id}")
|
|
|
|
print(f"\n📊 Summary:")
|
|
print(f"- Processed: {processed}")
|
|
print(f"- Errors: {errors}")
|
|
print(f"- Total posts with GPX: {len(posts_with_gpx)}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error populating existing routes: {e}")
|
|
return False
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
print("🚀 Creating MapRoute table and processing existing data...")
|
|
|
|
# Create the table
|
|
if create_map_route_table():
|
|
print("\n🔄 Processing existing published posts with GPX files...")
|
|
populate_existing_routes()
|
|
|
|
print("\n✅ Migration completed!")
|