mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6196] [CLOSED] feat(database): opt-in uniqueness enforcement for non-empty sub_id #6157
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6157
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6196
Author: @n0ctal
Created: 8/10/2026
Status: ❌ Closed
Base:
main← Head:upstream-unique-subid-gate📝 Commits (3)
93fc85cdatabase: gate unique non-empty sub_id enforcement47e2113fix(database): enforce unique subids after seeders315cff3Merge branch 'main' into upstream-unique-subid-gate📊 Changes
2 files changed (+366 additions, -1 deletions)
View changed files
📝
internal/database/db.go(+163 -1)➕
internal/database/unique_subid_test.go(+203 -0)📄 Description
Summary
Add an opt-in
XUI_ENFORCE_UNIQUE_SUBID=1gate that enforces uniqueness of non-emptysub_idwith a partial unique index. Default behaviour is unchanged and only audits.Why
sub_idcarries a plain index, so duplicates are accepted. Two clients sharing a subscription id are served the same subscription content, which is either an aggregation the operator intended or a mistake nobody is told about — and today there is no way to express the difference.The empty value has to stay duplicable: it is the normal state for clients with no subscription. That rules out a plain unique index and is why this needs a partial one.
Scope
XUI_ENFORCE_UNIQUE_SUBID=1: a preflight scan reports existing duplicates before anything is created, then a partial unique index over non-empty values, then a structural check of the resulting index definition rather than only its name.WHERE sub_id IS NULL) would otherwise pass as if enforcement were in place. Enforcement fails closed if the definition does not match.Validation
go test ./...green;go test -race ./internal/database -run TestUniqueSubIDgreen.Not proven: no migration was run against a copy of a large production database containing real duplicates. The fail-closed path is covered by tests, not by that rehearsal.
Risk
Off by default. An operator enabling it is told about existing duplicates before the index is attempted, so enforcement cannot half-apply against dirty data.
Relation to #6065
#6065 establishes that sharing one
sub_idacross client records is legitimate and is how a subscription aggregates several entries. That is why this is opt-in and off by default: enabling it forbids exactly that aggregation.The analysis in that issue also notes that the existing service-level checks are incomplete, which is the gap this closes for operators who do want one client per
sub_id— but at the database level, where a check cannot be bypassed by a different write path, rather than by adding more service-level checks.If you would rather this never becomes enforceable at the schema level because aggregation is the intended contract, say so and I will close it.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.