gastownhall/beads · error

ensure global db: failed to create %s: %w

Error message

ensure global db: failed to create %s: %w

What it means

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.

Source

Thrown at internal/doltserver/doltserver.go:1553

	db, err := sql.Open("mysql", dsn)
	if err != nil {
		return fmt.Errorf("ensure global db: failed to open connection: %w", err)
	}
	defer db.Close()
	db.SetMaxOpenConns(1)
	db.SetConnMaxLifetime(10 * time.Second)

	if err := db.PingContext(ctx); err != nil {
		return fmt.Errorf("ensure global db: server not reachable: %w", err)
	}

	// CREATE DATABASE IF NOT EXISTS is idempotent — safe on every call.
	// GlobalDatabaseName is a constant ("beads_global"), not user input.
	_, err = db.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", GlobalDatabaseName)) //nolint:gosec // G201: constant database name
	if err != nil {
		errLower := strings.ToLower(err.Error())
		if !strings.Contains(errLower, "database exists") && !strings.Contains(errLower, "1007") {
			return fmt.Errorf("ensure global db: failed to create %s: %w", GlobalDatabaseName, err)
		}
	}

	return nil
}

// FlushWorkingSet connects to the running Dolt server and commits any uncommitted
// working set changes across all databases. This prevents data loss when the server
// is about to be stopped or restarted. Returns nil if there's nothing to flush or
// if the server is not reachable (best-effort).
func FlushWorkingSet(host string, port int) error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	dsn := doltutil.ServerDSN{
		Host: host,
		Port: port,
		User: "root",

View on GitHub (pinned to 71377f2769)

Solutions

  1. Grant the connecting user CREATE privilege: connect as admin and run GRANT CREATE ON *.* TO 'user'@'%'; FLUSH PRIVILEGES;
  2. Check server-side disk space (df -h on the host running dolt sql-server)
  3. Check the server log for Dolt storage errors at the time of the CREATE
  4. Retry if the wrapped error is a timeout — transient 10s-window failures clear under lower load
  5. Verify the error is not a masked 'already exists' race by running SHOW DATABASES LIKE 'beads_global'

Example fix

// before
// error: ensure global db: failed to create beads_global: Error 1044: Access denied for user 'beads'@'%'
// after: grant the privilege (as admin on the dolt server)
// $ mysql -h <host> -u root -e "GRANT CREATE ON *.* TO 'beads'@'%'; FLUSH PRIVILEGES;"
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify the connecting user can create databases BEFORE calling EnsureGlobalDatabase
db, err := sql.Open("mysql", dsn)
if err == nil {
    defer db.Close()
    var have bool
    _ = db.QueryRow("SELECT COUNT(*)>0 FROM information_schema.SCHEMA_PRIVILEGES WHERE PRIVILEGE_TYPE='CREATE' AND GRANTEE LIKE ?", "'"+user+"'%").Scan(&have)
    if !have {
        return errors.New("user lacks CREATE privilege; GRANT CREATE ON *.* TO user@'%'")
    }
}

Try / catch

if err := doltserver.EnsureGlobalDatabase(host, port, user, password); err != nil {
    if strings.Contains(err.Error(), "failed to create "+doltserver.GlobalDatabaseName) {
        // permission or server-side storage failure: surface actionable guidance
        return fmt.Errorf("cannot create %s: %w\nCheck: GRANT CREATE; server disk space; server log", doltserver.GlobalDatabaseName, err)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/0e74a26f9e9f0ec5. Report an issue: GitHub.