Skip to content

Do not explicitly exit the serverpod process after applying migrations #3959

Open

Description

@Isakdl

Problem to Solve

When you run the server with the flags dart bin/main.dart --apply-migrations --role=maintenance the process will be killed with a call to exit.

This makes it impossible to run in this mode within a dart script that is supposed to execute other code after the migration is applied, as the process is killed as soon as the migration has completed.

Proposal

Do not make a call to exit but let the process die when it reaches the end of the main method.

Use Case

I have a stand alone migration script file where I want to apply the migration and then have custom logic after the migration is applied.

Alternatives

Wrap the apply migration code and instance of Serverpod in a dart process or split the code into multiple dart files and call them one after the other.

Additional context

This is the code that is causing this behavior inside server/serverpod.dart

if (config.role == ServerpodRole.maintenance && appliedMigrations) {
    logVerbose('Finished applying database migrations.');
    throw ExitException(_exitCode);
}

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
    on Sep 4, 2025
  2. marcelomendoncasoares commented on Sep 4, 2025

    @marcelomendoncasoares
    Collaborator

    A discussed approach for fixing this is to move all exit calls to the generated bin/main.dart instead of calling them directly on ExitException catches like this one:

    } on ExitException catch (e) {
    if (e.message.isNotEmpty) {
    stderr.writeln(e.message);
    }
    exit(e.exitCode);
    } catch (e, stackTrace) {

    This would be a breaking change, but we can instruct users on the migration guide to just add the try/catch block on their already existing main.dart file.

    A more advance and complete approach that also addresses #3873 is to add database startup hooks (onOpen, beforeMigration, afterMigration, etc) to allow hooking custom code.

  3. moved this from Inbox 📫 to Backlog 🗂️ in Serverpod Frameworkson Sep 4, 2025
  4. added
    area: database
    stake: cloud
    on Sep 4, 2025
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: databaseenhancementstake: cloud

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions