"NO TRANSACTION" migrations should mark database as dirty if not successful
Author: tumelowillCreated Mar 21, 2024Updated Sep 22, 2026
Migrations that do not have a transaction will not be rolled back. We should mark the database as dirty if the migration failed. Migrations on dirty databases should not be allowed.
Example:
-- 1_create_posts_table.sql
-- +goose Up
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(255),
content TEXT,
author_id INTEGER
);-- 2_update_posts.sql
-- +goose NO TRANSACTION
-- +goose Up
INSERT INTO posts (title, content, author_id) VALUES ('Hello World', 'This is my first post!', 1);
CREATE INDEX CONCURRENTLY idx_created_at ON posts (created_at); -- This fails as the field created_at does not exist on the posts tableIf we have a policy of running migrations when the database schema is outdated, we are now filling the posts table with inserts on each migration run as we have no idea whether the database is dirty.
What we would like to see after attempting to migrate multiple times:
postgres=# select * from public.posts;
id | title | content | author_id
----+-------------+------------------------+-----------
1 | Hello World | This is my first post! | 1
(1 row)
postgres=# select * from public.goose_db_version;
id | version_id | is_applied | tstamp
----+------------+------------+----------------------------
1 | 0 | t | 2024-03-21 14:33:00.782058
2 | 1 | f | 2024-03-21 15:08:48.144995
(2 rows)What we actually see after attempting to migrate multiple times:
postgres=# select * from public.posts;
id | title | content | author_id
----+-------------+------------------------+-----------
1 | Hello World | This is my first post! | 1
2 | Hello World | This is my first post! | 1
3 | Hello World | This is my first post! | 1
4 | Hello World | This is my first post! | 1
5 | Hello World | This is my first post! | 1
(5 rows)
postgres=# select * from public.goose_db_version;
id | version_id | is_applied | tstamp
----+------------+------------+----------------------------
1 | 0 | t | 2024-03-21 14:33:00.782058
(1 row)Source: pressly/goose