Skip to content

[management] fix index migration on mysql#6408

Open
pascal-fischer wants to merge 1 commit into
mainfrom
fix/mysql-index-migration
Open

[management] fix index migration on mysql#6408
pascal-fischer wants to merge 1 commit into
mainfrom
fix/mysql-index-migration

Conversation

@pascal-fischer

@pascal-fischer pascal-fischer commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Describe your changes

Following the PR from #5182

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • This change does not modify the public API, gRPC protocols, functionality behavior, CLI / service flags, or introduce a new feature — OR I have discussed it with the NetBird team beforehand (link the issue / Slack thread in the description). See CONTRIBUTING.md.

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • Refactor
    • Enhanced data validation and consistency measures to improve database integrity and ensure reliable unique key constraints.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR updates the Peer.Key field definition in the peer struct to include explicit GORM metadata tags enforcing a maximum size of 255 characters and applying a unique index constraint, replacing prior migration-time uniqueness handling.

Changes

Peer Key GORM Constraint Enforcement

Layer / File(s) Summary
Peer Key field GORM tags
management/server/peer/peer.go
The Peer.Key field now declares explicit GORM tags size:255;uniqueIndex to enforce database-level size and uniqueness constraints directly in the struct definition.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

  • netbirdio/netbird#5053: Adjusts migrations and indexes for the same peers.key uniqueness constraint, including duplicate-key cleanup logic.

Poem

🐰 A hop, a skip, through database lanes,
One field now shines with GORM's constraints,
Size and uniqueness hand in hand,
The Peer struct's stronger than before,
Migrations bow to schema's command!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing an index migration issue on MySQL by adding a size constraint to the Peer struct's Key field.
Description check ✅ Passed The description follows the required template with all critical sections filled: bug fix checkbox marked, documentation exemption explained, and reference to related PR provided for context.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mysql-index-migration

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
management/server/peer/peer.go (1)

28-28: ⚡ Quick win

Consider using 191 characters for broader MySQL compatibility.

While size:255 works for WireGuard public keys (which are 44 characters), MySQL databases using utf8mb4 without innodb_large_prefix support have a 767-byte index key limit. With utf8mb4's 4-byte-per-character encoding, this translates to a maximum of 191 characters (191 × 4 = 764 bytes).

Although 255 characters may work with modern MySQL configurations that have large prefix support enabled, using size:191 would provide better compatibility with older MySQL versions and stricter configurations.

Since WireGuard public keys are only 44 characters, reducing the size to 191 would not impact functionality while improving database portability.

🔧 Suggested change for better MySQL compatibility
-	Key string `gorm:"size:255;uniqueIndex"`
+	Key string `gorm:"size:191;uniqueIndex"`
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@management/server/peer/peer.go` at line 28, The GORM tag on the Peer struct's
Key field uses size:255 which can exceed MySQL utf8mb4 indexed-key limits;
change the struct field Key's gorm tag to use size:191 while keeping the
uniqueIndex option (i.e., update the `Key string` field tag in peer.go from
`gorm:"size:255;uniqueIndex"` to `gorm:"size:191;uniqueIndex"`) so indexes
remain portable without affecting WireGuard key storage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@management/server/peer/peer.go`:
- Line 28: The GORM tag on the Peer struct's Key field uses size:255 which can
exceed MySQL utf8mb4 indexed-key limits; change the struct field Key's gorm tag
to use size:191 while keeping the uniqueIndex option (i.e., update the `Key
string` field tag in peer.go from `gorm:"size:255;uniqueIndex"` to
`gorm:"size:191;uniqueIndex"`) so indexes remain portable without affecting
WireGuard key storage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 27fddde2-3c72-4cc5-b8be-b4d3f970edc5

📥 Commits

Reviewing files that changed from the base of the PR and between d770376 and 10a2273.

📒 Files selected for processing (1)
  • management/server/peer/peer.go

@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown

Release artifacts

Built for PR head 10a2273 in workflow run #15769.

Artifact Link
All release artifacts Download
Linux packages Download
Windows packages Download
macOS packages Download
UI artifacts Download
UI macOS artifacts Download

GHCR images (amd64)

This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant