Skip to content

cli: check for inconsistent migrations before migrate apply #8056

@rikinsk

Description

@rikinsk

Is your proposal related to a problem?

The CLI currently applies all unapplied migrations without ensuring the new migrations are consistent with already applied migrations. ie. it doesnt ensure if the migrations are incremental and all already applied migrations are also present in the list of migrations that are being applied.

This can cause issues when already applied migrations are squashed or if migrations of one project get applied on another project by mistake.

The motivation is to ensure the user is aware of any migration state inconsistency between the server and the local files system before updating any migrations state

Describe the solution you'd like

The CLI should ensure that migrations are incremental, ie. all migrations that are already applied on a database exist in the list of migrations that are being applied before applying any new unapplied migrations or updating any migrations state

  • The CLI by default can raise a warning/error (depending on the interactive mode) if it detects any migrations on the server that are not present in the migrations directory. In interactive mode the user can either choose to continue or abort the migrate apply.
  • There can be a flag, --skip-consistent-migrations-check, to skip this check.
  • Suggested warning/error should help explain the reason and how the user can get around it .e.g "Some migrations already applied on your database seem to be missing in the current migration list. Please check the migrations being applied or fix the migrations state of the database by deleting any irrelevant migrations using < command >". Or something on these lines that makes next steps clear
  • The same logic should work for all commands migrate apply sub-commands like --goto, --up, --down, --version etc.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions