Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
fix: Do not throw errors for idle transactions during migration (#118)
Browse files Browse the repository at this point in the history
Catch and ignore `pq: unexpected transaction status idle` errors when we
commit the migrations transaction.  We have not see the error in
practice when testing the recent changes to the migrations _but_ we do
see this in the unit tests for Hub. This error indicates that a
transaction has started, ie `BEGIN;` but that nothing has happened in
the transaction yet. A similar (but resolved) issue happened in the pq
driver, but the culprit (unclosed Rows) does not apply to our method,
all possible Row objects are automatically closed for us by the sql
methods. See lib/pq#225

See also
https://www.postgresql.org/message-id/[email protected]

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored May 7, 2021
1 parent 75bb109 commit 1cd3f2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ setup-env: $(GIT_SEMVER) $(GOLANGCI_LINT) ## Setup dev environment

.PHONY: .test-ci
.test-ci:
go test -v -cover ./...
go test -cover ./...

.PHONY: changelog
changelog: ## Print git hitstory based changelog
Expand Down
12 changes: 12 additions & 0 deletions pkg/db/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func migrate(ctx context.Context, db *sql.DB, list []string, assets http.FileSys
defer func() {
if err == nil {
err = tx.Commit()
// dirty ugly no-good hack!
// we have not seen this in the wild yet, _but_ during our unit tests
// we sometimes get this error, which indicates that the transaction was
// started (ie `BEGIN;`) but nothing has happened yet. For example:
// https://www.postgresql.org/message-id/[email protected]
// https://github.com/lib/pq/issues/225
// Initial experiments have only produced this during unit tests, but actual
// application environments run without any transaction issues.
if err != nil && err.Error() == "pq: unexpected transaction status idle" {
logger.WithError(err).Warn("idle transaction at Commit")
err = nil
}
if err != nil {
logger.WithError(err).Error("can not commit migrations transaction")
}
Expand Down

0 comments on commit 1cd3f2b

Please sign in to comment.