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.
This commit is contained in:
CJstate authored and Kristoffer Dalby committed 2026-11-06 10:04:31 +01:00
1 parent 0bdc332886
commit 3bb5789d24
2 files changed
+28 -8

No files matched your search

+19 -5
View File
@@ -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.
//
+9 -3
View File
@@ -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)