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.

fix(migrate): stop burying Continue under every migration ever written

Juan Denis69b98f7149b8
diff --git a/backend/app/services/migration_service.py b/backend/app/services/migration_service.py
index 4ddf07a5..a4ed6eb1 100644
--- a/backend/app/services/migration_service.py
+++ b/backend/app/services/migration_service.py
@@ -21,6 +21,11 @@ class MigrationService:
_current_revision = None
_head_revision = None
_pending_migrations = []
+ # True when the database is stamped at a revision this codebase no longer
+ # contains (a reverted branch, a downgraded install). Upgrading is impossible
+ # until it's stamped back to a known revision, so the UI must say that rather
+ # than offer a Continue button that cannot work.
+ _orphaned_revision = False
@classmethod
def _get_alembic_config(cls, app):
@@ -139,8 +144,29 @@ class MigrationService:
cls._current_revision = current_heads[0] if current_heads else None
if cls._current_revision != head:
- # Calculate pending migrations
+ # Is the stamped revision one this codebase still knows about?
+ # A branch that added a migration and was then reverted leaves the
+ # database stamped at a revision whose file is gone. The walk below
+ # breaks on `current_revision`, so an unknown one never matches and
+ # every revision in history gets reported as "pending" — which is
+ # both wrong and alarming (and `upgrade` would fail anyway with
+ # "Can't locate revision"). Detect it and say so instead.
+ known_revisions = {rev.revision for rev in script.walk_revisions()}
+ cls._orphaned_revision = bool(
+ cls._current_revision and cls._current_revision not in known_revisions
+ )
+
cls._pending_migrations = []
+ if cls._orphaned_revision:
+ logger.warning(
+ f'Database is stamped at unknown revision '
+ f'{cls._current_revision!r} (head={head}). It is not in this '
+ f'codebase — the schema cannot be upgraded until it is '
+ f'stamped back to a known revision.'
+ )
+ cls._needs_migration = True
+ return
+
for rev in script.walk_revisions():
if cls._current_revision and rev.revision == cls._current_revision:
break
@@ -184,11 +210,14 @@ class MigrationService:
and cls._current_revision != cls._head_revision
)
return {
- 'needs_migration': needs,
+ 'needs_migration': needs or cls._orphaned_revision,
'current_revision': cls._current_revision,
'head_revision': cls._head_revision,
'pending_count': len(cls._pending_migrations),
'pending_migrations': cls._pending_migrations,
+ # When true, `pending_migrations` is empty because it is unknowable,
+ # not because the database is up to date.
+ 'orphaned_revision': cls._orphaned_revision,
}
@classmethod
diff --git a/frontend/src/pages/DatabaseMigration.jsx b/frontend/src/pages/DatabaseMigration.jsx
index b08b8afd..8c712f93 100644
--- a/frontend/src/pages/DatabaseMigration.jsx
+++ b/frontend/src/pages/DatabaseMigration.jsx
@@ -12,6 +12,8 @@ import { Input } from '@/components/ui/input';
const TOTAL_STEPS = 4;
const STEP_TITLES = ['Overview', 'Backup', 'Apply', 'Done'];
+// How many pending migrations to show before collapsing the rest behind a toggle.
+const MIGRATION_PREVIEW_COUNT = 4;
const DatabaseMigration = () => {
const { isAuthenticated, isAdmin, needsMigration, migrationInfo, refreshSetupStatus, login } = useAuth();
@@ -23,6 +25,7 @@ const DatabaseMigration = () => {
const [applyLoading, setApplyLoading] = useState(false);
const [applyError, setApplyError] = useState(null);
const [migrationStatus, setMigrationStatus] = useState(null);
+ const [showAllMigrations, setShowAllMigrations] = useState(false);
// Login form state (for unauthenticated users)
const [loginEmail, setLoginEmail] = useState('');
@@ -121,6 +124,14 @@ const DatabaseMigration = () => {
const status = migrationStatus || migrationInfo || {};
const pendingCount = status.pending_count || 0;
const pendingMigrations = status.pending_migrations || [];
+ // A long-idle install can be dozens of migrations behind. Listing all of them
+ // buried the Continue button under a page of scrolling, so show the newest
+ // few — the ones that say what you're actually getting — and let the rest be
+ // opened deliberately.
+ const hiddenCount = Math.max(0, pendingMigrations.length - MIGRATION_PREVIEW_COUNT);
+ const shownMigrations = showAllMigrations
+ ? pendingMigrations
+ : pendingMigrations.slice(-MIGRATION_PREVIEW_COUNT);
return (
<div className="setup-wizard migration-wizard">
@@ -161,16 +172,55 @@ const DatabaseMigration = () => {
</div>
</div>
+ {status.orphaned_revision && (
+ <div className="wizard-info-banner wizard-info-banner--warn">
+ <AlertTriangle size={20} className="wizard-info-icon" />
+ <p>
+ This database is stamped at <code>{status.current_revision}</code>,
+ which does not exist in this build — usually a branch that added a
+ migration and was then reverted. The schema cannot be upgraded
+ until it is stamped back to a known revision:
+ {' '}<code>flask db stamp {status.head_revision || 'head'}</code>
+ </p>
+ </div>
+ )}
+
{pendingMigrations.length > 0 && (
<div className="migration-list">
- <div className="migration-list-title">Changes to apply:</div>
- {pendingMigrations.map((m, i) => (
- <div key={i} className="migration-list-item">
- <Database size={14} />
- <code>{m.revision.substring(0, 12)}</code>
- <span>{m.description || 'Schema update'}</span>
+ <div className="migration-list-head">
+ <span className="migration-list-title">
+ {shownMigrations.length === pendingMigrations.length
+ ? 'Changes to apply'
+ : `Latest ${shownMigrations.length} of ${pendingMigrations.length} changes`}
+ </span>
+ {pendingMigrations.length > MIGRATION_PREVIEW_COUNT && (
+ <button
+ type="button"
+ className="migration-list-toggle"
+ onClick={() => setShowAllMigrations(v => !v)}
+ >
+ {showAllMigrations
+ ? 'Show less'
+ : `Show all ${pendingMigrations.length}`}
+ </button>
+ )}
+ </div>
+ {/* Scrolls within itself so Continue stays reachable no matter
+ how far behind the database is. */}
+ <div className={`migration-list-items${showAllMigrations ? ' is-expanded' : ''}`}>
+ {shownMigrations.map((m, i) => (
+ <div key={m.revision || i} className="migration-list-item">
+ <Database size={14} />
+ <code>{m.revision.substring(0, 12)}</code>
+ <span>{m.description || 'Schema update'}</span>
+ </div>
+ ))}
+ </div>
+ {!showAllMigrations && hiddenCount > 0 && (
+ <div className="migration-list-more">
+ + {hiddenCount} earlier {hiddenCount === 1 ? 'update' : 'updates'}, applied first
</div>
- ))}
+ )}
</div>
)}
@@ -217,7 +267,9 @@ const DatabaseMigration = () => {
<Button
className="btn-wizard-next"
onClick={() => setCurrentStep(2)}
- disabled={!isAuthenticated || !isAdmin}
+ // An orphaned revision cannot be upgraded past — offering
+ // Continue would just fail at "Can't locate revision".
+ disabled={!isAuthenticated || !isAdmin || status.orphaned_revision}
>
Continue <ArrowRight size={16} />
</Button>
diff --git a/frontend/src/styles/pages/_migration-wizard.scss b/frontend/src/styles/pages/_migration-wizard.scss
index bba61980..7712c0f7 100644
--- a/frontend/src/styles/pages/_migration-wizard.scss
+++ b/frontend/src/styles/pages/_migration-wizard.scss
@@ -13,15 +13,15 @@
background: $bg-elevated;
border: 1px solid $border-default;
border-radius: $radius-xl;
- padding: $space-5;
- margin-bottom: $space-6;
+ padding: $space-4;
+ margin-bottom: $space-4;
}
.migration-status-row {
display: flex;
justify-content: space-between;
align-items: center;
- padding: $space-2 0;
+ padding: $space-1 0;
&:not(:last-child) {
border-bottom: 1px solid $border-subtle;
@@ -45,25 +45,79 @@
}
}
+// Orphaned-revision warning: the database is stamped at a revision this build
+// doesn't contain, so there is nothing to upgrade *to* and Continue is disabled.
+.wizard-info-banner--warn {
+ background: $warning-bg;
+ border: 1px solid $warning-border;
+
+ .wizard-info-icon { color: $warning; }
+
+ code {
+ font-family: $font-family-mono;
+ font-size: $font-size-xs;
+ background: $bg-secondary;
+ padding: 1px 6px;
+ border-radius: $radius-sm;
+ white-space: nowrap;
+ }
+}
+
// --------------------------------------------
// Migration list (pending changes)
// --------------------------------------------
.migration-list {
- margin-bottom: $space-6;
+ margin-bottom: $space-4;
+}
+
+// Title on the left, the show-all toggle on the right.
+.migration-list-head {
+ display: flex;
+ align-items: baseline;
+ justify-content: space-between;
+ gap: $space-3;
+ margin-bottom: $space-2;
}
.migration-list-title {
font-size: $font-size-sm;
font-weight: $font-weight-semibold;
color: $text-secondary;
- margin-bottom: $space-3;
+}
+
+.migration-list-toggle {
+ background: none;
+ border: none;
+ padding: 0;
+ cursor: pointer;
+ font-size: $font-size-xs;
+ font-weight: $font-weight-medium;
+ color: $accent-primary;
+
+ &:hover { text-decoration: underline; }
+}
+
+// Expanded, the list scrolls inside itself: an install 80 migrations behind
+// must never push the Continue button off the page.
+.migration-list-items {
+ &.is-expanded {
+ max-height: 260px;
+ overflow-y: auto;
+ padding-right: $space-2;
+ }
+}
+
+.migration-list-more {
+ margin-top: $space-2;
+ font-size: $font-size-xs;
+ color: $text-tertiary;
}
.migration-list-item {
display: flex;
align-items: center;
gap: $space-3;
- padding: $space-3 $space-4;
+ padding: $space-2 $space-3;
background: $bg-elevated;
border: 1px solid $border-subtle;
border-radius: $radius-lg;