{"record":{"id":"71e68e1871df7710","repo":"golang-migrate/migrate","slug":"no-database-name-71e68e","errorCode":null,"errorMessage":"no database name","messagePattern":"no database name","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"database/mongodb/mongodb.go","lineNumber":39,"sourceCode":"\nfunc init() {\n\tdb := Mongo{}\n\tdatabase.Register(\"mongodb\", &db)\n\tdatabase.Register(\"mongodb+srv\", &db)\n}\n\nvar DefaultMigrationsCollection = \"schema_migrations\"\n\nconst DefaultLockingCollection = \"migrate_advisory_lock\" // the collection to use for advisory locking by default.\nconst lockKeyUniqueValue = 0                             // the unique value to lock on. If multiple clients try to insert the same key, it will fail (locked).\nconst DefaultLockTimeout = 15                            // the default maximum time to wait for a lock to be released.\nconst DefaultLockTimeoutInterval = 10                    // the default maximum intervals time for the locking timout.\nconst DefaultAdvisoryLockingFlag = true                  // the default value for the advisory locking feature flag. Default is true.\nconst LockIndexName = \"lock_unique_key\"                  // the name of the index which adds unique constraint to the locking_key field.\nconst contextWaitTimeout = 5 * time.Second               // how long to wait for the request to mongo to block/wait for.\n\nvar (\n\tErrNoDatabaseName            = fmt.Errorf(\"no database name\")\n\tErrNilConfig                 = fmt.Errorf(\"no config\")\n\tErrLockTimeoutConfigConflict = fmt.Errorf(\"both x-advisory-lock-timeout-interval and x-advisory-lock-timout-interval were specified\")\n)\n\ntype Mongo struct {\n\tclient   *mongo.Client\n\tdb       *mongo.Database\n\tconfig   *Config\n\tisLocked atomic.Bool\n}\n\ntype Locking struct {\n\tCollectionName string\n\tTimeout        int\n\tEnabled        bool\n\tInterval       int\n}\ntype Config struct {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/golang-migrate/migrate/blob/01a9643f1475e75bb6d6224ddeaf9d8e2434ca8a/database/mongodb/mongodb.go#L21-L57","documentation":"ErrNoDatabaseName in database/mongodb is returned by WithInstance, Open and WithConnection when the resolved database name (from the MongoDB URL path or Config.DatabaseName) is empty. MongoDB migrations need a target database to store the schema_migrations collection; without a name the driver cannot determine where to migrate.","triggerScenarios":"Opening \"mongodb://host:27017/\" with no database in the path and no DatabaseName in the config; WithInstance with a *mongo.Database built from an empty name; WithConnection where config.DatabaseName is \"\".","commonSituations":"Connection string missing the trailing database segment (mongodb://host vs mongodb://host/mydb), env-based DSNs with an unset DB name, renaming a database and leaving the env var empty.","solutions":["Append the database name to the MongoDB URL path: mongodb://host:27017/mydb","Set Config.DatabaseName when using WithInstance/WithConnection","Validate the DSN env var before constructing the driver"],"exampleFix":"// before\nm, err := migrate.New(\"mongodb://localhost:27017\", \"file://migrations\")\n// after\nm, err := migrate.New(\"mongodb://localhost:27017/mydb\", \"file://migrations\")","handlingStrategy":"validation","validationCode":"u, _ := url.Parse(dsn)\nif strings.Trim(u.Path, \"/\") == \"\" {\n    return fmt.Errorf(\"mongodb DSN must include a database name: mongodb://host/db\")\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n    if errors.Is(err, mongodb.ErrNoDatabaseName) {\n        return fmt.Errorf(\"check MONGO_URL: missing database in path\")\n    }\n    return err\n}","preventionTips":["Always end MongoDB DSNs with /dbname","Validate the DSN env var at startup before building drivers","Set DatabaseName in Config when not using URL-based construction"],"tags":["go","golang-migrate","mongodb","config","missing-database-name"],"backgroundTag":"missing-database-name","analyzedSha":"01a9643f1475e75bb6d6224ddeaf9d8e2434ca8a","analyzedAt":"2026-09-02T19:38:29.671Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}