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 migrate_db CLI command for database migrations

Juan Denis236d04045826
diff --git a/backend/cli.py b/backend/cli.py
index e9cc58de..df6c0319 100644
--- a/backend/cli.py
+++ b/backend/cli.py
@@ -213,6 +213,50 @@ def init_db():
click.echo(click.style('Database initialized successfully!', fg='green'))
+def migrate_db():
+ """Apply database migrations for missing columns."""
+ app = create_app()
+ with app.app_context():
+ from sqlalchemy import text, inspect
+
+ inspector = inspect(db.engine)
+
+ # Get existing columns in applications table
+ existing_columns = [col['name'] for col in inspector.get_columns('applications')]
+
+ 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'))
+
+ if not migrations:
+ click.echo(click.style('Database is up to date. No migrations needed.', fg='green'))
+ return
+
+ click.echo(f'Found {len(migrations)} migration(s) to apply...')
+
+ for table, column, col_type in migrations:
+ try:
+ sql = f'ALTER TABLE {table} ADD COLUMN {column} {col_type}'
+ db.session.execute(text(sql))
+ click.echo(click.style(f' ✓ Added column {table}.{column}', fg='green'))
+ except Exception as e:
+ click.echo(click.style(f' ✗ Failed to add {table}.{column}: {e}', fg='red'))
+
+ db.session.commit()
+ click.echo(click.style('\nMigrations completed!', fg='green'))
+
+
@cli.command()
@click.confirmation_option(prompt='Are you sure you want to drop all tables?')
def drop_db():