From c9ee69712d24de84e9bf88ca6d32f508fd4f4613 Mon Sep 17 00:00:00 2001 From: Jens Luedicke Date: Tue, 1 Jul 2025 15:03:03 +0200 Subject: [PATCH] Fix for DB initialization. --- README.md | 4 ++-- app.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eb2d3b5..9af5d3c 100644 --- a/README.md +++ b/README.md @@ -145,11 +145,11 @@ The application provides various endpoints for different user roles: ## File Structure -- `app.py`: Main Flask application +- `app.py`: Main Flask application with integrated migration system - `models.py`: Database models and relationships - `templates/`: HTML templates for all pages - `static/`: CSS and JavaScript files -- `migrate_*.py`: Database migration scripts +- `migrate_*.py`: Legacy migration scripts (no longer needed) ## Contributing diff --git a/app.py b/app.py index 76cdd3e..0d86876 100644 --- a/app.py +++ b/app.py @@ -56,8 +56,9 @@ def run_migrations(): # Check if database exists if not os.path.exists(db_path): print("Database doesn't exist. Creating new database.") - db.create_all() - init_system_settings() + with app.app_context(): + db.create_all() + init_system_settings() return print("Running database migrations...") @@ -67,6 +68,17 @@ def run_migrations(): cursor = conn.cursor() try: + # Check if time_entry table exists first + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='time_entry'") + if not cursor.fetchone(): + print("time_entry table doesn't exist. Creating all tables...") + with app.app_context(): + db.create_all() + init_system_settings() + conn.commit() + conn.close() + return + # Migrate time_entry table cursor.execute("PRAGMA table_info(time_entry)") time_entry_columns = [column[1] for column in cursor.fetchall()]