fix(litemigrate,mymigrate,msmigrate): take *sql.Conn instead of *sql.DB

Same issue as pgmigrate: *sql.DB is a connection pool, so each call
may land on a different connection. Migrations need a pinned connection
for session state (SET search_path, temp tables, etc.) to persist
across sequential calls. *sql.Conn (from db.Conn(ctx)) pins one
underlying connection for its lifetime.
This commit is contained in:
AJ ONeal 2026-04-09 10:51:41 -06:00
parent d5d1df060f
commit 67ad7a9fa2
3 changed files with 33 additions and 26 deletions

View File

@ -4,6 +4,7 @@
// import _ "modernc.org/sqlite" // import _ "modernc.org/sqlite"
// //
// db, err := sql.Open("sqlite", "app.db?_pragma=foreign_keys(1)") // db, err := sql.Open("sqlite", "app.db?_pragma=foreign_keys(1)")
// conn, err := db.Conn(ctx)
// //
// SQLite disables foreign key enforcement by default. The _pragma DSN // SQLite disables foreign key enforcement by default. The _pragma DSN
// parameter enables it on every connection the pool opens. // parameter enables it on every connection the pool opens.
@ -18,14 +19,15 @@ import (
"github.com/therootcompany/golib/database/sqlmigrate" "github.com/therootcompany/golib/database/sqlmigrate"
) )
// Migrator implements sqlmigrate.Migrator using a *sql.DB with SQLite. // Migrator implements sqlmigrate.Migrator using a *sql.Conn with SQLite.
type Migrator struct { type Migrator struct {
DB *sql.DB Conn *sql.Conn
} }
// New creates a Migrator from the given database handle. // New creates a Migrator from the given connection.
func New(db *sql.DB) *Migrator { // Use db.Conn(ctx) to obtain a *sql.Conn from a *sql.DB.
return &Migrator{DB: db} func New(conn *sql.Conn) *Migrator {
return &Migrator{Conn: conn}
} }
var _ sqlmigrate.Migrator = (*Migrator)(nil) var _ sqlmigrate.Migrator = (*Migrator)(nil)
@ -41,7 +43,7 @@ func (m *Migrator) ExecDown(ctx context.Context, mig sqlmigrate.Migration, sql s
} }
func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error { func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error {
tx, err := m.DB.BeginTx(ctx, nil) tx, err := m.Conn.BeginTx(ctx, nil)
if err != nil { if err != nil {
return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err) return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err)
} }
@ -61,7 +63,7 @@ func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error {
// Applied returns all applied migrations from the _migrations table. // Applied returns all applied migrations from the _migrations table.
// Returns an empty slice if the table does not exist. // Returns an empty slice if the table does not exist.
func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) { func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) {
rows, err := m.DB.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name") rows, err := m.Conn.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name")
if err != nil { if err != nil {
// SQLite reports "no such table: _migrations" — stable across versions // SQLite reports "no such table: _migrations" — stable across versions
if strings.Contains(err.Error(), "no such table") { if strings.Contains(err.Error(), "no such table") {

View File

@ -2,6 +2,7 @@
// using database/sql with github.com/microsoft/go-mssqldb. // using database/sql with github.com/microsoft/go-mssqldb.
// //
// db, err := sql.Open("sqlserver", "sqlserver://user:pass@host:1433?database=mydb") // db, err := sql.Open("sqlserver", "sqlserver://user:pass@host:1433?database=mydb")
// conn, err := db.Conn(ctx)
package msmigrate package msmigrate
import ( import (
@ -15,14 +16,15 @@ import (
"github.com/therootcompany/golib/database/sqlmigrate" "github.com/therootcompany/golib/database/sqlmigrate"
) )
// Migrator implements sqlmigrate.Migrator using a *sql.DB with SQL Server. // Migrator implements sqlmigrate.Migrator using a *sql.Conn with SQL Server.
type Migrator struct { type Migrator struct {
DB *sql.DB Conn *sql.Conn
} }
// New creates a Migrator from the given database handle. // New creates a Migrator from the given connection.
func New(db *sql.DB) *Migrator { // Use db.Conn(ctx) to obtain a *sql.Conn from a *sql.DB.
return &Migrator{DB: db} func New(conn *sql.Conn) *Migrator {
return &Migrator{Conn: conn}
} }
var _ sqlmigrate.Migrator = (*Migrator)(nil) var _ sqlmigrate.Migrator = (*Migrator)(nil)
@ -38,7 +40,7 @@ func (m *Migrator) ExecDown(ctx context.Context, mig sqlmigrate.Migration, sql s
} }
func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error { func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error {
tx, err := m.DB.BeginTx(ctx, nil) tx, err := m.Conn.BeginTx(ctx, nil)
if err != nil { if err != nil {
return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err) return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err)
} }
@ -58,7 +60,7 @@ func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error {
// Applied returns all applied migrations from the _migrations table. // Applied returns all applied migrations from the _migrations table.
// Returns an empty slice if the table does not exist (SQL Server error 208). // Returns an empty slice if the table does not exist (SQL Server error 208).
func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) { func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) {
rows, err := m.DB.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name") rows, err := m.Conn.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name")
if err != nil { if err != nil {
// SQL Server error 208: "Invalid object name '_migrations'" // SQL Server error 208: "Invalid object name '_migrations'"
if msErr, ok := errors.AsType[mssql.Error](err); ok && msErr.Number == 208 { if msErr, ok := errors.AsType[mssql.Error](err); ok && msErr.Number == 208 {

View File

@ -1,12 +1,14 @@
// Package mymigrate implements sqlmigrate.Migrator for MySQL and MariaDB // Package mymigrate implements sqlmigrate.Migrator for MySQL and MariaDB
// using database/sql with github.com/go-sql-driver/mysql. // using database/sql with github.com/go-sql-driver/mysql.
// //
// The *sql.DB must be opened with multiStatements=true in the DSN; // The *sql.Conn must originate from a *sql.DB opened with
// without it, multi-statement migration files will silently execute only // multiStatements=true in the DSN; without it, multi-statement migration
// the first statement. The multiStatements requirement is validated lazily // files will silently execute only the first statement. The
// on the first ExecUp or ExecDown call: // multiStatements requirement is validated lazily on the first ExecUp or
// ExecDown call:
// //
// db, err := sql.Open("mysql", "user:pass@tcp(host:3306)/dbname?multiStatements=true") // db, err := sql.Open("mysql", "user:pass@tcp(host:3306)/dbname?multiStatements=true")
// conn, err := db.Conn(ctx)
// //
// MySQL and MariaDB do not support transactional DDL. Statements like // MySQL and MariaDB do not support transactional DDL. Statements like
// CREATE TABLE and ALTER TABLE cause an implicit commit, so if a migration // CREATE TABLE and ALTER TABLE cause an implicit commit, so if a migration
@ -25,17 +27,18 @@ import (
"github.com/therootcompany/golib/database/sqlmigrate" "github.com/therootcompany/golib/database/sqlmigrate"
) )
// Migrator implements sqlmigrate.Migrator using a *sql.DB with MySQL/MariaDB. // Migrator implements sqlmigrate.Migrator using a *sql.Conn with MySQL/MariaDB.
type Migrator struct { type Migrator struct {
DB *sql.DB Conn *sql.Conn
validated bool validated bool
} }
// New creates a Migrator from the given database handle. // New creates a Migrator from the given connection.
// Use db.Conn(ctx) to obtain a *sql.Conn from a *sql.DB.
// The multiStatements=true DSN requirement is validated lazily on the // The multiStatements=true DSN requirement is validated lazily on the
// first ExecUp or ExecDown call. // first ExecUp or ExecDown call.
func New(db *sql.DB) *Migrator { func New(conn *sql.Conn) *Migrator {
return &Migrator{DB: db} return &Migrator{Conn: conn}
} }
var _ sqlmigrate.Migrator = (*Migrator)(nil) var _ sqlmigrate.Migrator = (*Migrator)(nil)
@ -56,7 +59,7 @@ func (m *Migrator) exec(ctx context.Context, sqlStr string) error {
if !m.validated { if !m.validated {
// Probe for multi-statement support. Without it, migration files // Probe for multi-statement support. Without it, migration files
// that contain more than one statement silently execute only the first. // that contain more than one statement silently execute only the first.
if _, err := m.DB.ExecContext(ctx, "DO 1; DO 1"); err != nil { if _, err := m.Conn.ExecContext(ctx, "DO 1; DO 1"); err != nil {
return fmt.Errorf( return fmt.Errorf(
"%w: mymigrate: migration requires multiStatements=true in the MySQL DSN", "%w: mymigrate: migration requires multiStatements=true in the MySQL DSN",
sqlmigrate.ErrExecFailed, sqlmigrate.ErrExecFailed,
@ -65,7 +68,7 @@ func (m *Migrator) exec(ctx context.Context, sqlStr string) error {
m.validated = true m.validated = true
} }
tx, err := m.DB.BeginTx(ctx, nil) tx, err := m.Conn.BeginTx(ctx, nil)
if err != nil { if err != nil {
return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err) return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err)
} }
@ -85,7 +88,7 @@ func (m *Migrator) exec(ctx context.Context, sqlStr string) error {
// Applied returns all applied migrations from the _migrations table. // Applied returns all applied migrations from the _migrations table.
// Returns an empty slice if the table does not exist (MySQL error 1146). // Returns an empty slice if the table does not exist (MySQL error 1146).
func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) { func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) {
rows, err := m.DB.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name") rows, err := m.Conn.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name")
if err != nil { if err != nil {
if mysqlErr, ok := errors.AsType[*mysql.MySQLError](err); ok && mysqlErr.Number == 1146 { if mysqlErr, ok := errors.AsType[*mysql.MySQLError](err); ok && mysqlErr.Number == 1146 {
return nil, nil return nil, nil