[WIP] - goose schema command (postgres only) #459
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Close #278
EDIT: I think I'd like to put this under
goose beta schema
, to denote that this is an unstable command and is experimental. And we can promote this to a more stable command, likegoose schema
in the future. Hopefully with such commands we can get feedback from users, and fix any issues before making it "stable".This PR adds a
goose schema
command that shells out to eitherpg_dump
ordocker
(latest stable postgres version). By default, this dumps with--schema-only
and preserves the raw output, but there is an optional goose flag--clean
that effectively does what #345 (comment) describes:But why?
It's often desirable to check in the database schema, which can be used to re-create the database in lieu of the migrations.
Taking this a step further, when developers iterate on database changes locally they might interact with the database and apply SQL manually, but ultimately forget to port those changes over to their migration files. In CI the checked-in migrations can be applied and the schema dumped, that schema can then be compared to the committed schema within a PR, if there is a mismatch it means the migration files do not match the intended database schema, and the developer is likely working with incorrect assumptions. This should be caught as early as possible to avoid invalid assumptions of the database state.
Even if you don't take it this far, just having a schema in one place makes it easier to reason about. And building this into
goose
allows everyone to have a consistent way of dumping the schema.Example
From the root of this repository:
$ make docker-start-postgres $ export GOOSE_MIGRATION_DIR=./examples/sql-migrations $ go run ./cmd/goose up $ go run ./cmd/goose status 2023/01/28 16:27:17 Applied At Migration 2023/01/28 16:27:17 ======================================= 2023/01/28 16:27:17 Sat Jan 28 21:19:06 2023 -- 00001_create_users_table.sql 2023/01/28 16:27:17 Sat Jan 28 21:19:06 2023 -- 00002_rename_root.sql 2023/01/28 16:27:17 Sat Jan 28 21:19:06 2023 -- 00003_no_transaction.sql
Then dump the schema based on the example migrations with
pg_dump
, the sha256 is:6380fab48d773d69abcfa38a6c451b704b4b466e2b272b3f96778a2462f9a998
And the resulting
go run ./cmd/goose schema --clean | sha256sum
command:6380fab48d773d69abcfa38a6c451b704b4b466e2b272b3f96778a2462f9a998