Skip to content

[baseline] Make start and migrate fail on non-empty schema without migration histories #835

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

Conversation

andrew-farries
Copy link
Collaborator

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

Make the pgroll start and pgroll migrate commands fail when run against a non-empty schema without a migration history. The error message suggests running pgroll baseline before re-running start or migrate.

Running an initial migration against a non-empty schema is not recommended becauses it can result in a migration that can't be rolled back if the migration references pre-existing objects in the schema.

The recommended workflow is:

  1. Create the initial baseline migration
$ pgroll baseline 00_initial_baseline migrations/
  1. Run migrate as usual
$ pgroll migrate migrations/ --complete

This ensures that the full schema is brought into pgroll before running the first migration.

Part of #364

@github-actions github-actions bot temporarily deployed to Docs Preview May 15, 2025 09:12 Inactive
@andrew-farries andrew-farries changed the title Make start and migrate fail on non-empty schema without migration histories [baseline] Make start and migrate fail on non-empty schema without migration histories May 15, 2025
@andrew-farries andrew-farries marked this pull request as ready for review May 15, 2025 09:17
@andrew-farries andrew-farries requested a review from kvch May 15, 2025 09:17
@gulcin
Copy link

gulcin commented May 15, 2025

@andrew-farries could not we run pgroll baseline as part of start and migrate when run against non-empty schema?

@andrew-farries
Copy link
Collaborator Author

@andrew-farries could not we run pgroll baseline as part of start and migrate when run against non-empty schema?

We could automatically run the baseline, but we'd have to prompt the user for a baseline version name as the baseline command does. The user may also want to populate and test the placeholder baseline migration before they proceed with applying any other migrations.

It feels more like this is a scenario where the user should be in control of the baseline rather than making it happen automatically.

@exekias
Copy link
Member

exekias commented May 15, 2025

Having it happen after a prompt sounds good to me!

@gulcin
Copy link

gulcin commented May 15, 2025

Yes, absolutely. We could make the user aware, and give them the option to add a version name. I think it would still be an improvement if we let the baseline process start and once that's complete, prompted with complete, a bit more hand-holding at that stage too.

@github-actions github-actions bot temporarily deployed to Docs Preview May 15, 2025 15:36 Inactive
@andrew-farries
Copy link
Collaborator Author

andrew-farries commented May 15, 2025

The CTA to run pgroll baseline more is more obvious in 808912d.

Not sure about prompting the user directly in migrate/start. Currently, the only commands that will wait for user input are:

  • create
  • baseline

migrate is going to be run in automated workflows, so I don't think it should block on user input. We would need a flag to abort if a baseline is needed instead of prompting but maybe just the call to run baseline in the output is enough here without adding more flags to the migrate command?

@gulcin
Copy link

gulcin commented May 15, 2025

The CTA to run pgroll baseline more is more obvious in 808912d.

Not sure about prompting the user directly in migrate/start. Currently, the only commands that will wait for user input are:

  • create
  • baseline

migrate is going to be run in automated workflows, so I don't think it should block on user input. We would need a flag to abort if a baseline is needed instead of prompting but maybe just the call to run baseline in the output is enough here without adding more flags to the migrate command?

Good point, we would not want to break CI/CD processes of our users. I am okay with "Run pgroll baseline first" in the output.

@andrew-farries andrew-farries force-pushed the start-check-for-empty-history-with-schema branch from 808912d to 6d26c92 Compare May 16, 2025 12:58
@github-actions github-actions bot temporarily deployed to Docs Preview May 16, 2025 12:59 Inactive
@andrew-farries andrew-farries force-pushed the migrate-with-baseline branch from 06fb647 to f574366 Compare May 16, 2025 13:07
Base automatically changed from migrate-with-baseline to main May 16, 2025 13:10
The method checks whether the given schema is non-empty with an empty
history.
Allow tests to work with an uninitialized state and provide connection
info so that the test can initialize `pgroll` at some later point.
Ensure that starting a migration on a schema with existing tables but no
migration history fails with the expected error.
Make `Roll.Start` return an error when a caller tries to start a
migration when there is existing schema and no migration history.
Make the `start` and `migrate` commands check whether a baseline is
needed and exit early if so.
@andrew-farries andrew-farries force-pushed the start-check-for-empty-history-with-schema branch from 6d26c92 to 6adfba2 Compare May 16, 2025 13:11
@github-actions github-actions bot temporarily deployed to Docs Preview May 16, 2025 13:12 Inactive
@andrew-farries andrew-farries merged commit 50d690a into main May 16, 2025
30 checks passed
@andrew-farries andrew-farries deleted the start-check-for-empty-history-with-schema branch May 16, 2025 14:46
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.

4 participants