Fix Interactive Route Map: Create MapRoute records from GPX files

- Create MapRoute database records for all GPX files for map visualization
- Populate route statistics (distance, elevation, coordinates) from GPX parsing
- Update GPX file statistics to mirror MapRoute data for post detail pages
- Enable /community/api/routes endpoint to return proper route data for map iframe
- Fix post detail page GPX statistics display

This resolves the issue where the community map showed '2 routes discovered'
but routes weren't actually rendering on the Leaflet.js map visualization.

Changes:
- Dockerfile: Updated init script paths and added migrate-db call
- app/__init__.py: Added admin user auto-creation with error handling
- app/routes/community.py: Added debug logging and API route for map data
- docker-compose.yml: Simplified to use .env for environment variables
- run.py: Added comprehensive database schema migration command
This commit is contained in:
2025-12-31 18:46:21 +02:00
parent 16cc3fb0ba
commit f8508a436a
5 changed files with 142 additions and 90 deletions

147
run.py
View File

@@ -20,84 +20,101 @@ def make_shell_context():
@app.cli.command()
def init_db():
"""Initialize the database."""
"""Initialize the database with complete schema."""
print('Creating all database tables...')
db.create_all()
print('Database initialized.')
print('Database schema created successfully')
@app.cli.command()
def migrate_db():
"""Apply database migrations."""
from sqlalchemy import text
"""Apply all database schema migrations to ensure compatibility."""
from sqlalchemy import text, inspect
try:
# Check if the media_folder column exists
result = db.session.execute(text('PRAGMA table_info(posts)'))
columns = [row[1] for row in result.fetchall()]
# Get database inspector
inspector = inspect(db.engine)
if 'media_folder' not in columns:
# Add the media_folder column
db.session.execute(text('ALTER TABLE posts ADD COLUMN media_folder VARCHAR(100)'))
db.session.commit()
print('Successfully added media_folder column to posts table')
else:
print('media_folder column already exists')
# Check if is_cover column exists in post_images table
result = db.session.execute(text('PRAGMA table_info(post_images)'))
columns = [row[1] for row in result.fetchall()]
print('Starting database schema migration...\n')
if 'is_cover' not in columns:
# Add the is_cover column
db.session.execute(text('ALTER TABLE post_images ADD COLUMN is_cover BOOLEAN DEFAULT 0'))
db.session.commit()
print('Successfully added is_cover column to post_images table')
else:
print('is_cover column already exists')
# 1. Check and migrate posts table
if 'posts' in inspector.get_table_names():
columns = [col['name'] for col in inspector.get_columns('posts')]
# Check if page_views table exists
result = db.session.execute(text("SELECT name FROM sqlite_master WHERE type='table' AND name='page_views'"))
if not result.fetchone():
# Create page_views table
db.session.execute(text('''
CREATE TABLE page_views (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path VARCHAR(255) NOT NULL,
user_agent VARCHAR(500),
ip_address VARCHAR(45),
referer VARCHAR(500),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
user_id INTEGER,
post_id INTEGER,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (post_id) REFERENCES posts(id)
)
'''))
db.session.commit()
print('Successfully created page_views table')
else:
print('page_views table already exists')
if 'media_folder' not in columns:
db.session.execute(text('ALTER TABLE posts ADD COLUMN media_folder VARCHAR(100)'))
db.session.commit()
print('✓ Added media_folder column to posts table')
# Check if GPX statistics columns exist
result = db.session.execute(text('PRAGMA table_info(gpx_files)'))
columns = [row[1] for row in result.fetchall()]
# Check for other expected columns
expected = ['title', 'content', 'author_id', 'published', 'created_at']
for col in expected:
if col not in columns:
print(f'✗ WARNING: Expected column {col} not found in posts table')
new_columns = [
('total_distance', 'REAL DEFAULT 0.0'),
('elevation_gain', 'REAL DEFAULT 0.0'),
('max_elevation', 'REAL DEFAULT 0.0'),
('min_elevation', 'REAL DEFAULT 0.0'),
('total_points', 'INTEGER DEFAULT 0')
# 2. Check and migrate post_images table
if 'post_images' in inspector.get_table_names():
columns = [col['name'] for col in inspector.get_columns('post_images')]
if 'is_cover' not in columns:
db.session.execute(text('ALTER TABLE post_images ADD COLUMN is_cover BOOLEAN DEFAULT 0'))
db.session.commit()
print('✓ Added is_cover column to post_images table')
# 3. Check and migrate chat_rooms table
if 'chat_rooms' in inspector.get_table_names():
columns = [col['name'] for col in inspector.get_columns('chat_rooms')]
if 'category' not in columns:
db.session.execute(text('ALTER TABLE chat_rooms ADD COLUMN category VARCHAR(50) DEFAULT "general"'))
db.session.commit()
print('✓ Added category column to chat_rooms table')
if 'last_activity' not in columns:
db.session.execute(text('ALTER TABLE chat_rooms ADD COLUMN last_activity DATETIME DEFAULT CURRENT_TIMESTAMP'))
db.session.commit()
print('✓ Added last_activity column to chat_rooms table')
# 4. Check and add GPX statistics columns
if 'gpx_files' in inspector.get_table_names():
columns = [col['name'] for col in inspector.get_columns('gpx_files')]
gpx_columns = {
'total_distance': 'REAL DEFAULT 0.0',
'elevation_gain': 'REAL DEFAULT 0.0',
'max_elevation': 'REAL DEFAULT 0.0',
'min_elevation': 'REAL DEFAULT 0.0',
'total_points': 'INTEGER DEFAULT 0'
}
for col_name, col_type in gpx_columns.items():
if col_name not in columns:
db.session.execute(text(f'ALTER TABLE gpx_files ADD COLUMN {col_name} {col_type}'))
db.session.commit()
print(f'✓ Added {col_name} column to gpx_files table')
# 5. Verify all required tables exist
required_tables = [
'users', 'posts', 'post_images', 'gpx_files',
'comments', 'likes', 'page_views', 'chat_rooms',
'chat_messages', 'map_routes', 'password_reset_requests'
]
for column_name, column_type in new_columns:
if column_name not in columns:
db.session.execute(text(f'ALTER TABLE gpx_files ADD COLUMN {column_name} {column_type}'))
db.session.commit()
print(f'Successfully added {column_name} column to gpx_files table')
else:
print(f'{column_name} column already exists in gpx_files table')
print('Database schema is up to date')
existing_tables = inspector.get_table_names()
missing_tables = [t for t in required_tables if t not in existing_tables]
if missing_tables:
print(f'\n✗ WARNING: Missing tables: {missing_tables}')
print(' Running init-db to create missing tables...')
db.create_all()
print('✓ All missing tables created')
print('\n✓ Database migration completed successfully')
print(f'✓ Total tables: {len(existing_tables)}')
except Exception as e:
print(f'✗ Migration error: {e}')
db.session.rollback()
raise
print(f'Migration error: {e}')
db.session.rollback()