|  | 
| 24 | 24 | 
 | 
| 25 | 25 | app = Flask(__name__) | 
| 26 | 26 | app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production') | 
| 27 |  | -app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data/github_backup.db') | 
|  | 27 | +app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:////app/data/github_backup.db') | 
| 28 | 28 | app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | 
| 29 | 29 | 
 | 
|  | 30 | +# Diagnostic logging for DB path | 
|  | 31 | +db_uri = app.config['SQLALCHEMY_DATABASE_URI'] | 
|  | 32 | +logger.info(f"Configured DB URI: {db_uri}") | 
|  | 33 | +if db_uri.startswith('sqlite:///') or db_uri.startswith('sqlite:////'): | 
|  | 34 | +    # Normalize both relative and absolute sqlite URIs | 
|  | 35 | +    normalized = db_uri.replace('sqlite:////', '/').replace('sqlite:///', '') | 
|  | 36 | +    # If we replaced absolute variant, ensure leading slash retained | 
|  | 37 | +    if db_uri.startswith('sqlite:////'): | 
|  | 38 | +        sqlite_file = '/' + normalized.lstrip('/') | 
|  | 39 | +    else: | 
|  | 40 | +        sqlite_file = os.path.abspath(normalized) | 
|  | 41 | +    parent = os.path.dirname(sqlite_file) | 
|  | 42 | +    try: | 
|  | 43 | +        os.makedirs(parent, exist_ok=True) | 
|  | 44 | +        stat_parent = os.stat(parent) | 
|  | 45 | +        logger.info(f"SQLite file target: {sqlite_file} (parent exists, perms {oct(stat_parent.st_mode)[-3:]})") | 
|  | 46 | +    except Exception as e: | 
|  | 47 | +        logger.error(f"Failed ensuring SQLite directory {parent}: {e}") | 
|  | 48 | + | 
| 30 | 49 | # Initialize extensions | 
| 31 | 50 | db.init_app(app) | 
|  | 51 | + | 
|  | 52 | +# Immediate connectivity test (runs once at startup) | 
|  | 53 | +from sqlalchemy import text | 
|  | 54 | +with app.app_context(): | 
|  | 55 | +    try: | 
|  | 56 | +        db.session.execute(text('SELECT 1')) | 
|  | 57 | +        logger.info('Initial DB connectivity test succeeded.') | 
|  | 58 | +    except Exception as e: | 
|  | 59 | +        logger.error(f'Initial DB connectivity test failed: {e}') | 
| 32 | 60 | login_manager = LoginManager() | 
| 33 | 61 | login_manager.init_app(app) | 
| 34 | 62 | login_manager.login_view = 'login' | 
|  | 
0 commit comments