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 a pgroll pull subcommand #463

Merged
merged 4 commits into from
Nov 14, 2024
Merged

Add a pgroll pull subcommand #463

merged 4 commits into from
Nov 14, 2024

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Nov 13, 2024

Add a new pgroll pull command that pulls the complete migration history for a schema from the migrations table in the target database and dumps the migrations to disk.

Example

Given the migration history in pgroll.migrations after applying all example/ migrations in this repo:

+--------+-----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
| schema | name                                    | migration                                                                                                                                                                                                                                              >
|--------+-----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
| public | 01_create_tables                        | {"name": "01_create_tables", "operations": [{"create_table": {"name": "customers", "columns": [{"pk": true, "name": "id", "type": "integer"}, {"name": "name", "type": "varchar(255)", "unique": true}, {"name": "credit_card", "type": "text", "nullab>
| public | 02_create_another_table                 | {"name": "02_create_another_table", "operations": [{"create_table": {"name": "products", "columns": [{"pk": true, "name": "id", "type": "serial"}, {"name": "name", "type": "varchar(255)", "unique": true}, {"name": "price", "type": "decimal(10,2)"}>
| public | 03_add_column_to_products               | {"name": "03_add_column_to_products", "operations": [{"add_column": {"up": "UPPER(name)", "table": "products", "column": {"name": "description", "type": "varchar(255)", "nullable": true}}}, {"add_column": {"table": "products", "column": {"name": ">
| public | 04_rename_table                         | {"name": "04_rename_table", "operations": [{"rename_table": {"to": "clients", "from": "customers"}}]}                                                                                                                                                  >
| public | 05_sql                                  | {"name": "05_sql", "operations": [{"sql": {"up": "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)", "down": "DROP TABLE users"}}]}                                                                                                              >
| public | 06_add_column_to_sql_table              | {"name": "06_add_column_to_sql_table", "operations": [{"add_column": {"up": "UPPER(name)", "table": "users", "column": {"name": "description", "type": "varchar(255)", "nullable": true}}}]}                                                           >
...

Run:

$ pgroll pull migrations/

This will pull all migrations from the pgroll.migrations table into the migrations/ directory:

$ ls migrations/

01_create_tables.json
02_create_another_table.json
03_add_column_to_products.json
04_rename_table.json
05_sql.json
06_add_column_to_sql_table.json
...

The optional --with-prefixes flag prefixes each migration name with its position in the schema history:

$ pgroll pull --with-prefixes migrations/

This produces the following files:

$ ls migrations/

0001_01_create_tables.json
0002_02_create_another_table.json
0003_03_add_column_to_products.json
0004_04_rename_table.json
0005_05_sql.json
0006_06_add_column_to_sql_table.json
...

The --with-prefixes flag ensures that files are sorted lexicographically by their time of application.

@andrew-farries andrew-farries marked this pull request as ready for review November 13, 2024 16:29
pkg/state/history.go Outdated Show resolved Hide resolved
pkg/state/history.go Outdated Show resolved Hide resolved
pkg/state/history.go Outdated Show resolved Hide resolved
Copy link
Member

@exekias exekias left a comment

Choose a reason for hiding this comment

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

nice! should we add something to the docs?

@andrew-farries andrew-farries force-pushed the pgroll-pull-command branch 5 times, most recently from 7e67ccb to bc01a35 Compare November 14, 2024 07:58
@andrew-farries
Copy link
Collaborator Author

nice! should we add something to the docs?

If we're happy that pgroll pull is the right command name, yes.

I've added documentation in the most recent commit on the branch.

cmd/pull.go Outdated Show resolved Hide resolved
Define:
* A new `Migration` type to represent a single migration in the
  migration history of a schema.
* A `SchemaHistory` method on the `State` type to return all migrations
  applied to a schema in ascending timestamp order.
* A `WriteToFile` method on the `Migration` type to write the
  migration to a file in the target directory, optionally prepending the
  filename with the migration timestamp.
Pull the migration history of a schema from the target database and
write it to disk.
@exekias
Copy link
Member

exekias commented Nov 14, 2024

Awesome! I wonder if we should (not in this PR) update our examples folder to use these prefixes, while taking the numbers out of the migration names

@andrew-farries
Copy link
Collaborator Author

Awesome! I wonder if we should (not in this PR) update our examples folder to use these prefixes, while taking the numbers out of the migration names

Taking migration numbers out of the migration names is a good idea I think 👍

@andrew-farries andrew-farries merged commit 30d131e into main Nov 14, 2024
27 checks passed
@andrew-farries andrew-farries deleted the pgroll-pull-command branch November 14, 2024 12:06
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