[PR #6592] [MERGED] fix(database): keep the legacy tag cleanup from colliding with an existing tag #6367

Closed
opened 2026-11-02 04:08:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6592
Author: @n0ctal
Created: 9/17/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-pg-migration-requirements-fix


📝 Commits (2)

  • 43870fc fix(database): avoid legacy inbound tag cleanup collisions
  • 11a4f2a test(database): assert the legacy tag cleanup keeps the migration green

📊 Changes

2 files changed (+69 additions, -7 deletions)

View changed files

📝 internal/web/service/inbound_migration.go (+13 -3)
📝 internal/web/service/inbound_migration_test.go (+56 -4)

📄 Description

Summary

The legacy 0.0.0.0: tag cleanup in MigrationRequirements can rewrite an
inbound's tag onto one another inbound already holds. inbounds.tag is
unique, so the statement fails — and because the cleanup shares a transaction
with every other requirement, that failure rolls all of them back on every
boot.

Why

tagCleanup := `UPDATE inbounds
    SET tag = REPLACE(tag, '0.0.0.0:', '')
    WHERE INSTR(tag, '0.0.0.0:') > 0;`

An install carrying both inbound-0.0.0.0:30004 and inbound-30004 — the
second created after the address was dropped from new tags — makes this
statement collapse the first onto the second. model.Inbound.Tag is
gorm:"unique", so the UPDATE returns a unique violation.

MigrationRequirements returns on that error, which abandons the transaction
holding the client-traffics backfill, the share-address normalisation and the
rest. MigrateDB only logs it, so the panel starts, the migrations silently
do not apply, and the same thing happens on the next boot.

Rows whose cleaned tag is already taken are now left alone. They keep a
cosmetically legacy tag, which is what the previous state already was, and
every other requirement commits.

Scope

  • MigrationRequirements: both the SQLite and the PostgreSQL form of the
    cleanup skip rows whose cleaned tag another row already holds.
  • TestMigrationRequirements_SkipsLegacyZeroAddrTagCollision seeds the
    colliding pair and asserts the legacy row is untouched.

Validation

  • go build ./... and go vet ./internal/web/service/ are clean.
  • go test ./internal/web/service/ -run TestMigrationRequirements passes on
    SQLite and against a real PostgreSQL 16 — the Postgres form needs
    UPDATE inbounds AS i for the correlated subquery, so it is worth running
    on both.
  • Restoring the unguarded statement makes the test fail with
    MigrationRequirements: UNIQUE constraint failed: inbounds.tag.

The second commit strengthens that test. As first written it asserted only
that the colliding tag was left unchanged — which the unguarded cleanup also
produces, since the UPDATE fails and the row stays as it was. Both states are
identical from the outside; what differs is whether the migration completed.
The test now asserts the call itself succeeds, which is the difference that
matters.

Risk

Low. The change narrows one UPDATE. An install with no colliding tags is
unaffected, and a colliding one keeps the tag it already had.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6592 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 9/17/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-pg-migration-requirements-fix` --- ### 📝 Commits (2) - [`43870fc`](https://github.com/MHSanaei/3x-ui/commit/43870fc20ccb745e2e12a34cd634dc1e29f5e42e) fix(database): avoid legacy inbound tag cleanup collisions - [`11a4f2a`](https://github.com/MHSanaei/3x-ui/commit/11a4f2a0adbd4a38fe0794b9264b64ba7eed4ffa) test(database): assert the legacy tag cleanup keeps the migration green ### 📊 Changes **2 files changed** (+69 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/inbound_migration.go` (+13 -3) 📝 `internal/web/service/inbound_migration_test.go` (+56 -4) </details> ### 📄 Description ## Summary The legacy `0.0.0.0:` tag cleanup in `MigrationRequirements` can rewrite an inbound's tag onto one another inbound already holds. `inbounds.tag` is unique, so the statement fails — and because the cleanup shares a transaction with every other requirement, that failure rolls all of them back on every boot. ## Why ```go tagCleanup := `UPDATE inbounds SET tag = REPLACE(tag, '0.0.0.0:', '') WHERE INSTR(tag, '0.0.0.0:') > 0;` ``` An install carrying both `inbound-0.0.0.0:30004` and `inbound-30004` — the second created after the address was dropped from new tags — makes this statement collapse the first onto the second. `model.Inbound.Tag` is `gorm:"unique"`, so the UPDATE returns a unique violation. `MigrationRequirements` returns on that error, which abandons the transaction holding the client-traffics backfill, the share-address normalisation and the rest. `MigrateDB` only logs it, so the panel starts, the migrations silently do not apply, and the same thing happens on the next boot. Rows whose cleaned tag is already taken are now left alone. They keep a cosmetically legacy tag, which is what the previous state already was, and every other requirement commits. ## Scope - `MigrationRequirements`: both the SQLite and the PostgreSQL form of the cleanup skip rows whose cleaned tag another row already holds. - `TestMigrationRequirements_SkipsLegacyZeroAddrTagCollision` seeds the colliding pair and asserts the legacy row is untouched. ## Validation - `go build ./...` and `go vet ./internal/web/service/` are clean. - `go test ./internal/web/service/ -run TestMigrationRequirements` passes on SQLite and against a real PostgreSQL 16 — the Postgres form needs `UPDATE inbounds AS i` for the correlated subquery, so it is worth running on both. - Restoring the unguarded statement makes the test fail with `MigrationRequirements: UNIQUE constraint failed: inbounds.tag`. The second commit strengthens that test. As first written it asserted only that the colliding tag was left unchanged — which the unguarded cleanup also produces, since the UPDATE fails and the row stays as it was. Both states are identical from the outside; what differs is whether the migration completed. The test now asserts the call itself succeeds, which is the difference that matters. ## Risk Low. The change narrows one UPDATE. An install with no colliding tags is unaffected, and a colliding one keeps the tag it already had. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:16 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6367
No description provided.