From 3bb5789d24400b757122c437057a7a126aba2f6b Mon Sep 17 00:00:00 2001 From: CJstate <1507965754@qq.com> Date: Tue, 3 Nov 2026 18:38:37 +0800 Subject: [PATCH] db: reject pre-0.23 databases that lack a migrations table checkMinimumMigration treated any database without a migrations table as fresh. Databases created before gormigrate (0.23) also lack that table, so they were accepted as fresh, let InitSchema run against the legacy schema, and stamp SCHEMA_INIT plus every migration ID without running any migration. Only treat a missing migrations table as fresh when no legacy or current core table exists; otherwise return the too-old upgrade error. Add a test case covering the legacy-schema database. Closes #3648. --- hscontrol/db/versioncheck.go | 24 +++++++++++++++++++----- hscontrol/db/versioncheck_test.go | 12 +++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/hscontrol/db/versioncheck.go b/hscontrol/db/versioncheck.go index c2506a1f5..b63be3628 100644 --- a/hscontrol/db/versioncheck.go +++ b/hscontrol/db/versioncheck.go @@ -188,6 +188,16 @@ func isDev(version string) bool { // which only exists since 0.29.0 and is skipped for dev builds. func checkMinimumMigration(db *gorm.DB) error { if !db.Migrator().HasTable("migrations") { + // Databases created before gormigrate was introduced (0.23) have no + // migrations table either. Only treat the database as fresh when no + // legacy or current core table exists; otherwise InitSchema would + // stamp the schema as initialised instead of running migrations. + for _, table := range []string{"users", "machines", "nodes"} { + if db.Migrator().HasTable(table) { + return tooOldUpgradeError() + } + } + return nil } @@ -212,16 +222,20 @@ func checkMinimumMigration(db *gorm.DB) error { } if found == 0 { - return fmt.Errorf( - "upgrade to the latest v0.29.x release first, "+ - "release page: https://github.com/juanfont/headscale/releases: %w", - errDatabaseTooOld, - ) + return tooOldUpgradeError() } return nil } +func tooOldUpgradeError() error { + return fmt.Errorf( + "upgrade to the latest v0.29.x release first, "+ + "release page: https://github.com/juanfont/headscale/releases: %w", + errDatabaseTooOld, + ) +} + // checkVersionUpgradePath verifies that the running headscale version // is compatible with the version that last used this database. // diff --git a/hscontrol/db/versioncheck_test.go b/hscontrol/db/versioncheck_test.go index 4e391351d..2bd45d7ba 100644 --- a/hscontrol/db/versioncheck_test.go +++ b/hscontrol/db/versioncheck_test.go @@ -514,15 +514,17 @@ func checkVersionUpgradePathFromVersions(db *gorm.DB, currentVersion string) err func TestCheckMinimumMigration(t *testing.T) { tests := []struct { - name string - ids []string // nil means no migrations table - wantErr bool + name string + ids []string // nil means no migrations table + legacyTable string // non-empty creates this legacy/current core table without a migrations table + wantErr bool }{ {name: "no migrations table", ids: nil}, {name: "empty migrations table", ids: []string{}}, {name: "0.29 database", ids: []string{"SCHEMA_INIT", minimumMigrationID}}, {name: "created fresh by this release", ids: []string{"SCHEMA_INIT", "202606181200-recover-null-tags-node-user-id"}}, {name: "0.28 database", ids: []string{"SCHEMA_INIT", "202601121700-migrate-hostinfo-request-tags"}, wantErr: true}, + {name: "legacy database without migrations table", legacyTable: "users", wantErr: true}, } for _, tt := range tests { @@ -530,6 +532,10 @@ func TestCheckMinimumMigration(t *testing.T) { db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{}) require.NoError(t, err) + if tt.legacyTable != "" { + require.NoError(t, db.Exec("CREATE TABLE "+tt.legacyTable+" (id varchar(255) PRIMARY KEY)").Error) + } + if tt.ids != nil { require.NoError(t, db.Exec("CREATE TABLE migrations (id varchar(255) PRIMARY KEY)").Error)