Skip to content

[baseline] Add pgroll baseline command #832

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 9 commits into from
May 13, 2025
Merged

Conversation

andrew-farries
Copy link
Collaborator

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

Add a new pgroll baseline command to pgroll that allows users to establish a baseline migration from an existing database schema.

Usage

Run:

$ pgroll baseline 01_initial_version migrations/

This does two things:

  • Creates a new entry in pgroll.migrations with a new migration type of baseline. The migration JSON is empty and the resulting_schema field contains the schema at the time the command was run.
  • Creates a placeholder migrations/01_initial_version.yaml file for the user to populate (likely with the use of pg_dump).

Features

  • New pgroll baseline <version> <target directory> command (currently hidden)
  • Creates a baseline migration in the pgroll.migrations table that captures the current schema state without applying changes.
  • Validation to prevent baseline creation during active migrations
  • Adds test coverage for baseline functionality

Implementation Details

  • Added Roll.CreateBaseline method and corresponding tests
  • Added State.CreateBaseline method for writing baseline migrations to pgroll.migrations
  • Extended migration type definition to include 'baseline' type

The pgroll baseline command is currently hidden. Un-hiding the command will happen when the pull and migrate commands are made baseline-aware.

This is the first part of #364

@github-actions github-actions bot temporarily deployed to Docs Preview May 13, 2025 12:46 Inactive
Redefine the `migration_type` column to allow a `baseline` migration type.
The functionality was duplicated.
Make `writeMigrationToFile` return the path of the created file on
success.
Run `make generate`.
@github-actions github-actions bot temporarily deployed to Docs Preview May 13, 2025 12:51 Inactive
@andrew-farries andrew-farries marked this pull request as ready for review May 13, 2025 13:12
@andrew-farries andrew-farries requested a review from kvch May 13, 2025 13:13
Copy link
Contributor

@kvch kvch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code and the highlevel functionality looks good to me. I have one minor usability improvement ideas for a later PR:
It would be nice if pgroll would create the local baseline migration after the baselining was successful. I got an error from baseline a few times, and the migration file was there in my migrations folder, even though the database was not baselined.

Ensure that the migration file is removed if the baseline creation
fails.
@github-actions github-actions bot temporarily deployed to Docs Preview May 13, 2025 22:28 Inactive
@andrew-farries
Copy link
Collaborator Author

It would be nice if pgroll would create the local baseline migration after the baselining was successful. I got an error from baseline a few times, and the migration file was there in my migrations folder, even though the database was not baselined.

Resolved in 71459ab. The migration file is cleaned up if creating the baseline in the target database fails.

@andrew-farries andrew-farries merged commit 2aee64f into main May 13, 2025
30 checks passed
@andrew-farries andrew-farries deleted the add-baseline-cmd branch May 13, 2025 22:36
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