// Copyright 2026 Moko Consulting <hello@mokoconsulting.tech>
// SPDX-License-Identifier: GPL-3.0-or-later

package v1_27

import "xorm.io/xorm"

// DropDisplayNameColumns removes the display_name column from repo_manifest
// and update_stream_config. Display name is now computed from extension_type + name.
func DropDisplayNameColumns(x *xorm.Engine) error {
	// MySQL 8.0.x does not support DROP COLUMN IF EXISTS — check first.
	for _, table := range []string{"repo_manifest", "update_stream_config"} {
		var count int
		if _, err := x.SQL("SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = 'display_name'", table).Get(&count); err != nil {
			return err
		}
		if count > 0 {
			if _, err := x.Exec("ALTER TABLE `" + table + "` DROP COLUMN `display_name`"); err != nil {
				return err
			}
		}
	}
	return nil
}
