gastownhall/beads · error

dropping the wisp_dependencies primary key for the 0058 repa

Error message

dropping the wisp_dependencies primary key for the 0058 repair: %w

What it means

Wraps a failure when dropping the legacy primary key of wisp_dependencies (`ALTER TABLE wisp_dependencies DROP PRIMARY KEY`) during the 0058 repair. This is required before the final surrogate `id` key can take over. The DDL failed after schemaHasPrimaryKey confirmed a PK exists, aborting the repair.

Source

Thrown at internal/storage/schema/wisp_dep_forward_repair.go:355

		present, err := schemaConstraintExists(ctx, db, wispDepTable, c.name)
		if err != nil {
			return err
		}
		if !present {
			continue
		}
		if _, err := db.ExecContext(ctx, "ALTER TABLE wisp_dependencies DROP FOREIGN KEY "+c.name); err != nil {
			return fmt.Errorf("dropping %s for the 0058 repair: %w", c.name, err)
		}
	}

	hasPK, err := schemaHasPrimaryKey(ctx, db, wispDepTable)
	if err != nil {
		return err
	}
	if hasPK {
		if _, err := db.ExecContext(ctx, "ALTER TABLE wisp_dependencies DROP PRIMARY KEY"); err != nil {
			return fmt.Errorf("dropping the wisp_dependencies primary key for the 0058 repair: %w", err)
		}
	}

	hasGenerated, err := schemaColumnExists(ctx, db, wispDepTable, "depends_on_id")
	if err != nil {
		return err
	}
	if hasGenerated {
		if _, err := db.ExecContext(ctx, "ALTER TABLE wisp_dependencies DROP COLUMN depends_on_id"); err != nil {
			return fmt.Errorf("dropping wisp_dependencies.depends_on_id for the 0058 repair: %w", err)
		}
	}
	return nil
}

// ensureWispDepSurrogateKey adds the final shape's id column and primary key.
//
// It is added BEFORE deduplication on purpose. The natural identity of a row

View on GitHub (pinned to 71377f2769)

Solutions

  1. Check the wrapped cause: if an FK references the PK, drop the dependent FK first or confirm the repair's earlier FK-drop steps ran.
  2. Re-run the repair — steps are individually guarded so a partial pass resumes safely.
  3. Grant the migration user ALTER privilege.
  4. Ensure no other sessions hold open transactions on wisp_dependencies during the migration.

Example fix

// before: DROP PRIMARY KEY fails — FK from child table still references old PK
// after: resume full guarded repair so FK drops precede the PK drop
if err := repairWispDependenciesForwardShape(ctx, db); err != nil {
    return fmt.Errorf("resume repair after clearing dependent FKs: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

// ensure no foreign keys (from any table) still reference wisp_dependencies' PK
rows, err := db.QueryContext(ctx, `SELECT TABLE_NAME, CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME = 'wisp_dependencies'`)
if err != nil { log.Fatalf("cannot check FK references: %v", err) }
for rows.Next() {
    var t, c string
    _ = rows.Scan(&t, &c)
    log.Printf("dependent FK: %s.%s — must be dropped before PK drop", t, c)
}
rows.Close()

Try / catch

if err := repairWispDependenciesForwardShape(ctx, db); err != nil {
    if strings.Contains(err.Error(), "dropping the wisp_dependencies primary key") {
        // a dependent FK likely remains; inspect and drop it, then re-run the guarded repair
        return fmt.Errorf("clear dependent FKs before resuming: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: dropWispDepLegacyShape executes `ALTER TABLE wisp_dependencies DROP PRIMARY KEY` and it fails — the PK column is referenced by a foreign key from another table (MySQL refuses), insufficient privileges, lock contention, or Dolt engine restriction.

Common situations: Another table still holds a FK into the old PK column; repair run partially then resumed with leftover constraints; DB user lacking ALTER; long-running transactions blocking the metadata lock.

Related errors


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