From 1e148b9fa83ec7f44c69087677aa957c4953ae77 Mon Sep 17 00:00:00 2001 From: Elliott Davis Date: Mon, 21 May 2018 22:18:58 -0500 Subject: [PATCH] Change TRUNCATE to DELETE FROM in postgres database This actually isn't a problem for postgres but is a problem for cockroach. Internally cockroach drops the table when running a truncate which causes the insert to fail inside of this transaction. I added repro steps to mattes/migrate#320 which this PR should close. Closes: https://github.com/mattes/migrate/issues/320 Signed-off-by: Elliott Davis --- database/postgres/postgres.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/postgres/postgres.go b/database/postgres/postgres.go index bbab60f1d..5dc23237f 100644 --- a/database/postgres/postgres.go +++ b/database/postgres/postgres.go @@ -10,6 +10,7 @@ import ( nurl "net/url" "context" + "github.com/golang-migrate/migrate" "github.com/golang-migrate/migrate/database" "github.com/lib/pq" @@ -185,7 +186,7 @@ func (p *Postgres) SetVersion(version int, dirty bool) error { return &database.Error{OrigErr: err, Err: "transaction start failed"} } - query := `TRUNCATE "` + p.config.MigrationsTable + `"` + query := `DELETE FROM "` + p.config.MigrationsTable + `"` if _, err := tx.Exec(query); err != nil { tx.Rollback() return &database.Error{OrigErr: err, Query: []byte(query)}