pressly / goose

A database migration tool. Supports SQL migrations and Go functions.
http://pressly.github.io/goose/
Other
7.16k stars 523 forks source link

Command line issue: goose run: ERROR: relation "_db_migrations" already exists (SQLSTATE 42P07) #811

Closed albulescu closed 3 weeks ago

albulescu commented 3 months ago

Running:

goose -table _db_migrations up

Output:

2024/08/22 10:59:18 goose run: ERROR: relation "_db_migrations" already exists (SQLSTATE 42P07)
mfridman commented 3 months ago

Do you have any more information, which version of goose, which database, etc.?

E.g., using the latest version of goose and from the root of this repository:

$ make docker-postgres
...
postgres://dbuser:password1@localhost:5433/testdb?sslmode=disable

$ export GOOSE_DRIVER=pgx GOOSE_DBSTRING=postgres://dbuser:password1@localhost:5433/testdb?sslmode=disable

$ env | grep GOOSE_
GOOSE_DRIVER=pgx
GOOSE_DBSTRING=postgres://dbuser:password1@localhost:5433/testdb?sslmode=disable

$ goose -table _db_migrations -dir ./testdata/migrations up  
2024/08/22 06:34:24 OK   00001_users_table.sql (4.98ms)
2024/08/22 06:34:24 OK   00002_posts_table.sql (5.61ms)
2024/08/22 06:34:24 OK   00003_comments_table.sql (8.42ms)
2024/08/22 06:34:24 OK   00004_insert_data.sql (4.15ms)
2024/08/22 06:34:24 OK   00005_posts_view.sql (7.13ms)
2024/08/22 06:34:24 goose: successfully migrated database to version: 5

Then confirming the tables:

SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public';

_db_migrations
users
posts
comments
albulescu commented 3 months ago

It happens randomly even prior to v3.21.1 on osx.

mfridman commented 3 months ago

Are you running things in parallel? That's the only scenario I can think of that would cause this, especially in tests.

vzhurin commented 2 months ago

Have same problem with Postgres on Debian: 2024/09/13 16:42:11 goose run: ERROR: relation "goose_db_version" already exists (SQLSTATE 42P07) Goose version: 3.22.0 Postgres version: 13.8 I don't run anything in parallel.

alex-popov-tech commented 3 weeks ago

if that would help someone - i was having same issue with supabase postgres, until changed mode from transactional to sussion, after that all runs just fine