From ca8e70781c5decabac602195b78325cbd0c08de1 Mon Sep 17 00:00:00 2001 From: SERTY Date: Wed, 30 Jul 2025 18:55:23 +0300 Subject: [PATCH] fix empty base on 1st start --- app.py | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index 76e6b09..697b6cb 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import os from flask import Flask, session, request +from sqlalchemy import inspect from dotenv import load_dotenv # 1. Загрузка переменных окружения - в самом верху @@ -62,26 +63,29 @@ def create_app(): login_manager.login_message_category = "info" with app.app_context(): - from models import User, UserConfig - all_configs = UserConfig.query.all() - for config in all_configs: - user_id = config.user_id - mappings = config.mappings - for sheer_title, params in mappings.items(): - cron_schedule = params.get('schedule_cron') - if cron_schedule: - job_id = f"user_{user_id}_sheet_{sheer_title}" - try: - scheduler.add_job( - id=job_id, - func=execute_olap_export, - trigger='cron', - args=[user_id, sheer_title], - **_parse_cron_string(cron_schedule) - ) - app.logger.info(f"Job {job_id} loaded on startup.") - except Exception as e: - app.logger.error(f"Failed to load job {job_id}: {e}") + if inspect(db.engine).has_table('user_config'): + from models import User, UserConfig + all_configs = UserConfig.query.all() + for config in all_configs: + user_id = config.user_id + mappings = config.mappings + for sheer_title, params in mappings.items(): + cron_schedule = params.get('schedule_cron') + if cron_schedule: + job_id = f"user_{user_id}_sheet_{sheer_title}" + try: + scheduler.add_job( + id=job_id, + func=execute_olap_export, + trigger='cron', + args=[user_id, sheer_title], + **_parse_cron_string(cron_schedule) + ) + app.logger.info(f"Job {job_id} loaded on startup.") + except Exception as e: + app.logger.error(f"Failed to load job {job_id}: {e}") + else: + app.logger.warning("Database tables not found. Skipping job loading on startup. Run 'flask init-db' to create the tables.") scheduler.start() # --- Регистрация команд CLI ---