Skip to content

Commit

Permalink
Change TRUNCATE to DELETE FROM in postgres database
Browse files Browse the repository at this point in the history
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: mattes/migrate#320

Signed-off-by: Elliott Davis <[email protected]>
  • Loading branch information
Elliott Davis committed May 22, 2018
1 parent faf4307 commit 1e148b9
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
nurl "net/url"

"context"

"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database"
"github.com/lib/pq"
Expand Down Expand Up @@ -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)}
Expand Down

1 comment on commit 1e148b9

@bernielomax
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixed an issue I was experiencing with CockroachDB. However this project is now deprecated. https://github.com/golang-migrate/migrate is now the official project.

Please sign in to comment.