package bootstate

import (
	"context"
	"database/sql"
)

// ConnectorEventExecutionOwnerColumnReady reports whether the durable
// mixed-version execution-owner fence is present on _System_ConnectorEvent.
// Auto startup uses this live check together with the release receipt so a
// stale completed ApplyAll marker cannot hide schema drift or a prior-release
// database that has not applied the fence yet.
func ConnectorEventExecutionOwnerColumnReady(ctx context.Context, db *sql.DB) (bool, error) {
	var columns int
	err := db.QueryRowContext(ctx, `
SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE TABLE_SCHEMA = DATABASE()
   AND TABLE_NAME = '_System_ConnectorEvent'
   AND COLUMN_NAME = 'ExecutionOwner'`).Scan(&columns)
	if err != nil {
		return false, err
	}
	return columns == 1, nil
}
