Skip to content

[baseline] Make pgroll migrate baseline-aware #834

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 4 commits into from
May 16, 2025

Conversation

andrew-farries
Copy link
Collaborator

Make the pgroll migrate command baseline-aware. This means that only migrations since the most recent baseline are considered for application.

This PR:

  1. Adds a LatestBaseline method to the State package that retrieves the most recent baseline migration for a schema
  2. Changes the UnappliedMigrations function to properly handle baselines by only considering migrations after the most recent baseline
  3. Adds test coverage for baseline functionality with various scenarios

Example workflow

Initial Setup

Database: Empty
Migration Files: None
Migration History: Empty

Step 1: Create Initial Migrations

# 01_create_users.yaml
name: 01_create_users
operations:
  - create_table:
      name: users
      columns:
        - name: id
          type: serial
          pk: true
        - name: name
          type: varchar
# 02_create_posts.yaml
name: 02_create_posts
operations:
  - create_table:
      name: posts
      columns:
        - name: id
          type: serial
          pk: true
        - name: title
          type: varchar

Run pgroll migrate:

Migration History:
- 01_create_users (pgroll, done=true)
- 02_create_posts (pgroll, done=true)

Step 2: Create a Baseline

$ pgroll baseline 03_baseline
Migration History:
- 01_create_users (pgroll, done=true)
- 02_create_posts (pgroll, done=true)
- 03_baseline (baseline, done=true)

Step 3: Add More Migrations

# 04_add_comments.yaml
name: 04_add_comments
operations:
  - create_table:
      name: comments
      columns:
        - name: id
          type: serial
          pk: true
        - name: content
          type: text

Step 4: Run pgroll migrate Again

When running pgroll migrate, pgroll:

  1. Finds latest baseline (03_baseline)
  2. Only applies migrations after the baseline
Migration History:
- 01_create_users (pgroll, done=true)
- 02_create_posts (pgroll, done=true)
- 03_baseline (baseline, done=true)
- 04_add_comments (pgroll, done=true)

Part of #364

@github-actions github-actions bot temporarily deployed to Docs Preview May 14, 2025 11:44 Inactive
@andrew-farries andrew-farries marked this pull request as ready for review May 14, 2025 11:58
@andrew-farries andrew-farries requested a review from kvch May 14, 2025 11:58
Base automatically changed from pull-with-baseline to main May 16, 2025 13:06
Add a method to retrieve the latest baseline migration from the
migration history.

Add tests to ensure that the method works correctly with other
migrations coming before and after the baseline, and with multiple
baselines.
Make the `UnappliedMigrations` method aware of baselines.

Only migrations that come after the most recent baseline should be
considered unapplied; the baseline and all preceding migrations are
never considered unapplied as they are no longer part of the schema
history.
Ensure that `UnappliedMigrations` correctly returns the list of
unapplied migrations from a local migrations directory in the cases
where there are 1 or more baselines in the remote schema history.
@andrew-farries andrew-farries force-pushed the migrate-with-baseline branch from 06fb647 to f574366 Compare May 16, 2025 13:07
@github-actions github-actions bot temporarily deployed to Docs Preview May 16, 2025 13:07 Inactive
@andrew-farries andrew-farries merged commit 32bfe51 into main May 16, 2025
30 checks passed
@andrew-farries andrew-farries deleted the migrate-with-baseline branch May 16, 2025 13:10
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