using SqlSugar;
namespace FoodLabeling.Application.Helpers;
///
/// 探测 Role.AccessPermissionCodes 列是否已落库(与 RoleAggregateRoot.AccessPermissionCodesJson 映射一致)。
///
public static class RoleAccessPermissionSchemaHelper
{
private static bool? _hasColumn;
/// 库列名(非 C# 属性名 AccessPermissionCodesJson)。
public const string ColumnName = "AccessPermissionCodes";
public static async Task HasColumnAsync(ISqlSugarClient db)
{
if (_hasColumn is not null)
{
return _hasColumn.Value;
}
try
{
var count = await db.Ado.GetIntAsync(
"""
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'Role'
AND COLUMN_NAME = @col
""",
new { col = ColumnName });
_hasColumn = count > 0;
}
catch
{
_hasColumn = false;
}
return _hasColumn.Value;
}
/// 单元测试或切换库后调用。
public static void ResetCacheForTests() => _hasColumn = null;
}