package migrate

import (
	apprmod "github.com/tidbcloud/devops/models/approval"
	ccmod "github.com/tidbcloud/devops/models/cloudconfig"
	opmod "github.com/tidbcloud/devops/models/operation"

	"gorm.io/gorm"
)

// migration for approval table to add the configuration column.
var _ = AddMigrateRecord(
	"202607081400",
	func(db *gorm.DB) error {
		if err := db.AutoMigrate(&apprmod.Approval{}, &opmod.OperationInstance{}, &ccmod.ConfigurationChange{}); err != nil {
			return err
		}
		for _, sql := range []string{`
UPDATE operation_instances
SET operator_entity = CONCAT('user:', applicant)
WHERE operator_entity IS NULL OR operator_entity = '';
		`, `
UPDATE cloudconfig_configuration_changes
SET operator_entity = CONCAT('user:', applicant)
WHERE operator_entity IS NULL OR operator_entity = '';
		`} {
			if err := db.Exec(sql).Error; err != nil {
				return err
			}
		}
		return nil
	},
)
