mirror of
https://github.com/juanfont/headscale.git
synced 2026-09-17 14:02:07 +09:00
db, sqliteconfig: consolidate query and config helpers
This commit is contained in:
+14
-53
@@ -220,7 +220,7 @@ AND auth_key_id NOT IN (
|
||||
{
|
||||
ID: "202505141324",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
users, err := ListUsers(tx)
|
||||
users, err := ListUsers(tx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listing users: %w", err)
|
||||
}
|
||||
@@ -617,7 +617,7 @@ AND auth_key_id NOT IN (
|
||||
}
|
||||
|
||||
// 2. Load users and nodes to create PolicyManager
|
||||
users, err := ListUsers(tx)
|
||||
users, err := ListUsers(tx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading users for RequestTags migration: %w", err)
|
||||
}
|
||||
@@ -970,59 +970,20 @@ func openDB(cfg types.DatabaseConfig) (*gorm.DB, error) {
|
||||
|
||||
func runMigrations(cfg types.DatabaseConfig, dbConn *gorm.DB, migrations *gormigrate.Gormigrate) error {
|
||||
if cfg.Type == types.DatabaseSqlite {
|
||||
// SQLite: Run migrations step-by-step, only disabling foreign keys when necessary
|
||||
|
||||
// List of migration IDs that require foreign keys to be disabled
|
||||
// These are migrations that perform complex schema changes that GORM cannot handle safely with FK enabled
|
||||
// NO NEW MIGRATIONS SHOULD BE ADDED HERE. ALL NEW MIGRATIONS MUST RUN WITH FOREIGN KEYS ENABLED.
|
||||
migrationsRequiringFKDisabled := map[string]bool{
|
||||
"202501221827": true, // Route table automigration with FK constraint issues
|
||||
"202501311657": true, // PreAuthKey table automigration with FK constraint issues
|
||||
// Add other migration IDs here as they are identified to need FK disabled
|
||||
// SQLite: Run the early migrations that GORM cannot handle safely with
|
||||
// foreign keys enabled (route and pre-auth-key automigrations) with FK
|
||||
// disabled, then run everything else with FK enabled.
|
||||
//
|
||||
// NO NEW MIGRATIONS SHOULD RUN WITH FK DISABLED. As of 2025-07-02, all
|
||||
// new migrations must run with foreign keys enabled via the
|
||||
// migrations.Migrate() call below.
|
||||
if err := dbConn.Exec("PRAGMA foreign_keys = OFF").Error; err != nil { //nolint:noinlineerr
|
||||
return fmt.Errorf("disabling foreign keys: %w", err)
|
||||
}
|
||||
|
||||
// Get all migration IDs in order from the actual migration definitions
|
||||
// Only IDs that are in the migrationsRequiringFKDisabled map will be processed with FK disabled
|
||||
// any other new migrations are ran after.
|
||||
migrationIDs := []string{
|
||||
// v0.25.0
|
||||
"202501221827",
|
||||
"202501311657",
|
||||
"202502070949",
|
||||
|
||||
// v0.26.0
|
||||
"202502131714",
|
||||
"202502171819",
|
||||
"202505091439",
|
||||
"202505141324",
|
||||
|
||||
// As of 2025-07-02, no new IDs should be added here.
|
||||
// They will be ran by the migrations.Migrate() call below.
|
||||
}
|
||||
|
||||
for _, migrationID := range migrationIDs {
|
||||
log.Trace().Caller().Str("migration_id", migrationID).Msg("running migration")
|
||||
needsFKDisabled := migrationsRequiringFKDisabled[migrationID]
|
||||
|
||||
if needsFKDisabled {
|
||||
// Disable foreign keys for this migration
|
||||
err := dbConn.Exec("PRAGMA foreign_keys = OFF").Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("disabling foreign keys for migration %s: %w", migrationID, err)
|
||||
}
|
||||
} else {
|
||||
// Ensure foreign keys are enabled for this migration
|
||||
err := dbConn.Exec("PRAGMA foreign_keys = ON").Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("enabling foreign keys for migration %s: %w", migrationID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run up to this specific migration (will only run the next pending migration)
|
||||
err := migrations.MigrateTo(migrationID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("running migration %s: %w", migrationID, err)
|
||||
}
|
||||
// Run up to and including the last migration that requires FK disabled.
|
||||
if err := migrations.MigrateTo("202501311657"); err != nil { //nolint:noinlineerr
|
||||
return fmt.Errorf("running migration 202501311657: %w", err)
|
||||
}
|
||||
|
||||
if err := dbConn.Exec("PRAGMA foreign_keys = ON").Error; err != nil { //nolint:noinlineerr
|
||||
|
||||
Reference in New Issue
Block a user