Skip to content

Make pgroll pull and pgroll migrate command able to handle incompatible migration file formats #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 6, 2025

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented May 1, 2025

Ensure that the pgroll pull and pgroll migrate commands are able to operate safely when either:

  • The local migration directory contains migrations that can not be deserialized by the current version of pgroll.
  • The migration history in the target database contains migrations that can not be deserialized by the current version of pgroll.

This ensures that these commands work in the presence of older migration formats, either locally or in the remote schema history.

This is done by adding a new RawMigration type to the migrations package, which does not deserialize the Operations in the migration but leaves them as a json.RawMessage.

The following methods in the roll and state packages are then updated to work with and return RawMigrations:

  • SchemaHistory - used to return the schema history from the target database.
  • MissingMigrations - used to find remote migrations that don't exist in the local history
  • UnappliedMigrations - used to find local migrations that don't exist in the remote.

By updating these methods and the pull and migrate commands that use them, these commands can work safely even with local or remote migrations that can't be deserialized by the current version of pgroll.

Tests are added for both the MissingMigrations and UnappliedMigrations methods to ensure that they work with un-deserializable migrations.

Part of #770

Add `RawMigration` alongside `Migration`. `RawMigration` contains the
raw operations as a `json.RawMessage` instead of a fully parsed
`Operations` type. This allows us to read the migration file without
parsing the operations, which is useful for migrations that are not
compatible with the current version of pgroll.
Make the `State.SchemaHistory` method return raw migrations instead of
migrations. This allows us to retrieve old migration histories that
contain migrations that are not compatible with the current version of
pgroll.
Change the return type of `MissingMigrations` to return raw migrations
instead of `migrations.Migrations`. This means that migrations in the
schema history that use older formats that can no longer be serialized
will not cause `MissingMigrations` to fail.
Make `UnappliedMigrations` read migrations from the local filesystem as
raw migrations instead of deserializing them. This allows unapplied
migrations in directories containing migrations in older file formats to
be found, without causing an error.

Change the return type to `RawMigration` to avoid having to parse the
migrations before returning them. It is the caller's responsibility to
parse the migrations, if required.
Make `pgroll pull` write raw migrations rather than
`migrations.Migration`s. This means the command can write migrations in
older file formats that can't be deserialized into `migration.Migration`
objects.
* Ensure that `UnappliedMigrations` can handle local migration files
  that are not deserializable.
* Ensure that `UnappliedMigrations` can handle remote migrations that are
  not deserializable.
* Ensure that `MissingMigrations` can handle local migration files
  that are not deserializable.
* Ensure that `MissingMigrations` can handle remote migrations that are
  not deserializable.
@github-actions github-actions bot temporarily deployed to Docs Preview May 1, 2025 13:02 Inactive
@andrew-farries andrew-farries marked this pull request as ready for review May 1, 2025 14:23
@andrew-farries andrew-farries requested a review from kvch May 6, 2025 07:35
@andrew-farries andrew-farries merged commit 542db44 into main May 6, 2025
30 checks passed
@andrew-farries andrew-farries deleted the migrate-and-pull-with-old-migration-histories branch May 6, 2025 12:17
andrew-farries added a commit that referenced this pull request May 21, 2025
Remove support for specifying a `name` field in migration files:

```yaml
name: 01_some_migration # <-- no longer supported
operations:
  - create_table:
      name: products
      columns:
        - name: id
          type: serial
          pk: true
        - name: name
          type: varchar(255)
```

The migration filename is the source of truth for the migration name.
The `name` field was made optional and its use discouraged in the
documentation in #744. This PR removes support for it entirely.

⚠️ **Warning**

**This is a breaking change:** migration files that set the `name` field
will not be runnable by `pgroll` until the `name` field is removed.

Because of #812 `pgroll pull` and
`pgroll migrate` will still work with migration histories that contain
migrations with `name` fields.

The easiest way to remove the `name` fields from all migrations in the
history is to `pgroll pull` the migration history; this will serialize
all migrations in the remote history, removing the any `name` fields.

---

Closes #715 and #850
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants