Skip to content

Programmatic method to apply migrations before server start #3873

Open

Description

@SandPod

Problem to Solve

Currently, database migrations in Serverpod can only be applied as part of the server startup process. This limits workflows where a developer needs to apply migrations before starting the server, for example data transformations that depend on the new schema.

Proposal

Introduce a dedicated method (e.g., applyMigrations()) that can be called programmatically without starting the server. This method should:

  • Use the same logic as migrations during startup.
  • Apply only pending migrations.
  • Be idempotent to prevent reapplying completed migrations.
  • Provide clear feedback on applied changes and any errors.

This dedicated method could possibly live under a maintenance namespace that allows other maintenance mode operations such as running all scheduled future calls, performing the health check, and updating the log settings.

Use Case

A developer wants to apply the latest schema changes, then run a one-off Dart script to backfill or transform data before starting the server. This avoids starting the server in a partially migrated state.

Alternatives

Manually starting the server with migrations enabled, then stopping it to perform pre-startup tasks. This is inefficient and risks unexpected behavior if the server begins handling requests during the migration process.

Additional context

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    enhancement
    area: runtime
    on Aug 11, 2025
  2. christerswahn commented on Aug 11, 2025

    @christerswahn
    Contributor

    Should this maybe be designed in conjunction with the service lifecycle interface? E.g. standardizing something along these lines:

    1. Serverpod object creation
    2. Configurations ingestion
    3. Backend services initialization (connecting to backend services like DB and Redis)
    4. Migrations
    5. Start serving requests (incl future calls)

    Today there is only 1 and 5 with user-added "stuff" in between. A well-defined pattern on how to do this, how to add new services to this process, and inject tasks pre/post these steps would be nice!

  3. moved this from Inbox 📫 to Backlog 🗂️ in Serverpod Frameworkson Aug 14, 2025
  4. moved this from Backlog 🗂️ to Planned 🔭 in Serverpod Frameworkson Oct 24, 2025
  5. added this to the Post 3.0 release milestone on Oct 24, 2025
  6. moved this from Planned 🔭 to Backlog 🗂️ in Serverpod Frameworkson Oct 24, 2025
  7. moved this from Backlog 🗂️ to Planned 🔭 in Serverpod Frameworkson Nov 14, 2025
  8. moved this from Planned 🔭 to Backlog 🗂️ in Serverpod Frameworkson Nov 14, 2025
  9. moved this from Backlog 🗂️ to Planned 🔭 in Serverpod Frameworkson Nov 14, 2025
  10. moved this from Planned 🔭 to Next Up 🎯 in Serverpod Frameworkson Dec 4, 2025
  11. moved this from Next Up 🎯 to Planned 🔭 in Serverpod Frameworkson Dec 19, 2025
  12. moved this from Planned 🔭 to Next Up 🎯 in Serverpod Frameworkson Dec 19, 2025
  13. moved this from Next Up 🎯 to Planned 🔭 in Serverpod Frameworkson Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: runtimeenhancement

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions