package db

import (
	"github.com/tidbcloud/cluster-service-ng/pkg/storage/mysql/schema"
	"gorm.io/gorm"
)

// Migration 20260701001: introduce shared_pools.high_availability_type.
//
// The previous implementation used migrator.HasColumn + migrator.AddColumn, which on
// some MySQL configurations silently no-ops the ALTER TABLE while still recording the
// migration ID in the tracker. We saw the symptom "tracker says run + column missing".
//
// Fix: switch to db.AutoMigrate(&schema.SharedPool{}), the same idempotent pattern
// migrate_20260512001.go uses for the same struct. AutoMigrate compares the current
// schema with the target struct and emits the DDL needed to reconcile, which the
// gorm team guarantees includes missing columns regardless of MySQL configuration.
// The HasColumn check is no longer needed: AutoMigrate is a no-op when the column
// already exists, so the guard only added noise. Data migration (UPDATE
// shared_tiproxy rows to 'ZONAL') is preserved inside the same function so schema
// and data migration run together under one migration record.
var _ = AddDBMigrateRecord(
	"20260701001",
	func(db *gorm.DB) error {
		if err := db.AutoMigrate(&schema.SharedPool{}); err != nil {
			return err
		}
		return db.Model(&schema.SharedPool{}).
			Where("deploy_type = ?", schema.SharedPoolDeployTypeSharedTiProxy).
			Update("high_availability_type", "ZONAL").
			Error
	},
)
