Skip to content

feat: Apply migrations directly via cli (no server start) - #5028

Merged
nielsenko merged 21 commits into
serverpod:mainfrom
nielsenko:exp-cli-migrations
May 4, 2026
Merged

nielsenko merged 21 commits into
serverpod:mainfrom
nielsenko:exp-cli-migrations

Conversation

@nielsenko

@nielsenko nielsenko commented May 1, 2026 •

Copy link
Copy Markdown
Collaborator

This PR allows migrations to be applied by cli without restarting pod. This plays well together with the VmServiceProxy (from #5022) as it avoids detaching any potential debugger when applying migration from TUI (or if an agent does it via the MCP).

Also fixes a long standing request.

Fixes: #3119

@nielsenko nielsenko self-assigned this May 1, 2026
@nielsenko
nielsenko force-pushed the exp-cli-migrations branch 3 times, most recently from 1d5d21a to 37e2bd9 Compare May 3, 2026 08:17
nielsenko added 11 commits May 3, 2026 10:25
…w SQL

Drops the abstract loadInstalledVersions/loadInstalledRepairMigration methods that previously required the server-package's generated DatabaseMigrationVersion model. The version table schema (serverpod_migrations: id, module, version, timestamp) is stable so querying it via raw SQL is safe and unlocks running migrations from contexts that don't have access to the generated server runtime (e.g. the serverpod CLI driving migrations out-of-pod).

ServerMigrationManager keeps its constructor for source compat but loses the now-unused overrides.
…button

Adds a CLI-side migration runner that connects to the project's
database using the same config + passwords the pod uses, then runs
MigrationManager.migrateToLatest against it - no pod restart, no
generated server runtime needed.

- lib/src/migrations/cli_migration_runner.dart: helper that builds a
  PoolManager from ServerpodConfig.load and constructs a tiny
  DatabaseSession + SerializationManagerServer stub for migration use.
- lib/src/commands/migrate.dart: 'serverpod migrate' subcommand.
- start.dart's WatchSession now wires applyMigrationsAction to the
  helper. The TUI 'Apply Migration' button runs migrations in-place;
  the pod stays running and hot reload covers any model code changes.
- The legacy pod-restart path is preserved as a fallback for sessions
  that don't supply applyMigrationsAction (--no-fes, anything else
  that wants the old behaviour).
@nielsenko
nielsenko force-pushed the exp-cli-migrations branch from 37e2bd9 to a0a7291 Compare May 3, 2026 08:31
@nielsenko nielsenko changed the title feat: Cli migrations (EXPERIMENT) feat: Apply migrations directly via cli (no server start) May 4, 2026
@nielsenko
nielsenko marked this pull request as ready for review May 4, 2026 10:11

@marcelomendoncasoares marcelomendoncasoares left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few suggestions for improvement. On the integrity check, we can spin to fix as a follow-up, as discussed.

nielsenko added 4 commits May 4, 2026 18:04
- Make applyMigrationsAction required on WatchSession.
- Drop _applyMigrationByRestart and the extraArgs hook on ServerProcessFactory.
- Update applyingMigration enum + applyMigration dartdoc.
nielsenko added 2 commits May 4, 2026 19:23
Route ops through _chain() that hands failure to the caller while the queue stays runnable.

@marcelomendoncasoares marcelomendoncasoares left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

@nielsenko
nielsenko merged commit 9aa952b into serverpod:main May 4, 2026
83 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The serverpod start command should have --watch and --apply-migrations by default Apply DB migration via the serverpod CLI command

2 participants