Bc Upgrade Codeunit Generator 标志

Bc Upgrade Codeunit Generator

社区
fernandoartalf
bc-upgrade-codeunit-generator

Generates upgrade codeunits (Subtype = Upgrade) for Business Central extensions that run data migration code when upgrading to a new version. Creates codeunits with OnUpgradePerCompany and OnUpgradePerDatabase triggers, precondition checks via OnCheckPreconditionsPerCompany and OnCheckPreconditionsPerDatabase, post-upgrade validation via OnValidateUpgradePerCompany and OnValidateUpgradePerDatabase, upgrade tag management using the System Application Upgrade Tag module (HasUpgradeTag, SetUpgradeTag, OnGetPerCompanyUpgradeTags, OnGetPerDatabaseUpgradeTags), companion upgrade tag definitions codeunit, install codeunit integration for setting tags on fresh install, version-based upgrade control using ModuleInfo.DataVersion, ExecutionContext guards for protecting sensitive code during upgrades, and NavApp.RestoreArchiveData for archived data restoration. Handles field migration, new table population, enum conversion, data patching, and broken upgrade fixes. Use when asked to create upgrade code, add data migration between versions, implement upgrade logic, create upgrade codeunit, migrate fields to new tables, handle schema changes on upgrade, add upgrade tags, or implement version upgrade handling for a Business Central extension.

概览

发布者fernandoartalf
代码仓库AL-Copilot-Skills-Collection
技能名称bc-upgrade-codeunit-generator
星标数
28
复刻数
8
附带文件
3
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 3 个附带文件

    模型执行任务时可读取的脚本、模板和参考资料。这些文件为只读文件,绝不会被执行。

  • 开源

    由 fernandoartalf 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Bc Upgrade Codeunit Generator AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/fernandoartalf/AL-Copilot-Skills-Collection/tree/main/skills/bc-upgrade-codeunit-generator
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/fernandoartalf/AL-Copilot-Skills-Collection.git /tmp/AL-Copilot-Skills-Collection
mkdir -p .claude/skills
cp -r /tmp/AL-Copilot-Skills-Collection/skills/bc-upgrade-codeunit-generator .claude/skills/bc-upgrade-codeunit-generator
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Bc Upgrade Codeunit Generator,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Bc Upgrade Codeunit Generator。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Bc Upgrade Codeunit Generator 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Business Central Upgrade Codeunit Generator

Generates production-ready upgrade codeunits for Business Central extensions. Handles data migration between versions, precondition validation, upgrade tag management, and post-upgrade verification.

Overview

Upgrade codeunits (Subtype = Upgrade) run automatically when an extension is upgraded to a newer version (higher version number in app.json). They do not run during fresh installs or reinstalls — use install codeunits for that.

Upgrade code runs when:

  • A new version of an already-installed extension is published and data-upgraded
  • The new version has a higher version number than the currently installed version

This skill generates:

  • Upgrade codeunit with all six triggers
  • Precondition checks to block incompatible upgrades
  • Data migration procedures with upgrade tag guards
  • Post-upgrade validation
  • Companion upgrade tag definitions codeunit
  • Install codeunit integration (setting tags on fresh install)
  • ExecutionContext guards for sensitive event subscribers

Complete examples and patterns: references/upgrade-examples.md

Prerequisites

  • AL workspace with established object ID range
  • Prefix established (e.g., BCS, ABC, etc.)
  • Knowledge of what data needs migration between versions
  • System Application dependency (for Upgrade Tag module)
  • Existing install codeunit (to register upgrade tags on fresh install)

Quick Start — Version-Based Control

For simple extensions with few versions:

al
codeunit [ID] "[Prefix] Upgrade"
{
    Subtype = Upgrade;

    trigger OnCheckPreconditionsPerCompany()
    begin
        // Validate upgrade can proceed
    end;

    trigger OnUpgradePerCompany()
    var
        AppInfo: ModuleInfo;
    begin
        NavApp.GetCurrentModuleInfo(AppInfo);

        // DataVersion = version upgrading FROM
        if AppInfo.DataVersion < Version.Create(2, 0, 0, 0) then
            UpgradeToV2();
    end;

    trigger OnValidateUpgradePerCompany()
    begin
        // Verify upgrade succeeded
    end;

    local procedure UpgradeToV2()
    begin
        // Data migration logic
    end;
}

Quick Start — Upgrade Tags (Recommended)

For larger applications or frequent version changes:

al
codeunit [ID] "[Prefix] Upgrade"
{
    Subtype = Upgrade;

    trigger OnUpgradePerCompany()
    begin
        MigrateFieldData();
    end;

    local procedure MigrateFieldData()
    var
        UpgradeTagDef: Codeunit "[Prefix] Upgrade Tag Def.";
        UpgradeTag: Codeunit "Upgrade Tag";
    begin
        if UpgradeTag.HasUpgradeTag(UpgradeTagDef.GetFieldMigrationTag()) then
            exit;

        // Migration logic here

        UpgradeTag.SetUpgradeTag(UpgradeTagDef.GetFieldMigrationTag());
    end;
}

codeunit [ID+1] "[Prefix] Upgrade Tag Def."
{
    Access = Internal;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Upgrade Tag", OnGetPerCompanyUpgradeTags, '', false, false)]
    local procedure OnGetPerCompanyTags(var PerCompanyUpgradeTags: List of [Code[250]])
    begin
        PerCompanyUpgradeTags.Add(GetFieldMigrationTag());
    end;

    procedure GetFieldMigrationTag(): Code[250]
    begin
        exit('[PREFIX]-[ID]-[Description]-[YYYYMMDD]');
    end;
}

Upgrade Triggers

Triggers execute in the following guaranteed order:

OrderTriggerScopePurpose
1OnCheckPreconditionsPerDatabase()OnceValidate database-level prerequisites
2OnCheckPreconditionsPerCompany()Per companyValidate company-level prerequisites
3OnUpgradePerDatabase()OnceExecute database-wide data migration
4OnUpgradePerCompany()Per companyExecute company-specific data migration
5OnValidateUpgradePerDatabase()OnceVerify database-level upgrade success
6OnValidateUpgradePerCompany()Per companyVerify company-level upgrade success

Key rules:

  • PerCompany triggers run once for each company in the database
  • PerDatabase triggers run once regardless of number of companies
  • Errors in precondition triggers abort the entire upgrade
  • Errors in validation triggers mark the upgrade as failed
  • Multiple upgrade codeunits may exist but execution order between them is not guaranteed

Version Data in Upgrade Context

PropertyValue During Upgrade
AppVersionVersion upgrading to (new version)
DataVersionVersion upgrading from (old version)
al
var
    AppInfo: ModuleInfo;
begin
    NavApp.GetCurrentModuleInfo(AppInfo);
    // AppInfo.DataVersion = old version (upgrading from)
    // AppInfo.AppVersion = new version (upgrading to)
end;

Controlling When Upgrade Code Runs

Method 1: Version Comparison

Best for simple extensions with manual version control:

al
trigger OnUpgradePerCompany()
var
    AppInfo: ModuleInfo;
begin
    NavApp.GetCurrentModuleInfo(AppInfo);

    if AppInfo.DataVersion < Version.Create(2, 0, 0, 0) then
        UpgradeToV2();

    if AppInfo.DataVersion < Version.Create(2, 1, 0, 0) then
        UpgradeToV2_1();
end;

Method 2: Upgrade Tags (Recommended)

Best for larger applications, frequent releases, or complex upgrade chains:

al
local procedure MigrateData()
var
    UpgradeTagDef: Codeunit "[Prefix] Upgrade Tag Def.";
    UpgradeTag: Codeunit "Upgrade Tag";
begin
    if UpgradeTag.HasUpgradeTag(UpgradeTagDef.GetMyMigrationTag()) then
        exit;

    // Perform migration

    UpgradeTag.SetUpgradeTag(UpgradeTagDef.GetMyMigrationTag());
end;

When to Use Which

CriteriaVersion ComparisonUpgrade Tags
Large application with many versionsRecommended
Version changes frequently (>1x/year)Recommended
Version is set manuallyRecommended
Checking for first-time install (0.0.0.0)Recommended
Fixing a broken upgradeRecommended

Upgrade Tag Convention

Format: [Prefix]-[ObjectID]-[Description]-[YYYYMMDD]

Examples:

  • BCS-50100-ShoeSizeUpgrade-20260101
  • BCS-50100-FieldMigration-20260315
  • ABC-50200-EnumConversion-20260401

Three-Codeunit Pattern (Upgrade Tags)

When using upgrade tags, implement three companion codeunits:

1. Upgrade Codeunit (Subtype = Upgrade)

Contains the actual upgrade logic, guarded by HasUpgradeTag / SetUpgradeTag.

2. Upgrade Tag Definitions Codeunit

  • Defines tag values as procedures (not hard-coded strings)
  • Subscribes to OnGetPerCompanyUpgradeTags / OnGetPerDatabaseUpgradeTags to register tags for new companies
  • Access = Internal to prevent external dependencies on tag values

3. Install Codeunit (Subtype = Install)

  • Calls UpgradeTag.SetAllUpgradeTags() (or sets individual tags) on OnInstallAppPerCompany
  • Ensures upgrade code doesn't run on freshly installed extensions

Common Upgrade Patterns

Pattern 1: Field Migration (Old Field → New Field)

al
local procedure MigrateFieldData()
var
    UpgradeTagDef: Codeunit "[Prefix] Upgrade Tag Def.";
    UpgradeTag: Codeunit "Upgrade Tag";
    Customer: Record Customer;
begin
    if UpgradeTag.HasUpgradeTag(UpgradeTagDef.GetFieldMigrationTag()) then
        exit;

    Customer.SetLoadFields("[Prefix] Old Field", "[Prefix] New Field");
    Customer.SetFilter("[Prefix] Old Field", '<>%1', '');
    Customer.SetRange("[Prefix] New Field", '');
    if Customer.FindSet() then
        repeat
            Customer."[Prefix] New Field" := Customer."[Prefix] Old Field";
            Customer.Modify(false);
        until Customer.Next() = 0;

    UpgradeTag.SetUpgradeTag(UpgradeTagDef.GetFieldMigrationTag());
end;

Pattern 2: Populate New Table from Existing Data

al
local procedure PopulateNewTable()
var
    UpgradeTagDef: Codeunit "[Prefix] Upgrade Tag Def.";
    UpgradeTag: Codeunit "Upgrade Tag";
    Source: Record "Source Table";
    Target: Record "[Prefix] New Table";
begin
    if UpgradeTag.HasUpgradeTag(UpgradeTagDef.GetNewTableTag()) then
        exit;

    Source.SetLoadFields("No.", "Field A", "Field B");
    if Source.FindSet() then
        repeat
            if not Target.Get(Source."No.") then begin
                Target.Init();
                Target."Entry No." := Source."No.";
                Target."Field A" := Source."Field A";
                Target."Field B" := Source."Field B";
                Target.Insert(false);
            end;
        until Source.Next() = 0;

    UpgradeTag.SetUpgradeTag(UpgradeTagDef.GetNewTableTag());
end;

Pattern 3: Enum Value Migration

al
local procedure MigrateEnumValues()
var
    UpgradeTagDef: Codeunit "[Prefix] Upgrade Tag Def.";
    UpgradeTag: Codeunit "Upgrade Tag";
    MyRecord: Record "[Prefix] My Record";
begin
    if UpgradeTag.HasUpgradeTag(UpgradeTagDef.GetEnumMigrationTag()) then
        exit;

    MyRecord.SetLoadFields(Status, "Legacy Status Code");
    MyRecord.SetRange(Status, MyRecord.Status::" ");
    MyRecord.SetFilter("Legacy Status Code", '<>%1', 0);
    if MyRecord.FindSet() then
        repeat
            case MyRecord."Legacy Status Code" of
                1:
                    MyRecord.Status := MyRecord.Status::Draft;
                2:
                    MyRecord.Status := MyRecord.Status::Active;
                3:
                    MyRecord.Status := MyRecord.Status::Closed;
            end;
            MyRecord.Modify(false);
        until MyRecord.Next() = 0;

    UpgradeTag.SetUpgradeTag(UpgradeTagDef.GetEnumMigrationTag());
end;

Pattern 4: Default Value Seeding on Upgrade

al
local procedure SetNewFieldDefaults()
var
    UpgradeTagDef: Codeunit "[Prefix] Upgrade Tag Def.";
    UpgradeTag: Codeunit "Upgrade Tag";
    Setup: Record "[Prefix] Module Setup";
begin
    if UpgradeTag.HasUpgradeTag(UpgradeTagDef.GetDefaultValuesTag()) then
        exit;

    if Setup.Get() then begin
        Setup."New Feature Enabled" := false;
        Setup."Max Items" := 100;
        Setup.Modify(false);
    end;

    UpgradeTag.SetUpgradeTag(UpgradeTagDef.GetDefaultValuesTag());
end;

Pattern 5: Data Archive Restoration

al
trigger OnUpgradePerDatabase()
begin
    NavApp.RestoreArchiveData(Database::"[Prefix] Custom Table");
end;

Pattern 6: Precondition — Block Incompatible Versions

al
trigger OnCheckPreconditionsPerCompany()
var
    AppInfo: ModuleInfo;
begin
    NavApp.GetCurrentModuleInfo(AppInfo);

    if AppInfo.DataVersion < Version.Create(1, 0, 0, 0) then
        Error(MinVersionRequiredErr, '1.0.0.0', Format(AppInfo.DataVersion));
end;

var
    MinVersionRequiredErr: Label 'Upgrade requires minimum version %1. Current data version is %2.', Comment = '%1 = Required version, %2 = Current version';

Pattern 7: Post-Upgrade Validation

al
trigger OnValidateUpgradePerCompany()
var
    Setup: Record "[Prefix] Module Setup";
begin
    if not Setup.Get() then
        Error(SetupMissingErr);

    if Setup."Required Field" = '' then
        Error(RequiredFieldBlankErr);
end;

var
    SetupMissingErr: Label 'Setup record is missing after upgrade.';
    RequiredFieldBlankErr: Label 'Required field is blank after upgrade.';

Protecting Sensitive Code During Upgrade

Event subscribers may fire during upgrades. Guard irreversible operations:

al
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnAfterPostSalesDoc, '', false, false)]
local procedure OnAfterPostSalesDoc(var SalesHeader: Record "Sales Header")
begin
    if Session.GetExecutionContext() <> ExecutionContext::Normal then
        exit;

    // Safe to call external services here
end;

ExecutionContext Values

ValueWhen
ExecutionContext::NormalNormal user/API operation
ExecutionContext::InstallDuring extension install
ExecutionContext::UninstallDuring extension uninstall
ExecutionContext::UpgradeDuring extension upgrade

Design Guidelines

  • Use upgrade tags for anything beyond trivial upgrades — they're more robust than version checks
  • Register tags for new companies via OnGetPerCompanyUpgradeTags / OnGetPerDatabaseUpgradeTags
  • Set tags on fresh install via install codeunit so upgrade code doesn't run on new data
  • Use Modify(false) during bulk migration to skip trigger execution
  • Use SetLoadFields before FindSet when iterating large tables
  • Keep safety checks — verify target fields are blank/default before overwriting
  • No UI interaction — upgrade code runs without user context
  • Idempotent — upgrade may be retried after failure; guard with tags
  • Independent codeunits — don't depend on execution order between upgrade codeunits
  • Validate after upgrade — use validation triggers to confirm migration succeeded
  • Log telemetry — record step names, record counts, and timing in Application Insights
  • Limit tag nesting — keep tag checks to max two levels of nesting

Upgrade Codeunit Design Workflow

  1. Identify data changes — What changed between versions? New fields, removed fields, table restructuring?
  2. Choose control method — Version comparison or upgrade tags?
  3. Plan migration steps — List each data transformation needed, in dependency order
  4. Create upgrade tag definitions — One tag per migration step, registered for new companies
  5. Implement upgrade codeunit — Migration logic guarded by tags with SetLoadFields and Modify(false)
  6. Add precondition checks — Block incompatible source versions
  7. Add validation checks — Verify each migration step succeeded
  8. Update install codeunit — Set upgrade tags on fresh install (SetAllUpgradeTags or individual tags)
  9. Add telemetry — Log start/end of each step with record counts
  10. Test upgrade path — Verify from each supported source version to the new version

File Naming Convention

Follow the pattern:

  • Upgrade codeunit: [Prefix]Upgrade.Codeunit.al
  • Upgrade tag definitions: [Prefix]UpgradeTagDef.Codeunit.al

Examples:

  • BCSUpgrade.Codeunit.al / BCSUpgradeTagDef.Codeunit.al
  • BCSStatAccUpgrade.Codeunit.al / BCSStatAccUpgradeTagDef.Codeunit.al

Place in: src/Codeunit/ or feature folder src/[Feature]/Codeunit/.

Checklist

Before completing upgrade codeunit generation:

  • Subtype = Upgrade set on upgrade codeunit
  • Upgrade triggers used appropriately (Preconditions → Upgrade → Validate)
  • Upgrade tags created for each migration step (if using tag pattern)
  • Tag definitions codeunit created with Access = Internal
  • Tags registered for new companies via OnGetPerCompanyUpgradeTags / OnGetPerDatabaseUpgradeTags
  • Install codeunit updated to set tags on fresh install
  • SetLoadFields used before FindSet on large tables
  • Modify(false) used for bulk data migration (skip triggers)
  • Safety checks before overwriting fields (verify blank/default)
  • No UI interaction (no Message, Confirm, Page.Run)
  • Precondition checks block incompatible source versions
  • Validation checks verify migration success
  • Telemetry logging for each step (optional)
  • ExecutionContext guards on sensitive event subscribers
  • File follows naming convention

References

For complete examples:

  • references/upgrade-examples.md — Full working examples (version-based, upgrade tags, database-level, multi-step with telemetry, ExecutionContext guards, broken upgrade fixes, archive restoration, anti-patterns)

External Resources

附带文件

技能加载后,模型会按需读取这些文件。它们仅作为可读文件提供,绝不会被执行。

常见问题

Bc Upgrade Codeunit Generator AI 技能有什么作用?

Generates upgrade codeunits (Subtype = Upgrade) for Business Central extensions that run data migration code when upgrading to a new version. Creates codeunits with OnUpgradePerCompany and OnUpgradePerDatabase triggers, precondition checks via OnCheckPreconditionsPerCompany and OnCheckPreconditionsPerDatabase, post-upgrade validation via OnValidateUpgradePerCompany and OnValidateUpgradePerDatabase, upgrade tag management using the System Application Upgrade Tag module (HasUpgradeTag, SetUpgradeTag, OnGetPerCompanyUpgradeTags, OnGetPerDatabaseUpgradeTags), companion upgrade tag definitions c...

为什么在 TypingMind 中使用 Bc Upgrade Codeunit Generator?

因为您只需安装一次,就能搭配任何模型使用。Bc Upgrade Codeunit Generator 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Bc Upgrade Codeunit Generator?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/fernandoartalf/AL-Copilot-Skills-Collection/tree/main/skills/bc-upgrade-codeunit-generator。TypingMind 会读取其中的 SKILL.md,打包相关文件,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Bc Upgrade Codeunit Generator?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Bc Upgrade Codeunit Generator 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Bc Upgrade Codeunit Generator 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Bc Upgrade Codeunit Generator AI 技能免费吗?

是的。fernandoartalf 已在 GitHub 上根据 MIT 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇