golang-migrate/migrate · error

no config

Error message

no config

What it means

ErrNilConfig in database/mongodb is returned by WithInstance, WithConnection and extractCustomQueryParams when the *Config argument is nil. The Mongo driver requires a Config to hold DatabaseName and options like transaction mode and advisory lock settings. It is a programming/configuration guard, not a server error.

Source

Thrown at database/mongodb/mongodb.go:40

func init() {
	db := Mongo{}
	database.Register("mongodb", &db)
	database.Register("mongodb+srv", &db)
}

var DefaultMigrationsCollection = "schema_migrations"

const DefaultLockingCollection = "migrate_advisory_lock" // the collection to use for advisory locking by default.
const lockKeyUniqueValue = 0                             // the unique value to lock on. If multiple clients try to insert the same key, it will fail (locked).
const DefaultLockTimeout = 15                            // the default maximum time to wait for a lock to be released.
const DefaultLockTimeoutInterval = 10                    // the default maximum intervals time for the locking timout.
const DefaultAdvisoryLockingFlag = true                  // the default value for the advisory locking feature flag. Default is true.
const LockIndexName = "lock_unique_key"                  // the name of the index which adds unique constraint to the locking_key field.
const contextWaitTimeout = 5 * time.Second               // how long to wait for the request to mongo to block/wait for.

var (
	ErrNoDatabaseName            = fmt.Errorf("no database name")
	ErrNilConfig                 = fmt.Errorf("no config")
	ErrLockTimeoutConfigConflict = fmt.Errorf("both x-advisory-lock-timeout-interval and x-advisory-lock-timout-interval were specified")
)

type Mongo struct {
	client   *mongo.Client
	db       *mongo.Database
	config   *Config
	isLocked atomic.Bool
}

type Locking struct {
	CollectionName string
	Timeout        int
	Enabled        bool
	Interval       int
}
type Config struct {
	DatabaseName         string

View on GitHub (pinned to 01a9643f14)

Solutions

  1. Pass a non-nil *mongodb.Config to WithInstance/WithConnection
  2. Use mongodb.WithConnection with a config built from WithConfig-style construction
  3. Nil-check the config in your bootstrap code before creating the driver

Example fix

// before
d, err := mongodb.WithInstance(ctx, client, nil)
// after
cfg := &mongodb.Config{DatabaseName: "mydb"}
d, err := mongodb.WithInstance(ctx, client, cfg)
Defensive patterns

Strategy: validation

Validate before calling

if cfg == nil {
    return fmt.Errorf("mongodb migration requires non-nil *mongodb.Config")
}

Type guard

func hasMongoConfig(cfg *mongodb.Config) bool { return cfg != nil }

Try / catch

if err != nil {
    if errors.Is(err, mongodb.ErrNilConfig) {
        return fmt.Errorf("driver bootstrap bug: nil mongo config")
    }
    return err
}

Prevention

When it happens

Trigger: mongodb.WithInstance(client, nil); mongodb.WithConnection(ctx, db, nil); nil config reaching extractCustomQueryParams via Open.

Common situations: Typed-nil *mongodb.Config passed in, wrapper helpers that drop nil configs, refactors where WithConfig was removed but call sites still pass nil.

Related errors


AI-assisted analysis of golang-migrate/migrate@01a9643f14 (2026-09-02). Data as JSON: /api/errors/f7cb3180378faea6. Report an issue: GitHub.