{"record":{"id":"0e74a26f9e9f0ec5","repo":"gastownhall/beads","slug":"ensure-global-db-failed-to-create-s-w","errorCode":null,"errorMessage":"ensure global db: failed to create %s: %w","messagePattern":"ensure global db: failed to create (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/doltserver/doltserver.go","lineNumber":1553,"sourceCode":"\tdb, err := sql.Open(\"mysql\", dsn)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"ensure global db: failed to open connection: %w\", err)\n\t}\n\tdefer db.Close()\n\tdb.SetMaxOpenConns(1)\n\tdb.SetConnMaxLifetime(10 * time.Second)\n\n\tif err := db.PingContext(ctx); err != nil {\n\t\treturn fmt.Errorf(\"ensure global db: server not reachable: %w\", err)\n\t}\n\n\t// CREATE DATABASE IF NOT EXISTS is idempotent — safe on every call.\n\t// GlobalDatabaseName is a constant (\"beads_global\"), not user input.\n\t_, err = db.ExecContext(ctx, fmt.Sprintf(\"CREATE DATABASE IF NOT EXISTS `%s`\", GlobalDatabaseName)) //nolint:gosec // G201: constant database name\n\tif err != nil {\n\t\terrLower := strings.ToLower(err.Error())\n\t\tif !strings.Contains(errLower, \"database exists\") && !strings.Contains(errLower, \"1007\") {\n\t\t\treturn fmt.Errorf(\"ensure global db: failed to create %s: %w\", GlobalDatabaseName, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// FlushWorkingSet connects to the running Dolt server and commits any uncommitted\n// working set changes across all databases. This prevents data loss when the server\n// is about to be stopped or restarted. Returns nil if there's nothing to flush or\n// if the server is not reachable (best-effort).\nfunc FlushWorkingSet(host string, port int) error {\n\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\tdefer cancel()\n\n\tdsn := doltutil.ServerDSN{\n\t\tHost: host,\n\t\tPort: port,\n\t\tUser: \"root\",","sourceCodeStart":1535,"sourceCodeEnd":1571,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/doltserver/doltserver.go#L1535-L1571","documentation":"EnsureGlobalDatabase wraps any failure of 'CREATE DATABASE IF NOT EXISTS `beads_global`' with this message, unless the error indicates the database already exists (error text contains 'database exists' or MySQL error 1007), which is treated as success. So this error means the connection was fine but the server refused the CREATE — almost always a permissions problem or a server-side storage error.","triggerScenarios":"db.ExecContext fails executing CREATE DATABASE on the dolt server: the connecting user lacks CREATE privilege, disk is full on the server, Dolt reports a storage/engine error, or the context's 10s timeout expires mid-DDL.","commonSituations":"Connecting as a limited shared-server user (not root) without CREATE privilege on the server; shared dolt server disk full; Dolt database directory corruption or lock contention; network drop hitting the 10s context deadline during the DDL.","solutions":["Grant the connecting user CREATE privilege: connect as admin and run GRANT CREATE ON *.* TO 'user'@'%'; FLUSH PRIVILEGES;","Check server-side disk space (df -h on the host running dolt sql-server)","Check the server log for Dolt storage errors at the time of the CREATE","Retry if the wrapped error is a timeout — transient 10s-window failures clear under lower load","Verify the error is not a masked 'already exists' race by running SHOW DATABASES LIKE 'beads_global'"],"exampleFix":"// before\n// error: ensure global db: failed to create beads_global: Error 1044: Access denied for user 'beads'@'%'\n// after: grant the privilege (as admin on the dolt server)\n// $ mysql -h <host> -u root -e \"GRANT CREATE ON *.* TO 'beads'@'%'; FLUSH PRIVILEGES;\"","handlingStrategy":"try-catch","validationCode":"// Verify the connecting user can create databases BEFORE calling EnsureGlobalDatabase\ndb, err := sql.Open(\"mysql\", dsn)\nif err == nil {\n    defer db.Close()\n    var have bool\n    _ = db.QueryRow(\"SELECT COUNT(*)>0 FROM information_schema.SCHEMA_PRIVILEGES WHERE PRIVILEGE_TYPE='CREATE' AND GRANTEE LIKE ?\", \"'\"+user+\"'%\").Scan(&have)\n    if !have {\n        return errors.New(\"user lacks CREATE privilege; GRANT CREATE ON *.* TO user@'%'\")\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := doltserver.EnsureGlobalDatabase(host, port, user, password); err != nil {\n    if strings.Contains(err.Error(), \"failed to create \"+doltserver.GlobalDatabaseName) {\n        // permission or server-side storage failure: surface actionable guidance\n        return fmt.Errorf(\"cannot create %s: %w\\nCheck: GRANT CREATE; server disk space; server log\", doltserver.GlobalDatabaseName, err)\n    }\n    return err\n}","preventionTips":["Provision the shared-server user with CREATE privilege at setup time","Connect as a dedicated admin account for initialization steps","Monitor disk space on the dolt server host","Treat 'already exists' (error 1007) as success — EnsureGlobalDatabase already does; do not double-handle it in callers"],"tags":["go","mysql","sql","permissions","create-database"],"backgroundTag":"create-database-permission-denied","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}