Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop() does not work after https://github.com/golang-migrate/migrate/pull/150 #164

Closed
herpiko opened this issue Feb 1, 2019 · 5 comments

Comments

@herpiko
Copy link

herpiko commented Feb 1, 2019

Describe the Bug

I'm using migrate as lib. Drop() after initializing migrate instance does not work anymore.

Steps to Reproduce
Steps to reproduce the behavior:

	m, err := migrate.New(
		migrationPath,
		url)
	if err != nil {
		log.Fatal(err)
	}
	err = m.Drop()
	if err != nil && err.Error() != "no change" {
		log.Fatal(err)
	}
	err = m.Up()
	if err != nil && err.Error() != "no change" {
		log.Fatal(err)
	}

Expected Behavior

Before 4.2.3, Drop() was always work.

Migrate Version
e.g. v4.2.3

Obtained by running my unit test program that using migrate as lib.

Loaded Source Drivers
N/A

Loaded Database Drivers
postgres

Go Version
go version go1.11.1 linux/amd64

Stacktrace

MIGRATION_PATH=`pwd`/migrations/test go test
db_test.go:69: Migrating postgres://db:db@0.0.0.0:65432/testdb?sslmode=disable from file:///home/path/to/src/migrations/test
db_test.go:78: can't acquire lock
exit status 1
FAIL	xxx/yyy/zzz	0.169s
Makefile:15: recipe for target 'test' failed
make: *** [test] Error 1
@dhui
Copy link
Member

dhui commented Feb 1, 2019

Can you provide the full test code?

Are you running migrate in a goroutine? Based on the error message, It looks like you're hitting the case where local lock is being hit. e.g. database.ErrLocked is returned if multiple goroutines are locking migrate. It doesn't seem like an issue where grabbing the Postgres advisory lock is failing.

@herpiko
Copy link
Author

herpiko commented Feb 1, 2019

Are you running migrate in a goroutine?

No.

@dhui Okay I'll provide the reproducible code ASAP.

@lukaspj
Copy link
Contributor

lukaspj commented Feb 15, 2019

I'm experiencing this issue, seeing:

tearing down existing database because messages.database.fresh is true
can't acquire lock
failed to tear down database

I'm running on a clean Postgresql instance running inside Kubernetes via Minikube, deployed from Helm.
The code I wrote, it's not a Minimal Working Example, but I wanted to keep everything:
https://gist.github.com/lukaspj/eee69764cca2b58bb58fc368e4658a00

@lukaspj
Copy link
Contributor

lukaspj commented Feb 15, 2019

It seems to happen in postgres.go line 328

if err := p.ensureVersionTable(); err != nil {

Maybe some lock is not released previously or it might happen inside ensureVersionTable entirely.

Edit: Well it's not ensureVersionTable itself, as it's called earlier in WithInstance, so it must happen in Drop()

Edit2: Happens because p.isLocked is true

@lukaspj
Copy link
Contributor

lukaspj commented Feb 15, 2019

My guess would be that the Lock is acquired in migrate.go:

migrate/migrate.go

Lines 304 to 312 in f6d624c

func (m *Migrate) Drop() error {
if err := m.lock(); err != nil {
return err
}
if err := m.databaseDrv.Drop(); err != nil {
return m.unlockErr(err)
}
return m.unlock()
}

In the subsequent call to Drop, the database is still locked so when we call ensureVersionTable and it tries to lock the database it dies:

if err := p.ensureVersionTable(); err != nil {
return err
}

Edit: better links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants