Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Add lightweight auto-migration for missing DB columns

Juan Denisf0ccfea16071
diff --git a/backend/app/__init__.py b/backend/app/__init__.py
index 320c0a56..172ef204 100644
--- a/backend/app/__init__.py
+++ b/backend/app/__init__.py
@@ -183,6 +183,9 @@ def create_app(config_name=None):
with app.app_context():
db.create_all()
+ # Auto-migrate missing columns on existing tables
+ _auto_migrate_columns(app)
+
# Initialize default settings and migrate legacy roles
from app.services.settings_service import SettingsService
SettingsService.initialize_defaults()
@@ -219,6 +222,72 @@ def get_socketio():
return socketio
+def _auto_migrate_columns(app):
+ """Add missing columns to existing tables (lightweight auto-migration)."""
+ import logging
+ from sqlalchemy import text, inspect as sa_inspect
+
+ logger = logging.getLogger(__name__)
+
+ # Define expected columns per table: (table, column, sql_type)
+ expected_columns = [
+ # wordpress_sites table
+ ('wordpress_sites', 'environment_type', "VARCHAR(20) DEFAULT 'standalone'"),
+ ('wordpress_sites', 'multidev_branch', 'VARCHAR(200)'),
+ ('wordpress_sites', 'is_locked', 'BOOLEAN DEFAULT 0'),
+ ('wordpress_sites', 'locked_by', 'VARCHAR(100)'),
+ ('wordpress_sites', 'locked_reason', 'VARCHAR(200)'),
+ ('wordpress_sites', 'lock_expires_at', 'DATETIME'),
+ ('wordpress_sites', 'compose_project_name', 'VARCHAR(100)'),
+ ('wordpress_sites', 'container_prefix', 'VARCHAR(100)'),
+ ('wordpress_sites', 'resource_limits', 'TEXT'),
+ ('wordpress_sites', 'basic_auth_enabled', 'BOOLEAN DEFAULT 0'),
+ ('wordpress_sites', 'basic_auth_user', 'VARCHAR(100)'),
+ ('wordpress_sites', 'basic_auth_password_hash', 'VARCHAR(200)'),
+ ('wordpress_sites', 'health_status', "VARCHAR(20) DEFAULT 'unknown'"),
+ ('wordpress_sites', 'last_health_check', 'DATETIME'),
+ ('wordpress_sites', 'disk_usage_bytes', 'BIGINT DEFAULT 0'),
+ ('wordpress_sites', 'disk_usage_updated_at', 'DATETIME'),
+ ('wordpress_sites', 'auto_sync_schedule', 'VARCHAR(100)'),
+ ('wordpress_sites', 'auto_sync_enabled', 'BOOLEAN DEFAULT 0'),
+ # applications table
+ ('applications', 'private_slug', 'VARCHAR(50)'),
+ ('applications', 'private_url_enabled', 'BOOLEAN DEFAULT 0'),
+ ('applications', 'environment_type', "VARCHAR(20) DEFAULT 'standalone'"),
+ ('applications', 'linked_app_id', 'INTEGER'),
+ ('applications', 'shared_config', 'TEXT'),
+ ]
+
+ try:
+ inspector = sa_inspect(db.engine)
+ existing_tables = inspector.get_table_names()
+
+ # Group by table for efficient inspection
+ tables_checked = {}
+ applied = 0
+
+ for table, column, col_type in expected_columns:
+ if table not in existing_tables:
+ continue
+
+ if table not in tables_checked:
+ tables_checked[table] = [col['name'] for col in inspector.get_columns(table)]
+
+ if column not in tables_checked[table]:
+ try:
+ db.session.execute(text(f'ALTER TABLE {table} ADD COLUMN {column} {col_type}'))
+ applied += 1
+ logger.info(f'Auto-migrated: added {table}.{column}')
+ except Exception as e:
+ logger.warning(f'Auto-migrate failed for {table}.{column}: {e}')
+
+ if applied > 0:
+ db.session.commit()
+ logger.info(f'Auto-migration: applied {applied} column(s)')
+ except Exception as e:
+ logger.warning(f'Auto-migration check failed: {e}')
+
+
_auto_sync_thread = None
diff --git a/backend/cli.py b/backend/cli.py
index fc5b4d59..caa4a391 100644
--- a/backend/cli.py
+++ b/backend/cli.py
@@ -221,23 +221,48 @@ def migrate_db():
from sqlalchemy import text, inspect
inspector = inspect(db.engine)
+ existing_tables = inspector.get_table_names()
+
+ # Define all expected columns per table
+ expected_columns = [
+ # applications table
+ ('applications', 'private_slug', 'VARCHAR(50)'),
+ ('applications', 'private_url_enabled', 'BOOLEAN DEFAULT 0'),
+ ('applications', 'environment_type', "VARCHAR(20) DEFAULT 'standalone'"),
+ ('applications', 'linked_app_id', 'INTEGER'),
+ ('applications', 'shared_config', 'TEXT'),
+ # wordpress_sites table
+ ('wordpress_sites', 'environment_type', "VARCHAR(20) DEFAULT 'standalone'"),
+ ('wordpress_sites', 'multidev_branch', 'VARCHAR(200)'),
+ ('wordpress_sites', 'is_locked', 'BOOLEAN DEFAULT 0'),
+ ('wordpress_sites', 'locked_by', 'VARCHAR(100)'),
+ ('wordpress_sites', 'locked_reason', 'VARCHAR(200)'),
+ ('wordpress_sites', 'lock_expires_at', 'DATETIME'),
+ ('wordpress_sites', 'compose_project_name', 'VARCHAR(100)'),
+ ('wordpress_sites', 'container_prefix', 'VARCHAR(100)'),
+ ('wordpress_sites', 'resource_limits', 'TEXT'),
+ ('wordpress_sites', 'basic_auth_enabled', 'BOOLEAN DEFAULT 0'),
+ ('wordpress_sites', 'basic_auth_user', 'VARCHAR(100)'),
+ ('wordpress_sites', 'basic_auth_password_hash', 'VARCHAR(200)'),
+ ('wordpress_sites', 'health_status', "VARCHAR(20) DEFAULT 'unknown'"),
+ ('wordpress_sites', 'last_health_check', 'DATETIME'),
+ ('wordpress_sites', 'disk_usage_bytes', 'BIGINT DEFAULT 0'),
+ ('wordpress_sites', 'disk_usage_updated_at', 'DATETIME'),
+ ('wordpress_sites', 'auto_sync_schedule', 'VARCHAR(100)'),
+ ('wordpress_sites', 'auto_sync_enabled', 'BOOLEAN DEFAULT 0'),
+ ]
- # Get existing columns in applications table
- existing_columns = [col['name'] for col in inspector.get_columns('applications')]
-
+ # Check which columns are missing
+ table_columns_cache = {}
migrations = []
- # Check and add missing columns to applications table
- if 'private_slug' not in existing_columns:
- migrations.append(('applications', 'private_slug', 'VARCHAR(50)'))
- if 'private_url_enabled' not in existing_columns:
- migrations.append(('applications', 'private_url_enabled', 'BOOLEAN DEFAULT 0'))
- if 'environment_type' not in existing_columns:
- migrations.append(('applications', 'environment_type', "VARCHAR(20) DEFAULT 'standalone'"))
- if 'linked_app_id' not in existing_columns:
- migrations.append(('applications', 'linked_app_id', 'INTEGER'))
- if 'shared_config' not in existing_columns:
- migrations.append(('applications', 'shared_config', 'TEXT'))
+ for table, column, col_type in expected_columns:
+ if table not in existing_tables:
+ continue
+ if table not in table_columns_cache:
+ table_columns_cache[table] = [col['name'] for col in inspector.get_columns(table)]
+ if column not in table_columns_cache[table]:
+ migrations.append((table, column, col_type))
if not migrations:
click.echo(click.style('Database is up to date. No migrations needed.', fg='green'))