Skip to content
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

Add support for running SQL migrations as a regular migrator #710

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Mar 4, 2025

This PR adds a new command called sqlmigrate. This lets users run regular SQL migration scripts as they are written in the files. It does not add any pgroll guarantees like avoiding locks or providing multiple schema versions.

The command is for people who want to use pgroll as a regular SQL migrator.

@github-actions github-actions bot temporarily deployed to Docs Preview March 4, 2025 15:45 Inactive
@kvch kvch requested a review from andrew-farries March 13, 2025 15:48
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

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

I wonder if a different approach could work here. Rather than a new sqlmigrate command we allow the existing migrate command to work with .sql files too. So a migrations directory could look like:

01_some_migration.json
02_some_sql.sql
03_some_other_migration.json

I'm not sure what the start, complete, rollback workflow would look like for .sql files though.

It may be that supporting .yaml formats for migrations makes writing SQL in migration files less painful, in which case we wouldn't need to support standalone .sql files too.

@kvch
Copy link
Contributor Author

kvch commented Mar 14, 2025

I wonder if a different approach could work here. Rather than a new sqlmigrate command we allow the existing migrate command to work with .sql files too. So a migrations directory could look like:

01_some_migration.json
02_some_sql.sql
03_some_other_migration.json

Good idea.

I'm not sure what the start, complete, rollback workflow would look like for .sql files though.

Maybe we can expect migration_name.up.sql and migration_name.down.sql as SQL scripts, so start and rollback is taken care of. I would say complete is a no-op for this kind of operations. When a user runs an SQL migration, it is completed automatically after the script is finished. Also, having a down migration would not be a requirement.

It may be that supporting .yaml formats for migrations makes writing SQL in migration files less painful, in which case we wouldn't need to support standalone .sql files too.

The user request is that they want to run regular SQL migrations. So they do not have to write JSON/YAML/whatever format files. So I want to add support for SQL input as well.

@andrew-farries
Copy link
Collaborator

andrew-farries commented Mar 14, 2025

Maybe we can expect migration_name.up.sql and migration_name.down.sql as SQL scripts, so start and rollback is taken care of. I would say complete is a no-op for this kind of operations. When a user runs an SQL migration, it is completed automatically after the script is finished. Also, having a down migration would not be a requirement.

Having foo.up.sql and foo.down.sql makes sense to me.

With our current 'raw SQL' migations, we have the onComplete field which defaults to false. When true, the SQL is run on migration completion rather than start. We would probably lose the ability to do this if we go with the foo.up.sql and foo.down.sql approach.

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