|
29 | 29 | from flask_jwt_extended.exceptions import JWTDecodeError, NoAuthorizationError |
30 | 30 | from dotenv import load_dotenv |
31 | 31 | import requests |
32 | | -from sqlalchemy import text, inspect |
33 | | -from sqlalchemy.exc import SQLAlchemyError, ProgrammingError, OperationalError |
| 32 | +from sqlalchemy.exc import SQLAlchemyError |
34 | 33 |
|
35 | 34 | # Local imports |
36 | 35 | from app.database import db |
@@ -743,70 +742,14 @@ def internal_error(_error): |
743 | 742 | # APPLICATION STARTUP |
744 | 743 | # ============================================================================= |
745 | 744 |
|
746 | | -def migrate_database(): |
747 | | - """ |
748 | | - Run database migrations to add new columns if they don't exist. |
749 | | - This ensures the database schema matches the current model definitions. |
750 | | - """ |
751 | | - try: |
752 | | - # Get table inspector to check existing columns |
753 | | - inspector = inspect(db.engine) |
754 | | - columns = [col['name'] for col in inspector.get_columns('submissions')] |
755 | | - |
756 | | - # Check which columns need to be added |
757 | | - columns_to_add = [] |
758 | | - if 'article_author' not in columns: |
759 | | - columns_to_add.append(('article_author', 'VARCHAR(200) NULL')) |
760 | | - if 'article_created_at' not in columns: |
761 | | - columns_to_add.append(('article_created_at', 'VARCHAR(50) NULL')) |
762 | | - if 'article_word_count' not in columns: |
763 | | - columns_to_add.append(('article_word_count', 'INT NULL')) |
764 | | - if 'article_page_id' not in columns: |
765 | | - columns_to_add.append(('article_page_id', 'VARCHAR(50) NULL')) |
766 | | - |
767 | | - # Add missing columns |
768 | | - if columns_to_add: |
769 | | - print("📦 Running database migration: Adding article metadata columns...") |
770 | | - for col_name, col_type in columns_to_add: |
771 | | - try: |
772 | | - db.session.execute( |
773 | | - text(f"ALTER TABLE submissions ADD COLUMN {col_name} {col_type}") |
774 | | - ) |
775 | | - print(f" ✓ Added column: {col_name}") |
776 | | - except (ProgrammingError, OperationalError) as error: |
777 | | - # Column might already exist or there's a SQL/database error |
778 | | - print(f" ⚠ Could not add column {col_name}: {error}") |
779 | | - |
780 | | - db.session.commit() |
781 | | - print("✓ Database migration completed") |
782 | | - else: |
783 | | - print("✓ Database schema is up to date") |
784 | | - |
785 | | - except (OperationalError, SQLAlchemyError) as error: |
786 | | - # If table doesn't exist yet, db.create_all() will handle it |
787 | | - # Or if there's a database connection/query error |
788 | | - print(f"⚠ Migration check skipped (table may not exist yet): {error}") |
789 | | - db.session.rollback() |
790 | | - |
791 | 745 | if __name__ == '__main__': |
792 | | - # Main application entry point. |
793 | | - # This section runs when the script is executed directly (not imported). |
794 | | - # It initializes the database tables and starts the Flask development server. |
795 | | - |
796 | | - # Initialize database tables |
797 | | - # This creates all tables defined in the models if they don't exist |
798 | | - with app.app_context(): |
799 | | - db.create_all() |
800 | | - print("✓ Database tables initialized successfully") |
801 | | - |
802 | | - # Run migrations to add new columns to existing tables |
803 | | - migrate_database() |
804 | | - |
805 | | - # Start the Flask development server |
| 746 | + # This file can be run directly, but main.py is the recommended entry point. |
| 747 | + # Database migrations are handled by Alembic - run 'alembic upgrade head' before starting. |
806 | 748 | # Debug mode is enabled for development (disable in production) |
807 | 749 | print("🚀 Starting WikiContest API server...") |
808 | 750 | print("📡 Server will be available at: http://localhost:5000") |
809 | 751 | print("🔧 Debug mode: ENABLED") |
| 752 | + print("ℹ Note: Use Alembic for database migrations (alembic upgrade head)") |
810 | 753 |
|
811 | 754 | app.run( |
812 | 755 | debug=True, # Enable debug mode for development |
|
0 commit comments