mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-18 02:26:11 +00:00
fix(database): stop noisy per-startup errors in the Postgres server log
Two statements failed server-side on every panel start after a SQLite to Postgres migration, flooding the postgres log even though the Go side suppressed them: - resyncPostgresSequences issued SELECT MAX(id) against client_inbounds, whose composite primary key has no id column; Postgres validates the SELECT list at parse time, so the WHERE pg_get_serial_sequence(...) guard never got a chance to no-op it. Skip models whose GORM schema maps no id column before issuing the statement. - AutoMigrate detects existing columns via information_schema filtered by table_catalog = CURRENT_DATABASE(), which misdetects on some setups and re-issues ALTER TABLE ... ADD for columns that already exist. HasColumn/ HasIndex query without that filter and are reliable (the existing duplicate-column suppressor depends on exactly that), so skip AutoMigrate outright when the table, every column, and every index already exist. Closes #5665
This commit is contained in:
@@ -270,19 +270,14 @@ func resetPostgresSequences(dst *gorm.DB) error {
|
||||
return resyncPostgresSequences(dst, migrationModels())
|
||||
}
|
||||
|
||||
// resyncPostgresSequences sets each model's id sequence to MAX(id) so the next
|
||||
// auto-increment INSERT won't collide with an existing row. Table names are
|
||||
// resolved from the models themselves (not hardcoded), so they always match the
|
||||
// migrated tables. The statement is a no-op for tables without an id sequence
|
||||
// (e.g. composite-PK tables), and idempotent on a healthy DB, so it is safe to
|
||||
// run both after migration and on every Postgres startup.
|
||||
// resyncPostgresSequences sets each model's id sequence to MAX(id); idempotent. Id-less
|
||||
// composite-PK tables are skipped — Postgres rejects MAX(id) at parse time and logs it (#5665).
|
||||
func resyncPostgresSequences(db *gorm.DB, models []any) error {
|
||||
for _, m := range models {
|
||||
stmt := &gorm.Statement{DB: db}
|
||||
if err := stmt.Parse(m); err != nil {
|
||||
t, ok := tableWithIdColumn(db, m)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
t := stmt.Table
|
||||
// t comes from the trusted model set parsed by GORM, not user input, so
|
||||
// interpolating it as an identifier is safe. We ignore errors per-table.
|
||||
_ = db.Exec(
|
||||
@@ -293,3 +288,16 @@ func resyncPostgresSequences(db *gorm.DB, models []any) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// tableWithIdColumn resolves a model's table name and reports whether its GORM
|
||||
// schema maps an "id" database column.
|
||||
func tableWithIdColumn(db *gorm.DB, m any) (string, bool) {
|
||||
stmt := &gorm.Statement{DB: db}
|
||||
if err := stmt.Parse(m); err != nil {
|
||||
return "", false
|
||||
}
|
||||
if stmt.Schema == nil || stmt.Schema.LookUpField("id") == nil {
|
||||
return "", false
|
||||
}
|
||||
return stmt.Table, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user