fix: Try to fix db
All checks were successful
Build Docker / Build Docker (push) Successful in 5m2s

This commit is contained in:
2025-10-22 12:30:45 +11:00
parent eaf416316a
commit d1f35096e5
3 changed files with 60 additions and 12 deletions

10
migrate_db.py Normal file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env python3
from website import create_app
from website.migrations import add_missing_columns_to_oauth2_code
if __name__ == '__main__':
app = create_app()
print("Running database migration...")
add_missing_columns_to_oauth2_code(app)
print("Migration completed.")

View File

@@ -5,12 +5,13 @@ def create_app():
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS']) app.config.from_object(os.environ['APP_SETTINGS'])
# Import models after app creation but before init_db with app.app_context():
from .models import db, User # Run migrations first, before any database operations
db.init_app(app)
# Run migrations to fix schema issues
from .migrations import add_missing_columns_to_oauth2_code from .migrations import add_missing_columns_to_oauth2_code
add_missing_columns_to_oauth2_code(app) add_missing_columns_to_oauth2_code(app)
# Import models after migration but before init_db
from .models import db
db.create_all()
return app return app

View File

@@ -1,11 +1,14 @@
import os
import sqlite3 import sqlite3
from sqlalchemy import inspect from sqlalchemy import inspect
from flask import current_app from flask import current_app
import logging
def add_missing_columns_to_oauth2_code(app): def add_missing_columns_to_oauth2_code(app):
""" """
Check and add missing columns to oauth2_code table Check and add missing columns to oauth2_code table
""" """
print("Starting database migration check...")
with app.app_context(): with app.app_context():
from website.models import db from website.models import db
@@ -15,10 +18,12 @@ def add_missing_columns_to_oauth2_code(app):
# Check if oauth2_code table exists # Check if oauth2_code table exists
if 'oauth2_code' not in inspector.get_table_names(): if 'oauth2_code' not in inspector.get_table_names():
print("oauth2_code table doesn't exist yet, skipping migration")
return # Table doesn't exist yet return # Table doesn't exist yet
# Get existing columns # Get existing columns
columns = [column['name'] for column in inspector.get_columns('oauth2_code')] columns = [column['name'] for column in inspector.get_columns('oauth2_code')]
print(f"Existing columns in oauth2_code: {columns}")
# Define columns that should be added if missing # Define columns that should be added if missing
missing_columns = { missing_columns = {
@@ -32,23 +37,55 @@ def add_missing_columns_to_oauth2_code(app):
columns_to_add = {col: dtype for col, dtype in missing_columns.items() if col not in columns} columns_to_add = {col: dtype for col, dtype in missing_columns.items() if col not in columns}
if not columns_to_add: if not columns_to_add:
print("No columns need to be added, schema is up to date")
return # No columns need to be added return # No columns need to be added
print(f"Columns to add: {columns_to_add}")
# Connect directly to SQLite to add columns # Connect directly to SQLite to add columns
try: try:
db_path = current_app.config.get('SQLALCHEMY_DATABASE_URI').replace('sqlite:///', '') # Get database URI from app config
db_uri = current_app.config.get('SQLALCHEMY_DATABASE_URI')
print(f"Database URI: {db_uri}")
# Handle both relative and absolute paths
if db_uri.startswith('sqlite:///'):
# Relative path
if db_uri.startswith('sqlite:////'):
# Absolute path
db_path = db_uri.replace('sqlite:////', '/')
else:
# Relative path - may need to be adjusted for Docker
db_path = os.path.join(app.root_path, '..', db_uri.replace('sqlite:///', ''))
else:
# Memory or other type of database
print(f"Unsupported database type: {db_uri}")
return
print(f"Attempting to connect to database at: {db_path}")
# Ensure directory exists
db_dir = os.path.dirname(db_path)
if not os.path.exists(db_dir):
print(f"Database directory doesn't exist: {db_dir}")
# Connect to database
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
for column, dtype in columns_to_add.items(): for column, dtype in columns_to_add.items():
try: try:
cursor.execute(f'ALTER TABLE oauth2_code ADD COLUMN {column} {dtype};') sql = f'ALTER TABLE oauth2_code ADD COLUMN {column} {dtype};'
print(f"Added missing column '{column}' to oauth2_code table") print(f"Executing SQL: {sql}")
cursor.execute(sql)
print(f"Successfully added column '{column}' to oauth2_code table")
except sqlite3.OperationalError as e: except sqlite3.OperationalError as e:
# Column might have been added in a concurrent process print(f"Error adding column '{column}': {str(e)}")
print(f"Note: {str(e)}")
conn.commit() conn.commit()
conn.close() conn.close()
print("Migration completed successfully")
except Exception as e: except Exception as e:
print(f"Error during migration: {str(e)}") print(f"Error during migration: {str(e)}")
import traceback
traceback.print_exc()