goose icon indicating copy to clipboard operation
goose copied to clipboard

Issue with Turso + Goose provider

Open pedelman opened this issue 1 year ago • 5 comments

Hello, I recently ran into an issue with Goose + Turso when trying to leverage the Provider to migrate multiple Turso DBs.

provider, err := goose.NewProvider(
	database.DialectTurso,
	db.DB,
	os.DirFS(migrationsDir),
)
if err != nil {
	logger.Error(err.Error())
	continue
}

_, err = provider.Up(ctx)
if err != nil {
	logger.Error(err.Error())
	continue
}
{"time":"2025-06-21T18:28:32.642253-05:00","level":"ERROR","msg":"failed to list migrations: failed to execute SQL:\nstream is closed: driver: bad connection; sql: connection is already closed"}

When I run the migrations via Goose command directly, they work just fine.

GOOSE_DRIVER=turso GOOSE_DBSTRING="libsql://REDACTED" goose up -dir migrations/client-db
2025/06/21 18:29:30 goose: no migrations to run. current version: 20250204025224

Dependencies and Go version:

go 1.23.0

require (
	github.com/jmoiron/sqlx v1.4.0
	github.com/joho/godotenv v1.5.1
	github.com/pressly/goose/v3 v3.24.3
)

require (
	github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
	github.com/coder/websocket v1.8.13 // indirect
	github.com/mfridman/interpolate v0.0.2 // indirect
	github.com/sethvargo/go-retry v0.3.0 // indirect
	github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d // indirect
	go.uber.org/multierr v1.11.0 // indirect
	golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
	golang.org/x/sync v0.15.0 // indirect
)

Thanks in advance and please let me know if I can provide any other helpful information for my setup. Whats a bit puzzling is that this worked before.

Previously this was working on github.com/pressly/goose/v3 v3.24.0.

I updated to confirm its still not working on latest.

pedelman avatar Jun 21 '25 23:06 pedelman

Thanks for filing an issue. I took a quick look at the change logs and don't see anything that could have affected the sqlite/turso side of things.

I also don't see any recent changes to the driver either, except for the transitive deps.

https://github.com/tursodatabase/libsql-client-go

I'll need to allocate some time to setup a turso db (hopefully there's still a free plan) and bisect the issue.

mfridman avatar Aug 24 '25 20:08 mfridman

Looks like this is still an issue, did you find a fix?

ainsleyclark avatar Apr 29 '26 07:04 ainsleyclark

I reproduced this and have a candidate fix here:

https://github.com/raybarrera/goose/tree/fix-turso-provider-connection

Root cause appears to be Provider-specific connection handling. The Provider currently leases a dedicated *sql.Conn, runs version-table initialization, then later runs migration state queries on that same dedicated connection. With remote libsql/Turso, the first query can succeed and the next query on that same leased connection fails with:

stream is closed: driver: bad connection; sql: connection is already closed

The legacy CLI path works because it uses pool-level *sql.DB queries for this version-store flow.

The branch keeps the change scoped to built-in DialectTurso: Turso Provider version-store operations use pooled *sql.DB execution, while other dialects and WithSessionLocker remain pinned to *sql.Conn.

I intentionally did not add a public option for custom stores yet. If the built-in Turso-only direction seems acceptable, I will open this as a PR. If you would prefer this as a broader Provider option/capability instead, I can adjust before opening.

Edit: I ran into this in my own project using Turso Cloud via libsql. For now I worked around it by switching that migration path back to Goose’s legacy API, which avoids the dedicated *sql.Conn sequence above. I am also exploring the Provider fix above so Goose can handle this path directly.

raybarrera avatar Jul 02 '26 04:07 raybarrera

Could this be a bug in the driver itself? It feels like it should honor the *sql.Conn like many of the other implementations.

Putting that aside, I have some thoughts around how to handle this a bit more cleanly. There's already a database.DBTxConn abstraction for handling both *sql.Conn and *sql.DB, so I believe the right place for this is probably in the (*Provider).initialize method. I was hoping to have a centralized place to add connection handling, locking, deferred cleanup, initial table creation, etc. but I suspect some of these concerns we may need to handle differently depending on the database.

mfridman avatar Jul 02 '26 12:07 mfridman

I can probably be more thorough, but there wasn't a ton of information to go by: a lone discord thread and some issue chatter on the driver repos seemed to indicate that the needed change was with goose, not them, but to be fair, I took that at face value, so I'll dig into the driver there and see what I find. I’ll also reshape the candidate fix around initialize and the existing DBTxConn abstraction rather than treating this as a store/dialect concern.

raybarrera avatar Jul 02 '26 15:07 raybarrera