-
Notifications
You must be signed in to change notification settings - Fork 525
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
feature: squashing migrations #345
Comments
Hi there, We have this command in our CI pipeline, which we run on each commit:
It exports the latest schema from our Postgres DB into a a We "squash" all schema migrations by removing all existing migrations and renaming this Hope this helps. |
@VojtechVitek sorry to necro this thread, but I'm trying to reproduce the squashing flow you mention here. After squashing, do you also delete all recorded versions from the I could see this working, just trying to figure out if I'm missing anything. :)
|
We keep the goose table in tact. Before:
After
|
How does that work with Here's an example where migrations Before
After
Also, thanks a bunch for answering questions like this, it helps a lot. :) |
I see, so Maybe try this instead: - 00001_squashed.sql
- 00002_f.sql
+ 00006_squashed.sql
+ 00007_f.sql and keep incrementing the new migrations |
Yeah that seems like the obvious solution, thanks! :) |
Here is my current hack for creating a "squashed" schema SQL for Postgres. You'll need:
#!/bin/bash
set -euo pipefail
# --- Settings (update these) --- #
# - Where should the schema be written
SCHEMA_OUT=$PWD/schema/schema.sql
# - Where is the migration directory
MIGRATIONS_IN=$PWD/migration
# - Which postgres docker image to use
POSTGRES_IMAGE=postgres:14
# --- End of Settings --- #
# Start Postgres Server
docker run -d --name goose-postgres -p 15432:5432 --rm -e POSTGRES_PASSWORD=secret ${POSTGRES_IMAGE}
sleep 5 # TODO: better way to wait for db ready
export PGDATABASE=postgres
export PGHOST=127.0.0.1
export PGPORT=15432
export PGUSER=postgres
export PGPASSWORD=secret
export GOOSE_DRIVER=postgres
export GOOSE_DBSTRING="host=${PGHOST} port=${PGPORT} user=${PGUSER} dbname=${PGDATABASE} password=${PGPASSWORD} sslmode=disable"
goose status
goose -dir ${MIGRATIONS_IN} up
goose status
pg_dump --schema-only \
--no-comments \
--quote-all-identifiers \
-T public.goose_db_version \
-T public.goose_db_version_id_seq | sed \
-e '/^--.*/d' \
-e '/^SET /d' \
-e '/^[[:space:]]*$/d' \
-e '/^SELECT pg_catalog./d' \
-e '/^ALTER TABLE .* OWNER TO "postgres";/d' \
-e 's/"public"\.//' \
> ${SCHEMA_OUT}
docker kill goose-postgres |
Hi,
I have a long-running project and the number of sequential migration files is getting pretty high; it's mainly cosmetic, but I was wondering what the best way to squash all existing migrations into a single migration would be?
My first thought is to write a script that loops through the
*.sql
files and concats the sequentially into a single new migration file, addingIF EXISTS/IF NOT EXISTS
to the variousCREATE/ALTER
statements to make the actions idempotent. Then I can simply run the migration (which should have no effect on the DB), and delete the old files.Is this the easiest way? Is there a way I can achieve this without adding the
IF NOT EXISTS
logic into my script (updating the pointer of the latest applied migration to my migration without actually having to run it)? Or is there an even easier way to achieve this?Thank you!
The text was updated successfully, but these errors were encountered: