mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-25 05:08:00 +00:00
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.
87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
// Package msmigrate implements sqlmigrate.Migrator for Microsoft SQL Server
|
|
// using database/sql with github.com/microsoft/go-mssqldb.
|
|
//
|
|
// db, err := sql.Open("sqlserver", "sqlserver://user:pass@host:1433?database=mydb")
|
|
// conn, err := db.Conn(ctx)
|
|
package msmigrate
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
mssql "github.com/microsoft/go-mssqldb"
|
|
|
|
"github.com/therootcompany/golib/database/sqlmigrate"
|
|
)
|
|
|
|
// Migrator implements sqlmigrate.Migrator using a *sql.Conn with SQL Server.
|
|
type Migrator struct {
|
|
Conn *sql.Conn
|
|
}
|
|
|
|
// New creates a Migrator from the given connection.
|
|
// Use db.Conn(ctx) to obtain a *sql.Conn from a *sql.DB.
|
|
func New(conn *sql.Conn) *Migrator {
|
|
return &Migrator{Conn: conn}
|
|
}
|
|
|
|
var _ sqlmigrate.Migrator = (*Migrator)(nil)
|
|
|
|
// ExecUp runs the up migration SQL inside a transaction.
|
|
func (m *Migrator) ExecUp(ctx context.Context, mig sqlmigrate.Migration, sql string) error {
|
|
return m.execInTx(ctx, sql)
|
|
}
|
|
|
|
// ExecDown runs the down migration SQL inside a transaction.
|
|
func (m *Migrator) ExecDown(ctx context.Context, mig sqlmigrate.Migration, sql string) error {
|
|
return m.execInTx(ctx, sql)
|
|
}
|
|
|
|
func (m *Migrator) execInTx(ctx context.Context, sqlStr string) error {
|
|
tx, err := m.Conn.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err)
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
if _, err := tx.ExecContext(ctx, sqlStr); err != nil {
|
|
return fmt.Errorf("%w: exec: %w", sqlmigrate.ErrExecFailed, err)
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return fmt.Errorf("%w: commit: %w", sqlmigrate.ErrExecFailed, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Applied returns all applied migrations from the _migrations table.
|
|
// Returns an empty slice if the table does not exist (SQL Server error 208).
|
|
func (m *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) {
|
|
rows, err := m.Conn.QueryContext(ctx, "SELECT id, name FROM _migrations ORDER BY name")
|
|
if err != nil {
|
|
// SQL Server error 208: "Invalid object name '_migrations'"
|
|
if msErr, ok := errors.AsType[mssql.Error](err); ok && msErr.Number == 208 {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("%w: %w", sqlmigrate.ErrQueryApplied, err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var applied []sqlmigrate.Migration
|
|
for rows.Next() {
|
|
var a sqlmigrate.Migration
|
|
if err := rows.Scan(&a.ID, &a.Name); err != nil {
|
|
return nil, fmt.Errorf("%w: scanning row: %w", sqlmigrate.ErrQueryApplied, err)
|
|
}
|
|
applied = append(applied, a)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("%w: reading rows: %w", sqlmigrate.ErrQueryApplied, err)
|
|
}
|
|
|
|
return applied, nil
|
|
}
|