Skip to content

Append migration command #4503

Open

Description

@jwelmac

Problem to Solve

When multiple developers work on separate feature branches that each contain database migrations, merging these branches creates migration ordering conflicts. The migration_registry.txt file detects collisions, but developers currently have no automated way to resolve them. This forces teams to manually identify, delete, and recreate migrations after merge conflicts—a tedious and error-prone process that slows development velocity.

The core issue is that migrations must be applied in strict chronological order to maintain database integrity. When branches diverge and contain independent migrations, simply merging the branches can result in an incorrect migration sequence that isn't automatically detected until runtime.

Proposal

Introduce a new CLI command serverpod append-migration that automates the resolution of migration conflicts after branch merges. This command will:

  1. Parse merge conflict markers in migration_registry.txt to identify which migrations came from each branch (between <<<<<<< HEAD and ======= is our branch's migrations, between ======= and >>>>>>> is the incoming branch's migrations)

  2. Identify our local migrations to delete by extracting our migration IDs from the conflict markers - these will be replaced by a new unified migration that applies our model changes on top of the new base state

  3. Generate a unified migration that captures the complete database state transformation from the latest base migration to the current desired state, appending it with a new timestamp after the latest registry entry

Command usage:

serverpod append-migration

Example workflow:

Scenario: Developer A merged migrations ending at 20231115_123456, Developer B worked on a branch 
with migrations 20231116_234567 and 20231117_345678 based on an older state.

Before append-migration (after merge conflict):
- migration_registry.txt has merge conflict markers
- Folders on disk: 20231115_123456/ (from A), 20231116_234567/ (ours), 20231117_345678/ (ours)

After append-migration:
- migration_registry.txt conflict resolved to: 20231115_123456, 20231118_999999
- Folders on disk: 20231115_123456/, 20231118_999999/
- Our migrations (20231116_234567/, 20231117_345678/) deleted and backed up
- 20231118_999999/ contains migration representing: applying our model changes on top of A's new base state

Use Case

Scenario: A team of 3 developers works on independent features on separate branches over a 2-week sprint. Developer A completes a user authentication feature with 2 migrations. Developer B completes a reporting feature with 3 migrations. Developer C completes an analytics feature with 1 migration. When the team merges these branches into main:

  1. Developer A's migrations land first (timestamps: 20231115_xxx, 20231115_yyy)
  2. Developer B's branch, created from main before A's merge, has migrations with timestamps (20231114_aaa, 20231114_bbb, 20231114_ccc) that are now "out of order"
  3. Developer C's branch has the same problem with conflicting timestamps

Currently, each developer must manually:

  • Identify which migrations are "theirs"
  • Delete those migration folders
  • Create a new migration from scratch capturing all their changes
  • Test it thoroughly before re-pushing

With append-migration, this process becomes:

# After merging main into feature-branch (which creates a conflict in migration_registry.txt)
git merge origin/main
serverpod append-migration
# Done—your local migrations deleted, new unified migration created based on your models on the new base state, ready to push

This is especially valuable for teams that:

  • Work in short-lived feature branches with frequent integrations
  • Have multiple developers making database changes simultaneously
  • Need to maintain a clean, linear migration history, while handling merge conflicts

Alternatives

  1. Manual resolution (current approach): Developers manually delete and recreate migrations. This works but is time-consuming and error-prone, especially at scale.

  2. Strict sequential branch policy: Require all developers to base feature branches on a synchronized state and merge in a specific order. This severely limits parallelism and isn't practical, especially for larger teams.

The proposed append-migration command is superior because it's:

  • Lightweight and non-invasive
  • Maintains the clean, linear migration history principle
  • Aligns with Serverpod's documented best practice
  • Minimal additional complexity in the CLI tool

Additional context

This feature is directly requested in the discussion (#4299) by community member (@cotw-fabier) who has encountered this issue in production. @SandPod confirmed that this is a known pain point and indicated willingness to support a CLI command to address it.

"When a collision is detected you should remove all migrations you have created in the branch and create a new one to correctly append to the history.

Removing all migrations that are not part of the migration registry is currently a manual task, but I think we could introduce a command for that in the cli."

How experienced are you with this library?

Intermediate - Familiar with the basics or have used it in a few projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    enhancement
    on Dec 24, 2025
  2. jwelmac commented on Dec 24, 2025

    @jwelmac
    ContributorAuthor

    Thinking through this in more detail I am considering renaming the command to rebase-migration.

    The reason for this change is because of another use case I thought of in the developer workflow.

    As @SandPod stated in #4299

    Ideally a commit to main should contain a single migration. This allows you to test the effects of that migration in isolation before rolling the database forward further.

    So not only could it be used as part of the workflow locally detecting and resolving merge conflicts, but it could also be used without conflicts locally or in a GitHub action.

    Rationale

    Here is the scenario.
    Developer A creates a feature and during the process they created 2 migrations for the incremental changes to complete the feature. According to Serverpod best practices this should be merged in as a single migration. So before the developer marks the PR as ready they would need to ensure that they remove the previous migrations and create a new single migration. This is currently a manual task the developer would need to do.

    By using rebase-migration it would rebase from either a provided migration (e.g. 20251224xxxxxxxxx) or by default the last migration in the base branch (default: main) of your repository (which can be specified). This would then squash all migrations local to the branch to a single migration, hence making the branch compliant.

    Check flag

    The command could also take a flag (e.g. --check) that will confirm that the current branch has only a single migration since the last migration in the base branch, or a provided migration. It would then fail with a non-zero exit number if there are more than 1 migrations if the branch is not compliant.

    Usage

    The check flag could be run for pull requests that are not drafts to ensure that the branch is ready to be merged.
    In that way, there would be checks and balances for the developer who may forget to ensure there is only a single migration prior to merging their feature branch.

    # Scenario 1: Merge conflict (auto-detected and resolved)
    serverpod rebase-migration
    # If conflict markers exist: detects and rebases
    # If no conflicts: looks up origin/main and rebases against it
    
    # Scenario 2: Merge conflict with merge warnings or information that may be destroyed when creating merge
    serverpod rebase-migration --force
    # Merge commit created that may contain changes that may result in information being lost
    
    # Scenario 3: Explicit base migration
    serverpod rebase-migration --onto 20251224123456789
    # Rebases all local migrations onto the specified migration
    
    # Scenario 4: Check mode (for CI/CD)
    serverpod rebase-migration --check
    # Validates that only 1 migration exists since origin/main's latest
    # (or since --origin/--from if specified)
  3. jwelmac commented on Dec 29, 2025

    @jwelmac
    ContributorAuthor

    Implementation started.

    I decided to remove the onto-branch option as the time and complexity to implement it reliably outweighs the benefit of parsing the last migration from the desired branch and passing to the command if executing in CI.

    Next step after this I am thinking is to create a Github action that would retrieve the migration id and pass to the rebase-migration --check command to verify that only a single additional migration is to be merged.

  4. marcelomendoncasoares commented on Jan 2, 2026

    @marcelomendoncasoares
    Collaborator

    Hi @jwelmac! Thanks for this proposal! This is indeed an area that we want to improve the developer experience. We are right now writing an RFC that will soon be published including this pain point and other proposals to greatly enhance the UX when working with migrations. It will be very good to have you onboard when we have the RFC if you are willing to contribute more to this area!

    I saw you already started some work on your proposal, but we do have a few concerns about the coupling of git conflict resolution inside the serverpod_cli. Since there are so many ways of using Git, this would represent high complexity while probably covering only a few opinionated usage patterns.

    Take the challenge for implementing the check flag you have proposed as an example:

    The command could also take a flag (e.g. --check) that will confirm that the current branch has only a single migration since the last migration in the base branch, or a provided migration. It would then fail with a non-zero exit number if there are more than 1 migrations if the branch is not compliant.

    The way the detection of the "last migration in the base branch" is implemented in the PR - on the getLastIncomingMigration method - will only work correctly when the feature branch is the current branch (HEAD) and main is being merged into it. But if users calls rebase-migration from the middle of a git rebase (when current and incoming are inverted), it would silently produce wrong results.

    This is a simple example, but highlights the complexity of bundling git conflict resolution inside the CLI. Although building the happy path for merging feature-braches might be possible - and you are probably very close to complete -, there would be several unexpected ways to break the command. Or, even worst, make it run and produce wrong results, generating second order problems for users when trying to initialize their servers. Guarding against this would be very hard and could be too consuming from a framework maintenance perspective.

    From our first internal discussions around this RFC, we leaned more towards having a clean command that will serve to remove all migrations that are not in the migrations-registry.txt and leave the conflict resolution for the users. Then, after manually solving the conflicts on the migrations-registry.txt file, users can call an alias that would be the equivalent for clean && create, resulting in the same effect you proposed with the append-migration - but without the complexity of resolving the conflicts. This strategy also keeps users accountable by the migrations that are generated, without having to check the result of black-box operations that could wrongly replace existing migrations.

    Would you agree with a change towards this direction?

  5. added
    area: cli
    area: migrations
    in triage
    on Jan 2, 2026
  6. jwelmac commented on Jan 2, 2026

    @jwelmac
    ContributorAuthor

    Thanks for the feedback @marcelomendoncasoares

    After working through the complexities of the merge resolution and considering the other pathways to consider I agree with you. I would not mind removing the git related features and leaving that to the user. There are too many ways that issues can arise and its best that they are handled manually by the dev who will make the choices and take the responsibility.

    I would love to be a part of further discussions around this and other ways to enhance the framework. UX/DX is something I am really passionate about and it would be an honor to further Serverpod's mission in that aspect.

    Thanks again for the feedback.
    I will make the changes to the PR to remove the conflict resolution.

  7. marcelomendoncasoares commented on Jan 3, 2026

    @marcelomendoncasoares
    Collaborator

    You're welcome @jwelmac! And glad to know you would like to contribute more to this area of the framework! 🚀

    One of the changes that we will probably do in this initiative is to introduce a new migrations command that will contain all migration operations - as opposed as having several commands *-migration on the root of the CLI. We're going to decide on this very soon, but it would change the way we implement the append migration. Ideally we don't want to introduce new commands using the current format if we are going to place them under a namespace, so I'll suggest we briefly park this PR while we close these definitions.

    Then, as soon as we have them, I'll be sure to involve so we can move this implementation forward!

  8. moved this from Inbox 📫 to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Jan 3, 2026
  9. added
    ux
    and removed
    in triage
    on Jan 3, 2026
  10. jwelmac commented on Jan 4, 2026

    @jwelmac
    ContributorAuthor

    @marcelomendoncasoares
    That sounds like a great initiative.
    While working on this PR I had to extract some code of create-migration into a class so that it could be used within the rebase-migration command. This change would definitely change the structure of this command and others as we likely would need to redefine the project structure for common methods and classes.

    Love it. Looking forward to it.

    I am going to park the PR, but as a next step I will remove any code referencing conflict resolution so it only retains the necessary functionality.

  11. craiglabenz commented on Jan 7, 2026

    @craiglabenz
    Contributor

    Agreed with all the chatter here.

    As the team works on the RFC, I want to (once again 😆) advocate for a serverpod migrations rollback [migration_id] command which would un-apply any migrations downstream of the provided ID. IMO, this would make it quite manageable for a developer to back out one of their migrations to apply a different one that landed first, then regenerate and re-apply their own.

    This is how Django does it and it works very well there.

    Of course, to do this, the whole migrations feature in Serverpod would have to be expanded to start capturing inverse statements for all of its operations.

  12. craiglabenz commented on Jan 7, 2026

    @craiglabenz
    Contributor

    I first described that idea in #4539, but I'll now close that in favor of centralizing conversations here.

  13. jwelmac commented on Jan 7, 2026

    @jwelmac
    ContributorAuthor

    Agreed with all the chatter here.

    As the team works on the RFC, I want to (once again 😆) advocate for a serverpod migrations rollback [migration_id] command which would un-apply any migrations downstream of the provided ID. IMO, this would make it quite manageable for a developer to back out one of their migrations to apply a different one that landed first, then regenerate and re-apply their own.

    This is how Django does it and it works very well there.

    Of course, to do this, the whole migrations feature in Serverpod would have to be expanded to start capturing inverse statements for all of its operations.

    Great one @craiglabenz.

    With a refactor around migrations this may be an easy win as this is partly completed in the first stage of the current rebase-migration implementation. This includes backing up the migrations removed to the .dart-tool folder. Just in case there is a change of mind.

    Looking forward to the RFC because it will help to highlight the use cases and variations. That will help a lot with ensuring that all the appropriate functionality is exposed and accounted for during the implementation phase.

  14. marcelomendoncasoares commented on Jan 16, 2026

    @marcelomendoncasoares
    Collaborator

    @craiglabenz @jwelmac The RFC has just been posted! Would love to hear your perspective about the proposal.

  15. moved this from Contributor In Progress 👩‍💻 to Parked Contributions in Serverpod Frameworkson Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: cliarea: migrationsenhancementux

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions